From b3a3b632c82f19f87ef6f96da7048274db3338c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Liu=20=E5=AE=9D=E7=8E=89?= Date: Sun, 24 May 2026 18:57:01 -0500 Subject: [PATCH] fix(baoyu-post-to-wechat): repair htmlToPlainText syntax error and entity decoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../scripts/wechat-api.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/skills/baoyu-post-to-wechat/scripts/wechat-api.ts b/skills/baoyu-post-to-wechat/scripts/wechat-api.ts index 5656da9..9097ff5 100644 --- a/skills/baoyu-post-to-wechat/scripts/wechat-api.ts +++ b/skills/baoyu-post-to-wechat/scripts/wechat-api.ts @@ -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; });