/** * Linear scanners for text the renderer reads from markdown and from the HTML it rendered * (AGENTS.md rule 13). Each replaces a regex CodeQL reported as polynomial backtracking * (js/polynomial-redos) and gives the same answers on ordinary input; `test/text-scan.test.ts` * checks each against the regex it replaced and against a 100k-character adversarial input. */ const isSpace = (c: string | undefined): boolean => c !== undefined && /\s/.test(c); const LINE_BREAK = /[\n\r\u2028\u2029]/; /** * `html` with every tag (`<` up to the next `>`) replaced by `gap`: what * `.replace(/<[^>]*>/g, gap)` did, without rescanning to the end from every `<` when no `>` * follows. As there, a `<` with no `>` after it stays. */ export function stripTags(html: string, gap: string): string { let out = ""; let at = 0; for (;;) { const open = html.indexOf("<", at); if (open === -1) break; const close = html.indexOf(">", open + 1); if (close === -1) break; out += html.slice(at, open) + gap; at = close + 1; } return at === 0 ? html : out + html.slice(at); } const NAMED: Record = { amp: "&", lt: "<", gt: ">", quot: '"', "#39": "'" }; /** * Text of HTML this module's callers rendered (meta and feed descriptions, JSON-LD, * table-of-contents labels, heading ids, reading time): tags dropped, entities decoded once, * whitespace collapsed. * * **Plain text, not HTML.** Decoding is the point (`Array<string>` reads `Array`), * so the result can hold `` or `