3.2.0 • Published 2 years ago

striptags v3.2.0

Weekly downloads
436,355
License
MIT
Repository
github
Last release
2 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@jetshop/template-trend@carbon/ibmdotcom-utilities@instructure/quiz-interactions@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-2837podverse-sharedpodverse-uipubsweet-component-xpub-reviewpubsweet-component-xpub-submitprocobproject-spadepush-chanquiz-corequiz-interactionsslidewinderslitu-apmokslangopediarn-matrixshared-components-esmsherry_graphdocsimsardaxonstaticonvsteemviz-2raccnraconteur-wordcountradio-canada-ohdioreact-native-speech-eel-testreact-native-speakreact-native-matrix-uipostleafperspective-api-clientpstv-commerce-toolsreact-simple-text-editorreact-taggable-inputreact-truncate-domsmmry-fi-scrapersocotrasoclialsrt-to-assssb-bot-feedspamscannerspotipo-support-themesocial-editorssbifyssbify-bomtfk-seneca-collect-content-wptext-image-mergertextblockstext-cleanertistory-backup-extractortistory-backup-extractor-coretheme-persona-linh
4.0.0-alpha.3

2 years ago

4.0.0-alpha.4

2 years ago

3.2.0

3 years ago

4.0.0-alpha.2

3 years ago

4.0.0-alpha.1

3 years ago

4.0.0-alpha.0

3 years ago

3.1.1

6 years ago

3.1.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.0

10 years ago