@igorskyflyer/strip-html
v2.1.0
Published
🥞 Removes HTML code from the given string. Can even extract text-only from the given an HTML string. ✨
Downloads
13
Maintainers
Readme
📃 Table of Contents
🤖 Features
- ✅ Strips HTML tags cleanly
- 🧹 Trims leading and trailing whitespace
- 📏 Collapses multiple spaces, tabs, newlines into one
- ⚙️ Provides safe defaults for predictable output
🕵🏼 Usage
Install it by executing any of the following, depending on your preferred package manager:
pnpm add @igorskyflyer/strip-htmlyarn add @igorskyflyer/strip-htmlnpm i @igorskyflyer/strip-html🤹🏼 API
stripHtml(html: string, args?: IOptions): string
Strips HTML tags completely.
html: string - The HTML string to process.
args: IOptions - The options to pass.
An object with the following properties:
interface IOptions {
trimWhitespace?: boolean
}trimWhitespace?: boolean - If true, trims and normalizes whitespace - internal runs of whitespace (spaces, tabs, newlines) collapse to a single space. Leading/trailing whitespace is always trimmed.
Returns the processed string.
stripHtmlCode(html: string, args?: IOptions): string
Strips only the HTML code while keeping the text content.
html: string - The HTML string to process.
args: IOptions - The options to pass.
An object with the following properties:
interface IOptions {
trimWhitespace?: boolean
}trimWhitespace?: boolean - If true, trims and normalizes whitespace - internal runs of whitespace (spaces, tabs, newlines) collapse to a single space. Leading/trailing whitespace is always trimmed.
Returns the processed string.
🗒️ Examples
example.ts
import { stripHtmlCode, stripHtml } from '@igorskyflyer/strip-html'
console.log(
stripHtmlCode(`<div class="container">
<h1>Title</h1>
<p>This is a <strong>paragraph</strong> with some <em>emphasis</em> and a <a href="#">link</a>.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>`))
/*
will print
Title
This is a paragraph with some emphasis and a link.
Item 1
Item 2
Item 3
*/
console.log(
stripHtml(`<div class="container">
<h1>Title</h1>
<p>This is a <strong>paragraph</strong> with some <em>emphasis</em> and a <a href="#">link</a>.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>`)) // will print an empty string, i.e. ''
console.log(stripHtml(`Hello world`)) // will print 'Hello world'
console.log(
stripHtmlCode(`<div class="container">
<h1>Title</h1>
<p>This is a <strong>paragraph</strong> with some <em>emphasis</em> and a <a href="#">link</a>.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>`, { trimWhitespace: true })) // will print 'Title This is a paragraph with some emphasis and a link. Item 1 Item 2 Item 3'📝 Changelog
📑 Read about the latest changes in the CHANGELOG.
🪪 License
Licensed under the MIT license.
💖 Support
🧬 Related
✒ DúöScríbî allows you to convert letters with diacritics to regular letters. 🤓
@igorskyflyer/magic-queryselector
🪄 A TypeScript-types patch for querySelector/querySelectorAll, make them return types you expect them to! 🔮
@igorskyflyer/astro-escaped-component
🏃🏻♂️➡️ An Astro component that holds only HTML-encoded content. 📜
🧵 Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. 🔍
@igorskyflyer/strip-html-headings
🍛 Strips HTML headings! 🍤
👨🏻💻 Author
Created by Igor Dimitrijević (@igorskyflyer).
