Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
string-strip-html
Advanced tools
Strip HTML tags from strings. No parser, accepts mixed sources.
The string-strip-html npm package is used to remove HTML tags from strings. It is useful for sanitizing user input, cleaning up text for display, and ensuring that text data is free from HTML elements.
Basic HTML Stripping
This feature allows you to remove HTML tags from a string, leaving only the text content. The example demonstrates stripping HTML tags from a simple string.
const { stripHtml } = require('string-strip-html');
const result = stripHtml('<p>Hello <strong>World</strong>!</p>');
console.log(result.result); // 'Hello World!'
Handling Self-Closing Tags
This feature handles self-closing tags, ensuring that they are removed from the string. The example shows how an image tag is stripped from the input string.
const { stripHtml } = require('string-strip-html');
const result = stripHtml('<img src="image.jpg" alt="Image" />');
console.log(result.result); // ''
Removing Attributes
This feature removes HTML tags along with their attributes, leaving only the text content. The example demonstrates stripping an anchor tag with an href attribute.
const { stripHtml } = require('string-strip-html');
const result = stripHtml('<a href="https://example.com">Link</a>');
console.log(result.result); // 'Link'
The sanitize-html package allows for more granular control over HTML sanitization, including the ability to allow certain tags and attributes while removing others. It is more configurable compared to string-strip-html.
The html-to-text package converts HTML content to plain text while preserving some formatting, such as line breaks and lists. It is useful for converting HTML emails or web pages to readable text format.
The he package is used for encoding and decoding HTML entities. While it does not strip HTML tags, it is useful for handling HTML entities within text content.
Strip HTML tags from strings. No parser, accepts mixed sources.
This package is pure ESM. If you're not ready yet, install an older version of this program, 8.5.0 (npm i string-strip-html@8.5.0
).
npm i string-strip-html
import { strict as assert } from "assert";
import { stripHtml } from "string-strip-html";
assert.equal(
stripHtml("Some text <b>and</b> text.").result,
"Some text and text.",
);
// prevents accidental string concatenation
assert.equal(stripHtml("aaa<div>bbb</div>ccc").result, "aaa bbb ccc");
// tag pairs with content, upon request
assert.equal(
stripHtml("a <pre><code>void a;</code></pre> b", {
stripTogetherWithTheirContents: [
"script", // default
"style", // default
"xml", // default
"pre", // <-- custom-added
],
}).result,
"a b",
);
// detects raw, legit brackets:
assert.equal(stripHtml("a < b and c > d").result, "a < b and c > d");
Please visit codsen.com for a full description of the API. Also, try the GUI playground.
To report bugs or request features or assistance, raise an issue on GitHub.
MIT License.
Copyright © 2010-2024 Roy Revelt and other contributors.
FAQs
Strip HTML tags from strings. No parser, accepts mixed sources.
The npm package string-strip-html receives a total of 202,704 weekly downloads. As such, string-strip-html popularity was classified as popular.
We found that string-strip-html demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.