@igorskyflyer/strip-html-headings
v1.1.0
Published
🍛 Lightweight utility to strip HTML heading tags (h1-h6). Remove headings completely or extract their text content while cleaning up markup. Perfect for transforming HTML, parsing content, or preparing plain text output. 🍤
Downloads
3
Maintainers
Readme
📃 Table of Contents
🤖 Features
- 🧹 Strips all HTML headings h1-h6 completely
- ✂️ Removes only heading tags while keeping text
- 🧪 Safe on undefined null non-string inputs
- 📏 Handles multiple headings in one string
- 🪞 Preserves non-heading tags and content
- 🧼 Trims and normalizes whitespace
- 🎭 Cleans nested tags inside headings
- 📚 Supports multi-line formatted headings
- 🚫 Never throws always returns a string
🕵🏼 Usage
Install it by executing any of the following, depending on your preferred package manager:
pnpm add @igorskyflyer/strip-html-headingsyarn add @igorskyflyer/strip-html-headingsnpm i @igorskyflyer/strip-html-headings🤹🏼 API
function stripHeadings(value: string): stringStrips HTML headings completely.
value: string - the HTML string to process.
Returns the processed string.
function stripHeadingsCode(value: string): stringStrips only the HTML code while keeping the heading text.
value: string - the HTML string to process.
Returns the processed string.
🗒️ Examples
import { stripHeadings, stripHeadingsCode } from '@igorskyflyer/strip-html-headings'
// 🧹 strip all headings completely
console.log(stripHeadings('<h1>Hello</h1><p>World</p>'))
// '<p>World</p>'
// ✂️ keep heading text, remove only tags
console.log(stripHeadingsCode('<h2 class="title">Hello</h2>'))
// 'Hello'
// 📚 multiple headings handled
console.log(stripHeadingsCode('<h1>A</h1><h2>B</h2><h3>C</h3>'))
// 'ABC'
// 🪞 nested tags inside headings cleaned
console.log(stripHeadingsCode('<h1><em>Bold</em> Title</h1>'))
// 'Bold Title'
// 🧼 whitespace trimmed
console.log(stripHeadingsCode(' <h3> Trimmed </h3> '))
// 'Trimmed'
// 🚫 safe on bad input
// @ts-expect-error
console.log(stripHeadings()) // ''
console.log(stripHeadingsCode(null as any)) // ''📝 Changelog
📑 Read about the latest changes in the CHANGELOG.
🪪 License
Licensed under the MIT license.
💖 Support
🧬 Related
🔦 Provides frequently used types for your TypeScript projects. 🦄
🐯 A utility that lets you manipulate HTML elements, their attributes and innerHTML as strings, on the go and then render the modified HTML. Very useful in SSG projects. 💤
🎨 Provides common Color-related TypeScript types. 🌈
✒ DúöScríbî allows you to convert letters with diacritics to regular letters. 🤓
🏃♂️ Fast and simple Map and RegExp based HTML entities encoder. 🍁
👨🏻💻 Author
Created by Igor Dimitrijević (@igorskyflyer).
