§ 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,
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)
templatingTagsnull 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,
reportProgressFunc: null,
reportProgressFuncFrom: 0,
reportProgressFuncTo: 100,

§ Changelog

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

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

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