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

posthtml-parser

v0.12.1

Published

Parse HTML/XML to PostHTMLTree

Downloads

4,242,955

Readme

Parse HTML/XML to PostHTML AST

Version Build License Coverage

Installation

npm install posthtml-parser

Usage

Input HTML:

<a class="animals" href="#">
  <span class="animals__cat" style="background: url(cat.png)">Cat</span>
</a>

Parse with posthtml-parser:

import fs from 'fs'
import { parser } from 'posthtml-parser'

const html = fs.readFileSync('path/to/input.html', 'utf-8')

console.log(parser(html))

Resulting PostHTML AST:

[
  {
    tag: 'a',
    attrs: {
      class: 'animals',
      href: '#'
    },
    content: [
      '\n    ',
      {
        tag: 'span',
        attrs: {
          class: 'animals__cat',
          style: 'background: url(cat.png)'
        },
        content: ['Cat']
      },
      '\n'
    ]
  }
]

PostHTML AST Format

Any parser used with PostHTML should return a standard PostHTML Abstract Syntax Tree (AST).

Fortunately, this is a very easy format to produce and understand. The AST is an array that can contain strings and objects. Strings represent plain text content, while objects represent HTML tags.

Tag objects generally look like this:

{
  tag: 'div',
  attrs: {
    class: 'foo'
  },
  content: ['hello world!']
}

Tag objects can contain three keys:

  • The tag key takes the name of the tag as the value. This can include custom tags.
  • The optional attrs key takes an object with key/value pairs representing the attributes of the html tag. A boolean attribute has an empty string as its value.
  • The optional content key takes an array as its value, which is a PostHTML AST. In this manner, the AST is a tree that should be walked recursively.

Options

directives

Type: Array
Default: [{name: '!doctype', start: '<', end: '>'}]

Adds processing of custom directives.

The property name in custom directives can be of String or RegExp type.

xmlMode

Type: Boolean
Default: false

Indicates whether special tags (<script> and <style>) should get special treatment and if "empty" tags (eg. <br>) can have children. If false, the content of special tags will be text only.

For feeds and other XML content (documents that don't consist of HTML), set this to true.

decodeEntities

Type: Boolean
Default: false

If set to true, entities within the document will be decoded.

lowerCaseTags

Type: Boolean
Default: false

If set to true, all tags will be lowercased. If xmlMode is disabled.

lowerCaseAttributeNames

Type: Boolean
Default: false

If set to true, all attribute names will be lowercased.

This has noticeable impact on speed.

recognizeCDATA

Type: Boolean
Default: false

If set to true, CDATA sections will be recognized as text even if the xmlMode option is not enabled.

If xmlMode is set to true, then CDATA sections will always be recognized as text.

recognizeSelfClosing

Type: Boolean
Default: false

If set to true, self-closing tags will trigger the onclosetag event even if xmlMode is not set to true.

If xmlMode is set to true, then self-closing tags will always be recognized.

sourceLocations

Type: Boolean
Default: false

If set to true, AST nodes will have a location property containing the start and end line and column position of the node.

recognizeNoValueAttribute

Type: Boolean
Default: false

If set to true, AST nodes will recognize attribute with no value and mark as true which will be correctly rendered by posthtml-render package.