From b6217f6e6e0eec19a43ff4b43498cfbb535e04c0 Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Sun, 29 Dec 2024 10:57:44 +0800 Subject: [PATCH] opt: regTitle Signed-off-by: bggRGjQaUbCoE --- lib/utils/em.dart | 51 +++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/utils/em.dart b/lib/utils/em.dart index 2c5af8ba..3c617233 100644 --- a/lib/utils/em.dart +++ b/lib/utils/em.dart @@ -1,3 +1,5 @@ +import 'package:html/parser.dart' show parse; + class Em { static regCate(String origin) { String str = origin; @@ -12,32 +14,37 @@ class Em { static regTitle(String origin) { RegExp exp = RegExp('<[^>]*>([^<]*)]*>'); List res = []; - origin.splitMapJoin(exp, onMatch: (Match match) { - String matchStr = match[0]!; - Map map = {'type': 'em', 'text': regCate(matchStr)}; - res.add(map); - return regCate(matchStr); - }, onNonMatch: (String str) { - if (str != '') { - str = decodeHtmlEntities(str); - Map map = {'type': 'text', 'text': str}; + origin.splitMapJoin( + exp, + onMatch: (Match match) { + String matchStr = match[0]!; + Map map = {'type': 'em', 'text': regCate(matchStr)}; res.add(map); - } - return str; - }); + return regCate(matchStr); + }, + onNonMatch: (String str) { + if (str != '') { + str = parse(str).body?.text ?? str; + Map map = {'type': 'text', 'text': str}; + res.add(map); + } + return str; + }, + ); return res; } static String decodeHtmlEntities(String title) { - return title - .replaceAll('<', '<') - .replaceAll('>', '>') - .replaceAll('"', '"') - .replaceAll(''', "'") - .replaceAll('"', '"') - .replaceAll(''', "'") - .replaceAll(' ', " ") - .replaceAll('&', "&") - .replaceAll(''', "'"); + return parse(title).body?.text ?? title; + // return title + // .replaceAll('<', '<') + // .replaceAll('>', '>') + // .replaceAll('"', '"') + // .replaceAll(''', "'") + // .replaceAll('"', '"') + // .replaceAll(''', "'") + // .replaceAll(' ', " ") + // .replaceAll('&', "&") + // .replaceAll(''', "'"); } }