| 143 | }) |
| 144 | |
| 145 | async 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 | |
| 177 | function convertHTMLToMarkdown(html: string): string { |
| 178 | const turndownService = new TurndownService({ |