npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-parser-lite

v1.1.0

Published

A light weight html parser

Downloads

1,529

Readme

html-parser-lite Test CI

A light weight html parser and more.

NPM

API

parse(html: string, options?: object)=>Node|Node[]:

|property|type|desc|defaults| |--------|---------|-------|-------| | html | string | The string to parse | None | | options.wrapWithDocument | boolean | Whether force to create a document node as root wrapper. | false | | options.ignoreWhitespaceText | boolean | Whether create text node when all the chars are white space. | true | | options.scanner | HtmlScanner | Inner html scanner. Config it only when you want to implement custom complex logic. | inner HtmlScanner instance |

If options.ignoreWhitespaceText set to true, it will return a DocumentNode(as the root of the whole tree); otherwise, it will return an array of nodes.

Important Tips

The library's goal is not to behave the same as the browser, it just parses html string to node tree.

When you use default options (just run parse(html)),it will always return an array of nodes. And the white space between tags will be ignored. Take <p>s t a r t</p>↵ ↵<p> </p> for example:

  • ↵ ↵ between two paragraphs will be ignored, so only return two paragraph nodes.
  • The first paragraph <p>s t a r t</p> will keep all white space characters.
  • The second paragraph <p> </p> will ingore white space, so this p node has no text child node.

If you want to keep white space(which generates corresponding text nodes), set options.ignoreWhitespaceText=false.

Usage

const fs = require('fs')
const parse = require('html-parser-lite')
const html = fs.readFileSync('test/textures/simple.html').toString()

// html-parser will parse html to nodes array (default behavior).
const nodes = parse(html)
// JSON.stringify(nodes):
// [{"tagName":"doctype","nodeType":10,"publicId":"","systemId":"","name":"html"},{"tagName":"html","nodeType":1,"childNodes":[{"tagName":"head","nodeType":1,"childNodes":[{"tagName":"meta","nodeType":1,"childNodes":[],"attrs":{"charset":"utf-8"}},{"tagName":"title","nodeType":1,"childNodes":[{"tagName":"text","nodeType":3,"textContent":"hi"}],"attrs":{}}],"attrs":{}},{"tagName":"body","nodeType":1,"childNodes":[{"tagName":"h1","nodeType":1,"childNodes":[{"tagName":"text","nodeType":3,"textContent":"heading title"}],"attrs":{}}],"attrs":{}}],"attrs":{"class":"html-ok","lang":"zh-hans-cn"},"className":"html-ok"}]

License

MIT