MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / htmlToText

Function htmlToText

src/tools/web/web-fetch.ts:82–104  ·  view source on GitHub ↗

Strip HTML tags into readable text. Lightweight; doesn't preserve structure.

(html: string)

Source from the content-addressed store, hash-verified

80
81/** Strip HTML tags into readable text. Lightweight; doesn't preserve structure. */
82function htmlToText(html: string): string {
83 // Remove script/style first so their contents don't leak through
84 html = html.replace(/<script[\s\S]*?<\/script>/gi, '');
85 html = html.replace(/<style[\s\S]*?<\/style>/gi, '');
86 html = html.replace(/<!--[\s\S]*?-->/g, '');
87 // Convert block elements to newlines for readability
88 html = html.replace(/<\/(p|div|h[1-6]|li|tr|br)[^>]*>/gi, '\n');
89 html = html.replace(/<br[^>]*>/gi, '\n');
90 // Strip all remaining tags
91 html = html.replace(/<[^>]+>/g, '');
92 // HTML entities (just the common ones)
93 html = html
94 .replace(/&nbsp;/g, ' ')
95 .replace(/&amp;/g, '&')
96 .replace(/&lt;/g, '<')
97 .replace(/&gt;/g, '>')
98 .replace(/&quot;/g, '"')
99 .replace(/&#39;/g, "'")
100 .replace(/&apos;/g, "'");
101 // Collapse whitespace
102 html = html.replace(/[ \t]+/g, ' ').replace(/\n{3,}/g, '\n\n');
103 return html.trim();
104}
105
106/** Best-effort HTML→markdown. Captures headings, links, lists, code, paragraphs. */
107function htmlToMarkdown(html: string): string {

Callers 1

executeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected