Skip to content

Commit

Permalink
fix: 标题转义
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Dec 28, 2024
1 parent 3ddeaa9 commit d0a2a54
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/utils/em.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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)};
Expand All @@ -22,17 +23,34 @@ class Em {
str = str
.replaceAll('&lt;', '<')
.replaceAll('&gt;', '>')
.replaceAll('&#34;', '"')
.replaceAll('&#39;', "'")
.replaceAll('&quot;', '"')
.replaceAll('&apos;', "'")
.replaceAll('&nbsp;', " ")
.replaceAll('&amp;', "&");

// 处理类似 &#x27; &#34; 这类的 HTML 实体字符
final entityRegex = RegExp(r'&#x([0-9A-Fa-f]+);|&#(\d+);');
str = str.replaceAllMapped(entityRegex, (match) {
if (match[1] != null) {
final hexValue = int.tryParse(match[1]!, radix: 16);
if (hexValue != null) {
return String.fromCharCode(hexValue);
}
} else if (match[2] != null) {
final decimalValue = int.tryParse(match[2]!, radix: 10);
if (decimalValue != null) {
return String.fromCharCode(decimalValue);
}
}
return match.group(0)!;
});

Map map = {'type': 'text', 'text': str};
res.add(map);
}
return str;
});

return res;
}
}

0 comments on commit d0a2a54

Please sign in to comment.