From 1aca444b229b41d979f310f4856390bdb9a4a39d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E4=BA=86=E7=9D=A1=E5=A4=A7=E8=A7=89?= <64798754+stakeswky@users.noreply.github.com> Date: Tue, 24 Feb 2026 01:29:09 +0800 Subject: [PATCH] fix: properly strip HTML tags and resolve entities in feed article summaries (#149) * fix: properly strip HTML tags and resolve entities in feed article summaries Fixes #146 The parseTextFromHtml function was using document.textContent directly on the parsed HTML document, which could leave raw HTML tags and unresolved entities in feed article summaries. Changes: - Extract text from body element to avoid document wrapper artifacts - Collapse multiple whitespace/newlines into single spaces for cleaner output - Add early return for empty/whitespace-only input - Use optional chaining for safer null handling * fix: preserve single line breaks, only collapse 2+ consecutive whitespace Address review feedback: the previous \s+ regex was too aggressive and broke text-only summaries with legitimate line breaks. Now: - Collapse runs of 2+ non-newline whitespace into a single space - Collapse 3+ consecutive newlines into double newline (paragraph break) - Single line breaks are preserved --------- Co-authored-by: User --- lib/feed.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/feed.ts b/lib/feed.ts index 77a2e3a..79e6545 100644 --- a/lib/feed.ts +++ b/lib/feed.ts @@ -222,13 +222,20 @@ export async function getUrlInfo(url: string): Promise<{ title: string; htmlBody } export async function parseTextFromHtml(html: string): Promise { - let text = ''; + if (!html || !html.trim()) { + return ''; + } await initParser(); const document = new DOMParser().parseFromString(html, 'text/html'); - text = document!.textContent; + // Extract text from body to avoid any artifacts from the document wrapper + const text = (document?.querySelector('body')?.textContent || document?.textContent || '') + // Collapse runs of 2+ whitespace/newline characters, preserving single line breaks + .replace(/[^\S\n]{2,}/g, ' ') + .replace(/\n{3,}/g, '\n\n') + .trim(); return text; }