fix(baoyu-post-to-wechat): repair htmlToPlainText syntax error and entity decoding

Follow-up to the previous newspic conversion commit:

- `“` / `”` mapped to `"""` (three straight double-quotes),
  producing an unterminated string literal that prevented wechat-api.ts
  from parsing. Replaced with the intended curly Unicode characters.
- `‘` / `’` similarly mapped to straight `'`; switched to the
  curly singles to match the entity meaning.
- Numeric entities used `String.fromCharCode`, which mangles code points
  above 0xFFFF (e.g. emoji like `😀` → empty string). Switched to
  `String.fromCodePoint`.
- Added hexadecimal numeric entity support (`😀` etc.); the
  previous regex only matched decimal forms.
- Anchored the entity sub-patterns with `^...$` so the callback cannot
  accidentally re-match a substring of the captured entity.
This commit is contained in:
Jim Liu 宝玉
2026-05-24 18:57:01 -05:00
parent 3aafe60463
commit b3a3b632c8
@@ -125,17 +125,20 @@ function htmlToPlainText(html: string): string {
"—": "—",
"–": "",
"…": "…",
"“": """,
"”": """,
"‘": "'",
"’": "'",
"“": "",
"”": "",
"‘": "",
"’": "",
};
text = text.replace(/&(?:[a-zA-Z]+|#\d+);/g, (entity) => {
text = text.replace(/&(?:[a-zA-Z]+|#x[0-9a-fA-F]+|#\d+);/g, (entity) => {
if (entityMap[entity]) return entityMap[entity];
// 处理数字实体,如 {
const numMatch = entity.match(/&#(\d+);/);
const hexMatch = entity.match(/^&#x([0-9a-fA-F]+);$/);
if (hexMatch) {
return String.fromCodePoint(Number.parseInt(hexMatch[1]!, 16));
}
const numMatch = entity.match(/^&#(\d+);$/);
if (numMatch) {
return String.fromCharCode(Number.parseInt(numMatch[1]!, 10));
return String.fromCodePoint(Number.parseInt(numMatch[1]!, 10));
}
return entity;
});