@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
46 lines (45 loc) • 2.28 kB
TypeScript
/**
* Single-pass markup removal for turning HTML into indexable text.
*
* This is text conversion, not output sanitization. The result is plain text
* for retrieval and chunking; it is not meant to be written back into a page,
* and the caller's entity decoding can legitimately turn `<b>` into
* `<b>`.
*
* Why one pass: the previous implementation chained several global regex
* replacements (script, style, comment, block end, generic tag). Each removal
* could join the text on either side of it into a new tag for the next
* replacement (`<scr<!---->ipt>`), and the lazy `[\s\S]*?` bodies rescanned
* the rest of the input once per unclosed `<script`, which is quadratic.
* Here the input is read left to right exactly once. Every character of the
* output is copied from an unremoved stretch of the input, a removed stretch
* is never re-read, and every search either consumes what it scans or is
* answered in constant time from an index computed up front, so the work is
* linear in the input length.
*
* Invariant: the output never matches `<[^>]+>`. A `<` survives only when no
* `>` follows it in the input, or when the next character is the `>` itself
* (`<>`, which is not a tag). A removed span therefore cannot leave behind,
* or assemble, a tag.
*/
/**
* Remove HTML markup from `html` in one linear pass.
*
* - `<script>` / `<style>` elements are removed with their bodies. The end
* tag may carry whitespace or attributes (`</script >`). An element with
* no usable end tag loses only its start tag, as before.
* - `<!-- ... -->` is replaced with `commentReplacement`. An unterminated
* comment is treated like any other tag and ends at the next `>`.
* - A closing tag named in `lineBreakEndTags` and a void tag named in
* `lineBreakVoidTags` (`<br>`, `<hr />`) become a newline.
* - Any other `<...>` becomes `tagReplacement`. A `<` with no `>` after it,
* and the empty `<>`, are literal text and are kept.
*
* Tag-name lists are lower case.
*/
export declare function removeHtmlMarkup(html: string, options: {
readonly tagReplacement: string;
readonly commentReplacement: string;
readonly lineBreakEndTags: readonly string[];
readonly lineBreakVoidTags: readonly string[];
}): string;