stophtml
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

stophtml

A utility for Node.js (0.32 kB) and the browser (0.43 kB) that extracts plain text from an HTML string while ignoring HTML tags. It's useful for Natural Language Processing (NLP) tasks that require only the textual content of HTML documents.

Install

npm install stophtml

Or yarn:

yarn add stophtml

Alternatively, you can also include this module directly in your HTML file from CDN:

UMD: https://cdn.jsdelivr.net/npm/stophtml/dist/index.umd.js
ESM: https://cdn.jsdelivr.net/npm/stophtml/+esm
CJS: https://cdn.jsdelivr.net/npm/stophtml/dist/index.cjs

Usage

import stophtml from 'stophtml'

const input = '<p>This is <b>bold</b> and <i>italic</i>.</p>'
const segments = stophtml(input)

console.log(segments)

API

stophtml(input: string): string[]

Tokenizes an HTML string, extracting plain text while ignoring HTML tags.

  • input: The input HTML string to tokenize.

Returns an array of plain text segments extracted from the HTML string.

Related

  • boox – Performing full-text search across multiple documents by combining TF-IDF score with inverted index weight.
  • stopmarkdown – Extracts plain text from an Markdown string.
  • nomark – Transforms hypertext strings (e.g., HTML, Markdown) into plain text for natural language processing (NLP) normalization.
  • stopword – Allows you to strip stopwords from an input text (supports a ton of languages).

Benchmark

✓ test/index.bench.ts (2) 1305ms
     name                 hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · stophtml     136,571.33  0.0064  0.3648  0.0073  0.0069  0.0241  0.0263  0.1222  ±0.70%    68286   fastest
   · htmlparser2   68,310.52  0.0131  2.0111  0.0146  0.0138  0.0348  0.0458  0.0769  ±0.96%    34156


 BENCH  Summary

  stophtml - test/index.bench.ts >
    2.00x faster than htmlparser2
See benchmark code
import { bench } from 'vitest'
import { Parser } from 'htmlparser2'
import stophtml from 'stophtml'

const html = getHtml()

bench('stophtml', () => {
  stophtml(html)
})

bench('htmlparser2', () => {
  htmlparser2Parser(html)
})

function htmlparser2Parser(text: string) {
  const res: string[] = []

  const parser = new Parser({
    ontext(data) {
      res.push(data)
    }
  })

  parser.write(text)
  parser.end()

  return res.join(' ')
}

function getHtml() {
  return `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Template</title>
</head>
<body>
    <h1>Welcome to my HTML Template</h1>
    <p>This is a paragraph within the HTML template.</p>
    <ul>
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
    </ul>
    <img src="https://example.com/image.jpg" alt="Example Image">
    <a href="https://example.com">Visit our website</a>
</body>
</html>
`
}

Contributing

We 💛  issues.

When committing, please conform to the semantic-release commit standards. Please install commitizen and the adapter globally, if you have not already.

npm i -g commitizen cz-conventional-changelog

Now you can use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

git add . && git cz

License

GitHub

A project by Stilearning © 2024.

Package Sidebar

Install

npm i stophtml

Weekly Downloads

9

Version

1.0.2

License

MIT

Unpacked Size

7.82 kB

Total Files

7

Last publish

Collaborators

  • bent10