### Install striptags via npm Source: https://github.com/ericnorris/striptags/blob/main/README.md This command installs the alpha version of the striptags library using npm. It's recommended for testing new features before the official release. ```bash npm install striptags@alpha ``` -------------------------------- ### Configure StateMachineOptions for HTML Sanitization Source: https://context7.com/ericnorris/striptags/llms.txt Demonstrates how to use the StateMachineOptions interface to define allowed or disallowed tags, set replacement text, and toggle plaintext delimiter encoding for secure HTML processing. ```typescript import { striptags, StateMachine, StateMachineOptions } from "striptags"; const options: Partial = { allowedTags: new Set(["a", "strong", "em", "p", "br"]), disallowedTags: new Set(["script", "style", "iframe"]), tagReplacementText: " ", encodePlaintextTagDelimiters: true }; const userContent = `

Hello!

Check out < this > and click here `; const sanitized = striptags(userContent, { allowedTags: new Set(["p", "a", "br", "strong", "em"]), tagReplacementText: "", encodePlaintextTagDelimiters: true }); console.log(sanitized); ``` -------------------------------- ### Basic striptags usage in TypeScript Source: https://github.com/ericnorris/striptags/blob/main/README.md Demonstrates the basic usage of the `striptags` function in TypeScript. It shows how to remove all HTML tags, allow specific tags, and replace disallowed tags with custom text. ```typescript striptags(text: string, options?: Partial): string; ``` ```javascript // commonjs const striptags = require("striptags").striptags; // alternatively, as an es6 import // import { striptags } from "striptags"; var html = ` lorem ipsum dolor sit amet `.trim(); console.log(striptags(html)); console.log(striptags(html, { allowedTags: new Set(["strong"]) })); console.log(striptags(html, { tagReplacementText: "🍩" })); ``` -------------------------------- ### Process streaming HTML with StateMachine Source: https://context7.com/ericnorris/striptags/llms.txt Shows how to use the StateMachine class for stateful, chunked HTML processing. This is useful for large documents or real-time streams where tags may be split across multiple data packets. ```typescript import { StateMachine } from "striptags"; const machine = new StateMachine(); const chunk1 = machine.consume("some text with link'); const chunk3 = machine.consume(" and more text"); console.log(chunk1 + chunk2 + chunk3); const streamMachine = new StateMachine({ allowedTags: new Set(["b", "i"]), tagReplacementText: "" }); const htmlParts = [ "
Start ", "bold ", "and italic", " text
" ]; let result = ""; for (const part of htmlParts) { result += streamMachine.consume(part); } console.log(result); const filterMachine = new StateMachine({ disallowedTags: new Set(["script", "style"]), tagReplacementText: " " }); const unsafeHtml = `

Safe content

More safe

`; const safeOutput = filterMachine.consume(unsafeHtml); console.log(safeOutput); ``` -------------------------------- ### StateMachineOptions Source: https://github.com/ericnorris/striptags/blob/main/README.md Configuration options for customizing the behavior of the striptags function and StateMachine class. ```APIDOC ## StateMachineOptions ### Description These options allow fine-grained control over how HTML tags are processed and stripped. ### Parameters #### Query Parameters None #### Request Body - **allowedTags** (Set) - Optional - A set of tag names to allow. Tags not in this list will be removed. Takes precedence over `disallowedTags`. Default: `undefined` - **disallowedTags** (Set) - Optional - A set of tag names to disallow. Tags not in this list will be allowed. Ignored if `allowedTags` is set. Default: `undefined` - **tagReplacementText** (string) - Optional - A string to use as replacement text when a tag is found and not allowed. Default: `""` - **encodePlaintextTagDelimiters** (boolean) - Optional - If true, `<` and `>` characters immediately followed by whitespace will be HTML encoded. Set to `false` if output is only used as plaintext. Default: `true` ### Request Example ```javascript // Example using options const striptags = require("striptags").striptags; var html = `

This is important.

`; // Allow only strong tags console.log(striptags(html, { allowedTags: new Set(["strong"]) })); // Replace disallowed tags with a specific string console.log(striptags(html, { tagReplacementText: "[REPLACED]" })); // Disable encoding of plaintext tag delimiters console.log(striptags("Plain text with < tags", { encodePlaintextTagDelimiters: false })); ``` ### Response #### Success Response (string) - Returns the processed string based on the provided options. #### Response Example ``` This is important. [REPLACED]This is [REPLACED]important[REPLACED].[REPLACED] Plain text with < tags ``` ``` -------------------------------- ### Importing striptags via CommonJS and ES6 Source: https://context7.com/ericnorris/striptags/llms.txt Shows the various ways to import the striptags library into a project, supporting both Node.js CommonJS require syntax and modern ES6 import statements. ```javascript // CommonJS (Node.js) const { striptags, StateMachine } = require("striptags"); // ES6 Modules import { striptags, StateMachine } from "striptags"; const html = "

Title

Content with emphasis

"; console.log(striptags(html, { allowedTags: new Set(["em"]) })); ``` -------------------------------- ### StateMachine Class API Source: https://github.com/ericnorris/striptags/blob/main/README.md The StateMachine class allows for processing text in chunks, maintaining state between calls, suitable for streaming scenarios. ```APIDOC ## StateMachine Class ### Description Provides a stateful way to strip tags, allowing you to consume text in chunks. Useful for processing streams of data. ### Method Class Instantiation and Method Call ### Endpoint N/A (Client-side class) ### Parameters #### Path Parameters None #### Query Parameters None #### Constructor Parameters - **partialOptions** (Partial) - Optional - Configuration options to initialize the StateMachine. #### `StateMachine.consume(text: string)` Parameters - **text** (string) - Required - The chunk of text to process. ### Request Example ```javascript // commonjs const StateMachine = require("striptags").StateMachine; const instance = new StateMachine(); console.log(instance.consume("some text with and more text")); ``` ### Response #### Success Response (string) - The `consume` method returns the processed chunk of text. #### Response Example ``` some text with and more text ``` ``` -------------------------------- ### StateMachine Class API Source: https://context7.com/ericnorris/striptags/llms.txt The StateMachine class provides stateful tag stripping for streaming or chunked text processing. It maintains parsing state between calls to `consume()`, making it ideal for large documents or real-time data streams. ```APIDOC ## StateMachine Class ### Description Provides stateful tag stripping for streaming or chunked text processing. Maintains parsing state between calls to `consume()`, ideal for large documents or real-time data streams. ### Method `new StateMachine(options?: StriptagsOptions)` `consume(text: string): string` ### Parameters #### Constructor Parameters - **options** (object) - Optional - Configuration options for tag stripping (same as `striptags` function options). #### `consume` Method Parameters - **text** (string) - Required - The chunk of text to process. ### Request Example ```typescript import { StateMachine } from "striptags"; const machine = new StateMachine({ allowedTags: new Set(["b"]) }); const chunk1 = machine.consume("This is "); const chunk2 = machine.consume("bold text."); console.log(chunk1 + chunk2); // Output: This is bold text. ``` ### Response #### Success Response (200) - **consumedText** (string) - The processed text chunk, with tags stripped according to the machine's configuration. ``` -------------------------------- ### Strip HTML tags using striptags function Source: https://context7.com/ericnorris/striptags/llms.txt Demonstrates how to use the main striptags function to remove HTML tags from a string. It covers basic usage, selective tag filtering using allowlists and blocklists, and custom tag replacement text. ```typescript import { striptags } from "striptags"; const html = `lorem ipsum dolor sit amet`; const plainText = striptags(html); console.log(plainText); const withStrong = striptags(html, { allowedTags: new Set(["strong"]) }); console.log(withStrong); const withoutStrong = striptags(html, { disallowedTags: new Set(["strong"]), allowedTags: new Set(["a", "em"]) }); console.log(withoutStrong); const withEmoji = striptags(html, { tagReplacementText: "🍩" }); console.log(withEmoji); const complexHtml = `

Hello World

`; const stripped = striptags(complexHtml); console.log(stripped); ``` -------------------------------- ### Advanced striptags StateMachine usage Source: https://github.com/ericnorris/striptags/blob/main/README.md Illustrates the advanced usage of the `StateMachine` class for processing text streams. This class maintains state across multiple calls to `consume()`, making it suitable for handling data incrementally. ```typescript class StateMachine { constructor(partialOptions?: Partial); consume(text: string): string; } ``` ```javascript // commonjs const StateMachine = require("striptags").StateMachine; // alternatively, as an es6 import // import { StateMachine } from "striptags"; const instance = new StateMachine(); console.log(instance.consume("some text with and more text")); ``` -------------------------------- ### striptags Function API Source: https://github.com/ericnorris/striptags/blob/main/README.md The main striptags function for stripping HTML tags from a string. It accepts the text to process and optional configuration options. ```APIDOC ## striptags Function ### Description Strips HTML tags from a given string. This is the primary function for simple tag removal. ### Method Function Call ### Endpoint N/A (Client-side function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **text** (string) - Required - The HTML string to process. - **options** (Partial) - Optional - Configuration options to customize tag stripping behavior. ### Request Example ```javascript // commonjs const striptags = require("striptags").striptags; var html = `lorem ipsum dolor sit amet`; console.log(striptags(html)); console.log(striptags(html, { allowedTags: new Set(["strong"]) })); console.log(striptags(html, { tagReplacementText: "🍩" })); ``` ### Response #### Success Response (string) - Returns the processed string with HTML tags removed or replaced according to the options. #### Response Example ``` lorem ipsum dolor sit amet lorem ipsum dolor sit amet 🍩lorem ipsum 🍩dolor🍩 🍩sit🍩 amet🍩 ``` ``` -------------------------------- ### striptags Function API Source: https://context7.com/ericnorris/striptags/llms.txt The main striptags function strips all HTML tags from a string and returns plain text. It offers options for allowed/disallowed tags, tag replacement, and XSS prevention. ```APIDOC ## striptags Function ### Description Strips all HTML tags from a string and returns plain text. By default, it removes all tags and encodes standalone `<` and `>` characters followed by whitespace to prevent XSS attacks. ### Method `striptags(html: string, options?: StriptagsOptions): string` ### Parameters #### Query Parameters - **html** (string) - Required - The HTML string to strip tags from. - **options** (object) - Optional - Configuration options for tag stripping. - **allowedTags** (Set) - Optional - A set of tags to keep. Takes precedence over `disallowedTags`. - **disallowedTags** (Set) - Optional - A set of tags to remove. - **tagReplacementText** (string) - Optional - Text to replace removed tags with. - **encodeTags** (boolean) - Optional - Whether to encode standalone angle brackets to prevent XSS. Defaults to true. ### Request Example ```typescript import { striptags } from "striptags"; const html = `Link Bold`; const plainText = striptags(html); console.log(plainText); // Output: Link Bold const withStrong = striptags(html, { allowedTags: new Set(["b"]) }); console.log(withStrong); // Output: Link Bold ``` ### Response #### Success Response (200) - **plainText** (string) - The HTML string with tags stripped. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.