MCPcopy Create free account
hub / github.com/arctic-cli/interface / extractTextFromHTML

Function extractTextFromHTML

packages/arctic/src/tool/webfetch.ts:145–175  ·  view source on GitHub ↗
(html: string)

Source from the content-addressed store, hash-verified

143})
144
145async function extractTextFromHTML(html: string) {
146 let text = ""
147 let skipContent = false
148
149 const rewriter = new HTMLRewriter()
150 .on("script, style, noscript, iframe, object, embed", {
151 element() {
152 skipContent = true
153 },
154 text() {
155 // Skip text content inside these elements
156 },
157 })
158 .on("*", {
159 element(element) {
160 // Reset skip flag when entering other elements
161 if (!["script", "style", "noscript", "iframe", "object", "embed"].includes(element.tagName)) {
162 skipContent = false
163 }
164 },
165 text(input) {
166 if (!skipContent) {
167 text += input.text
168 }
169 },
170 })
171 .transform(new Response(html))
172
173 await rewriter.text()
174 return text.trim()
175}
176
177function convertHTMLToMarkdown(html: string): string {
178 const turndownService = new TurndownService({

Callers 1

executeFunction · 0.85

Calls 2

transformMethod · 0.80
textMethod · 0.65

Tested by

no test coverage detected