3.2.0 • Published 2 years ago
striptags v3.2.0
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 tofalse
if the output is expected to be used only as plaintext.
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