string-strip-html
Strip HTML tags from strings without a full parser, safely handling mixed and templated markup.
Repository Health
Technical Analysis
string-strip-html removes HTML tags from arbitrary strings without building a DOM or running a full HTML parser, which lets it work on mixed and malformed sources such as strings containing JSX, JSP, ESP, or other templating syntax mixed in with HTML. Instead of a tree-based parse, it walks the input, collects ranges to delete or replace using the codsen ranges-push/ranges-apply toolkit, and applies them in a single pass, which keeps behaviour predictable on partial or broken markup that would break a strict parser.
Beyond a plain strip, it exposes fine-grained controls: ignoreTags and onlyStripTags for selective stripping, stripTogetherWithTheirContents to drop tag pairs and their inner content (script/style/xml by default, extensible to things like pre), a cb callback for per-tag interception, and dumpLinkHrefsNearby to preserve link targets as visible text when stripping anchors. The result object returns not just the cleaned string but the underlying ranges, allTagLocations, and filteredTagLocations, so callers can re-apply or inspect exactly what was removed.
What You Get
- A single
stripHtml(str, opts)function returning{ result, ranges, allTagLocations, filteredTagLocations, log }for both the cleaned string and full introspection of what was removed - Selective stripping controls:
ignoreTags,onlyStripTags, andstripTogetherWithTheirContents(defaults stripscript/style/xmlbodies entirely) - A
cbcallback hook invoked per detected tag, letting callers override deletion/insertion behaviour on the fly dumpLinkHrefsNearbyoption to surfacehrefvalues as visible text instead of silently discarding them- First-class TypeScript types (
Opts,Res,Tag,Attribute,CbObj) shipped via a dedicatedtypes/index.d.ts - Pure ESM build produced by esbuild, with an older CJS-era release (8.5.0) pinned for consumers not yet on ESM
Common Use Cases
- Stripping HTML markup out of user-submitted content or CMS fields before storing or indexing plain text
- Cleaning HTML email templates or JSP/ESP/JSX-mixed source strings where a strict HTML parser would choke on template syntax
- Extracting readable text from HTML for search indexing, previews, or NLP pipelines
- Sanitizing snippets before re-inserting them into non-HTML contexts (JSON strings, plain-text emails, logs)
Under The Hood
Architecture: The core logic lives in src/main.ts (~3,300 lines) as a single stripHtml(str, opts) function that walks the input string character-by-character looking for plausible tag boundaries, gathering [from, to] deletion ranges via the sibling ranges-push package rather than constructing a DOM. Tag-pair content marked for removal (stripTogetherWithTheirContents, default script/style/xml) is tracked separately in rangedOpeningTagsForDeletion so matching closing tags can be resolved before ranges are finalized; ranges-apply then performs a single pass to produce the cleaned output, and the same ranges are returned to the caller for introspection or reapplication elsewhere.
Tech Stack: Pure ESM TypeScript compiled by esbuild, with .d.ts bundling via rollup + rollup-plugin-dts. It sits inside the codsen/codsen monorepo (npm workspaces + Turborepo + Lerna for versioning) and depends on sibling codsen packages (codsen-utils, ranges-push, ranges-apply, string-left-right) plus html-entities for entity decoding and lodash-es for a handful of utilities — a deliberately narrow, in-house-controlled dependency graph rather than reaching for a general HTML-parsing library.
Code Quality: Test coverage is enforced at the tooling level — the package’s c8 config sets check-coverage: true with lines: 100, and the test/ directory holds 32 files covering normal use, edge cases (false-positives.js, dirty.js), template dialects (jsp.js, esp.js, jsx.js), and specific options (ignoreTags.js, onlyStripTags.js, dumpLinkHrefsNearby.js), run via uvu. A separate examples/ directory with 13 runnable scripts doubles as both documentation and an additional correctness check (npm run examples).
API Design: The public surface is intentionally small — one function, one options object with sane defaults (defaults export), and one result shape (Res) that returns not just the cleaned string but also ranges, allTagLocations, and filteredTagLocations for callers who need to audit or reuse what was stripped. Naming is consistent with the wider codsen ecosystem, and getting started requires no configuration: stripHtml(str).result covers the common case in one line, while power users can reach ignoreTags, cb, or dumpLinkHrefsNearby without a different entry point.
Used by 3 apps in this directory
Activepieces
Automation · AI Assistants
Open-source AI automation platform that converts 280+ workflow integrations into MCP servers for LLMs, with no-code builders and TypeScript extensibility.
Openship
Devops · Hosting Control Panel
Openship is an open-source, self-hostable deployment platform that points at a repo and builds, ships, routes, and TLS-terminates the app — driven from a desktop app, web dashboard, or CLI.
Rocket.Chat
Team Chat
The secure, self-hosted team communications platform for organizations that cannot compromise on data sovereignty.