| 1 | export interface HtmlTextOptions { |
| 2 | stripScripts?: boolean; |
| 3 | stripCapturePre?: boolean; |
| 4 | tagReplacement?: string; |
| 5 | decodeEntities?: boolean; |
| 6 | } |
| 7 | |
| 8 | const ENTITY_MAP: Record<string, string> = { |
| 9 | "<": "<", |
| 10 | ">": ">", |
| 11 | """: '"', |
| 12 | "'": "'", |
| 13 | "&": "&", |
| 14 | }; |
| 15 | |
| 16 | function decodeBasicEntities(text: string): string { |
| 17 | let out = ""; |
| 18 | let i = 0; |
| 19 | while (i < text.length) { |
| 20 | if (text[i] === "&") { |
| 21 | const semi = text.indexOf(";", i + 1); |
| 22 | if (semi > i && semi - i <= 8) { |
| 23 | const entity = text.slice(i, semi + 1); |
| 24 | const mapped = ENTITY_MAP[entity]; |
| 25 | if (mapped !== undefined) { |
| 26 | out += mapped; |
| 27 | i = semi + 1; |
| 28 | continue; |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | out += text[i]; |
| 33 | i += 1; |
| 34 | } |
| 35 | return out; |
| 36 | } |
| 37 | |
| 38 | function tagNameAt(html: string, start: number): { closing: boolean; name: string; end: number; raw: string } | null { |
| 39 | const close = html.indexOf(">", start + 1); |
| 40 | if (close === -1) return null; |
| 41 | const raw = html.slice(start + 1, close); |
| 42 | const trimmed = raw.trimStart(); |
| 43 | const closing = trimmed.startsWith("/"); |
| 44 | const body = closing ? trimmed.slice(1).trimStart() : trimmed; |
| 45 | let nameEnd = 0; |
| 46 | while (nameEnd < body.length && /[A-Za-z0-9-]/.test(body[nameEnd])) { |
| 47 | nameEnd += 1; |
| 48 | } |
| 49 | return { closing, name: body.slice(0, nameEnd).toLowerCase(), end: close, raw }; |
| 50 | } |
| 51 | |
| 52 | export function extractHtmlText(html: string, options: HtmlTextOptions = {}): string { |
| 53 | const { |
| 54 | stripScripts = false, |
| 55 | stripCapturePre = false, |
| 56 | tagReplacement = " ", |
| 57 | decodeEntities = false, |
| 58 | } = options; |
| 59 | let out = ""; |
| 60 | let i = 0; |
| 61 | let skipUntilTag: string | null = null; |
| 62 | |
| 63 | while (i < html.length) { |
| 64 | const ch = html[i]; |
| 65 | if (ch === "<") { |
| 66 | const tag = tagNameAt(html, i); |
| 67 | if (tag) { |
| 68 | if (skipUntilTag !== null) { |
| 69 | if (tag.closing && tag.name === skipUntilTag) { |
| 70 | skipUntilTag = null; |
| 71 | out += tagReplacement; |
| 72 | } |
| 73 | i = tag.end + 1; |
| 74 | continue; |
| 75 | } |
| 76 | if (!tag.closing && stripScripts && tag.name === "script") { |
| 77 | skipUntilTag = "script"; |
| 78 | i = tag.end + 1; |
| 79 | continue; |
| 80 | } |
| 81 | if ( |
| 82 | !tag.closing && |
| 83 | stripCapturePre && |
| 84 | tag.name === "pre" && |
| 85 | tag.raw.toLowerCase().includes("data-capture-file") |
| 86 | ) { |
| 87 | skipUntilTag = "pre"; |
| 88 | i = tag.end + 1; |
| 89 | continue; |
| 90 | } |
| 91 | out += tagReplacement; |
| 92 | i = tag.end + 1; |
| 93 | continue; |
| 94 | } |
| 95 | } |
| 96 | if (skipUntilTag === null) { |
| 97 | out += ch; |
| 98 | } |
| 99 | i += 1; |
| 100 | } |
| 101 | |
| 102 | return decodeEntities ? decodeBasicEntities(out) : out; |
| 103 | } |
| 104 | |
| 105 | export function extractJsonLdScripts(html: string): string[] { |
| 106 | const blocks: string[] = []; |
| 107 | let i = 0; |
| 108 | while (i < html.length) { |
| 109 | const open = html.indexOf("<", i); |
| 110 | if (open === -1) break; |
| 111 | const tag = tagNameAt(html, open); |
| 112 | if (!tag) break; |
| 113 | if (!tag.closing && tag.name === "script" && tag.raw.toLowerCase().includes('type="application/ld+json"')) { |
| 114 | let cursor = tag.end + 1; |
| 115 | let endIdx = -1; |
| 116 | while (cursor < html.length) { |
| 117 | const nextOpen = html.indexOf("<", cursor); |
| 118 | if (nextOpen === -1) break; |
| 119 | const endTag = tagNameAt(html, nextOpen); |
| 120 | if (endTag && endTag.closing && endTag.name === "script") { |
| 121 | endIdx = nextOpen; |
| 122 | i = endTag.end + 1; |
| 123 | break; |
| 124 | } |
| 125 | cursor = nextOpen + 1; |
| 126 | } |
| 127 | if (endIdx !== -1) { |
| 128 | blocks.push(html.slice(tag.end + 1, endIdx)); |
| 129 | continue; |
| 130 | } |
| 131 | } |
| 132 | i = tag.end + 1; |
| 133 | } |
| 134 | return blocks; |
| 135 | } |
| 136 |