3.2.0 • Published 3 years ago

striptags v3.2.0

Weekly downloads
436,355
License
MIT
Repository
github
Last release
3 years ago

striptags (WIP)

An implementation of PHP's strip_tags in Typescript.

Highlights

  • No dependencies
  • Prevents XSS by default

Installing

npm install striptags

Basic Usage

// commonjs format
striptags(text: string, options?: Partial<StateMachineOptions>): string;

Examples

// commonjs format
const striptags = require("striptags").striptags;

// alternatively:
// import { striptags } from "striptags";

var html = `
<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>
`.trim();

console.log(striptags(html));
console.log(striptags(html, {allowedTags: new Set(["strong"])}));
console.log(striptags(html, {tagReplacementText: "🍩"}));

Outputs:

lorem ipsum dolor sit amet
lorem ipsum <strong>dolor</strong> sit amet
🍩lorem ipsum 🍩dolor🍩 🍩sit🍩 amet🍩

Advanced Usage

class StateMachine {
    constructor(partialOptions?: Partial<StateMachineOptions>);
    consume(text: string): string;
}

The StateMachine class is similar to the striptags function, but persists state across calls to consume() so that you may safely pass in a stream of text. For example:

const StateMachine = require("striptags").StateMachine;

// alternatively:
// import { StateMachine } from "striptags";

const instance = new StateMachine();

console.log(
  instance.consume("some text with <a") + instance.consume("tag>and more text")
);

Outputs:

some text with and more text

Options

  • allowedTags: Set<string> a set containing a list of tag names to allow (e.g. new Set(["tagname"])), default: new Set([]).
  • tagReplacementText: string a string to use as replacement text when a tag is found and not allowed, default: "".
  • encodePlaintextTagDelimiters: boolean true if < and > characters immediately followed by whitespace should be HTML encoded, default: true. This is safe to set to false if the output is expected to be used only as plaintext.
@shoptet/graphql@sayl1th/databless@kentico/kontent-delivery-html-parser-node-jstheme-nodejsvnhexo-analysistext-cleaner2compassdigital.provider.menu.bamcogamestackgatsby-theme-tablexgew-nodejs-api-clientseeklocliem-theme-personanodebb-plugin-theme-liemnodebb-theme-liemnodebb-liem-theme-persona@cangir/gatsby-theme-flex@cangir/gatsby-theme-phoenixcp-preprocessornodebb-theme-persona-custom-btn@agorapulse/quill-clipboard@deity/falcon-server-envhomebridge-airnow-schmittx@dopplerhq/clisky-editor-tinyquire-cligraphdocnodebb-theme-2kutzar-corenodebb-theme-2kuniversenodebb-theme-2kuuniversejwn-jsnodebb-theme-persona-vite@amoraschi/earthmcllui@service-exchange/handlebars-helpersyoutube-subtitles-downloadergreenhat-basegreenhat-bloggergreenhat-ip-managergreenhat-sbsgreenhat-utilhomelike-feed-processor@infinitebrahmanuniverse/nolb-strip@paiondata/nextwikiprocare-nordic-apinodebb-theme-bytetimedecentraleyes-rsswbu-design-system@everything-registry/sub-chunk-2837nodebb-theme-mobvoinodebb-theme-mobvoi-twotcom-editorwiki-to-htmlwidget-starter-kitwingbot-botserviceweb-component-aspenifyvue-laravel-table-componentwebcanarywebdev-infrawfreqwords-splitterwp-search-indexwppmworkshops-comments-apiwp-plugin-infospotipo-support-themestrapi-plugin-email-designerstrapi-plugin-email-designer-5strapi-plugin-import-contentstrapi-pdf-designersweetwork-utilstfk-seneca-collect-content-wptext-image-mergertextblockstext-cleanerthomzz-hexo-theme-docvoxavokativ-jsutterson-generatorutube-cap-extrac-tsvicjesviccloudninemcvue-aw-components@dofy/youtube-caption-extractor@drorgl/youtube-captions-scraper@daiyam/remark-slate@colon/mysql@conversationai/moderator-backend-core@conversationai/perspectiveapi-js-client@dadi/dustjs-helpers@deoxys-1/md-to-site@democracy-deutschland/bt-agenda@democracy-deutschland/bt-named-polls@dragondyt/vuepress-plugin-algoliasearch@dragondyt/vuepress-theme-sakura@bnnvara/typography@cangir/gatsby-theme-casper-v3@cangir/gatsby-theme-tabler@canner/excerpt-html@prezly/slate-editor
4.0.0-alpha.3

3 years ago

4.0.0-alpha.4

3 years ago

3.2.0

4 years ago

4.0.0-alpha.2

5 years ago

4.0.0-alpha.1

5 years ago

4.0.0-alpha.0

5 years ago

3.1.1

8 years ago

3.1.0

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.1

10 years ago

2.1.0

10 years ago

2.0.4

10 years ago

2.0.3

10 years ago

2.0.2

10 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.0.0

11 years ago