§ Quick Take

import { strict as assert } from "assert";
import { stri } from "stristri";

// strips both HTML and Nunjucks, leaves text only:
    `<html><div>The price is{% if data.price > 100 %} high{% endif %}</div>`,
      html: true,
      css: true,
      text: false,
      templatingTags: true,
  "The price is high"

§ Features

  • Pick what to strip: HTML and/or CSS and/or text and/or templating tags
  • Supports well: Nunjucks/Jinja, JSP, Responsys
  • Should support all other templating languages and ESP templating tags
  • Runs off our own lexer

PS. We have string-strip-html which also strips HTML. It can't do CSS, text or templating tags but it has more granular control over the whitespace. Also, it's non-parsing so more resilient to really messed up code.

§ API - Input

stri(input, [opts])

In other words, it's a function which takes a string and an optional options.

Input argumentTypeObligatory?Description
inputStringyesText you want to strip HTML tags from
optsPlain objectnoThe Optional Options Object, see below for its API

If input arguments are supplied have any other types, an error will be thrown.

§ API - Output

The stri() function will return a plain object, for example:

log: {
timeTakenInMilliseconds: 23
result: "abc click me def",
applicableOpts: {
html: true,
css: false,
text: true,
templatingTags: false,
js: false,
templatingLang: {
name: "Nunjucks"

Here is its API:

Key's nameKey value's typeDescription
logPlain objectVarious statistics
resultStringThe result.
applicableOptsPlain object, only boolean keys from optsTells which options object keys would have made a difference for this string input, if toggled differently
templatingLangPlain objectPipes the output of detect-templating-language

§ Optional Options Object

opts is a plain object. Here are all its keys:

An Optional Options Object's keyType of its valueDefaultDescription
htmlBooleantrueShould we strip HTML, XML tags and their comments
cssBooleantrueShould we strip head CSS style tags, CSS rules and ar-rules inside
textBooleanfalseShould we strip text
templatingTagsBooleanfalseShould we strip any templating tags (like Nunjucks)
jsBooleantrueShould we strip any JS code
reportProgressFuncnull or FunctionnullPass a function, progress value, 0-100 will be passed to it
reportProgressFuncFromNatural number [0; 100]0Starting progress percentage
reportProgressFuncToNatural number [0; 100]100Ending progress percentage

The Optional Options Object is not validated; please take care of what values and of what type you pass.

Here is the Optional Options Object in one place (in case you ever want to copy it whole):

html: true,
css: true,
text: false,
templatingTags: false,
js: true,
reportProgressFunc: null,
reportProgressFuncFrom: 0,
reportProgressFuncTo: 100,

§ Changelog

See it in the monorepo opens in a new tab, on GitHub.

§ Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

§ Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 codsen-tokenizer 5.5.5
HTML and CSS lexer aimed at code with fatal errors, accepts mixed coding languages
📦 string-strip-html 8.2.11
Strips HTML tags from strings. No parser, accepts mixed sources
📦 emlint 4.5.5
Pluggable email template code linter
📦 html-crush 4.1.8
Minifies HTML/CSS: valid or broken, pure or mixed with other languages
📦 detect-is-it-html-or-xhtml 4.0.14
Answers, is the string input string more an HTML or XHTML (or neither)
📦 detect-templating-language 2.0.14
Detects various templating languages present in string
📦 html-table-patcher 4.0.14
Visual helper to place templating code around table tags into correct places