mirror of
https://github.com/bewcloud/bewcloud.git
synced 2026-03-11 08:54:49 +00:00
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
This commit is contained in:
parent
6d5ee7b53c
commit
2297ba8b4d
1 changed files with 8 additions and 2 deletions
10
lib/feed.ts
10
lib/feed.ts
|
|
@ -222,13 +222,19 @@ export async function getUrlInfo(url: string): Promise<{ title: string; htmlBody
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function parseTextFromHtml(html: string): Promise<string> {
|
export async function parseTextFromHtml(html: string): Promise<string> {
|
||||||
let text = '';
|
if (!html || !html.trim()) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
await initParser();
|
await initParser();
|
||||||
|
|
||||||
const document = new DOMParser().parseFromString(html, 'text/html');
|
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 multiple whitespace/newlines into single spaces
|
||||||
|
.replace(/\s+/g, ' ')
|
||||||
|
.trim();
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue