posthtml-parser
Parse HTML/XML to PostHTMLTree
Last updated a month ago by voischev .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install posthtml-parser 
SYNC missed versions from official npm registry.

PostHTML Parser

Parse HTML/XML into a PostHTML AST using htmlparser2.

Installation

Requires Node.js ^20.19.0 || >=22.12.0. These versions support synchronous CommonJS loading of the ESM-only htmlparser2 backend. Both require() and native ESM imports expose the synchronous parser API.

TypeScript consumers need TypeScript 5.3 or later.

npm install posthtml-parser

Usage

import { parser } from "posthtml-parser";

const tree = parser('<a class="link">Hello</a>');
console.log(tree);

Result:

[
  {
    tag: "a",
    attrs: {
      class: "link"
    },
    content: ["Hello"]
  }
]

CommonJS is supported too:

const { parser } = require("posthtml-parser");

PostHTML AST Format

A tree is an array of strings and element objects. Elements have a tag and optional attrs and content; content is another tree. Comments and directives remain strings.

Valueless attributes are empty strings by default, or true with recognizeNoValueAttribute. Enabling sourceLocations adds a location with 1-based start/end line and column positions.

Options

Pass options as the second argument to parser(html, options):

Option Default Description
directives [] Extra directives with name (string or RegExp), start, and end. Doctypes are always included.
xmlMode false Parse XML, including CDATA and self-closing tags.
decodeEntities false Decode character entities, including text inside textarea.
lowerCaseTags false Lowercase element names, including SVG names.
lowerCaseAttributeNames false Lowercase attribute names.
recognizeCDATA xmlMode Recognize CDATA in HTML mode; XML always recognizes it.
recognizeSelfClosing xmlMode Recognize <tag /> as a self-closing tag.
sourceLocations false Add source positions to element nodes.
recognizeNoValueAttribute false Represent valueless attributes as true instead of empty strings.

For example, preserve a PHP directive:

parser('<?php echo "Hello"; ?>', {
  directives: [{ name: "?php", start: "<", end: ">" }]
});

Contributing

See contributing and release instructions for development commands, commit guidelines, and publishing.

Maintainer: Ivan Voischev.

License

MIT.

Current Tags

  • 0.13.0                                ...           latest (a month ago)

36 Versions

  • 0.13.0                                ...           a month ago
  • 0.12.1                                ...           2 years ago
  • 0.12.0                                ...           3 years ago
  • 0.11.0                                ...           5 years ago
  • 0.10.2                                ...           5 years ago
  • 0.10.1                                ...           5 years ago
  • 0.10.0                                ...           5 years ago
  • 0.9.1                                ...           5 years ago
  • 0.9.0                                ...           6 years ago
  • 0.8.5                                ...           6 years ago
  • 0.8.4                                ...           6 years ago
  • 0.8.3                                ...           6 years ago
  • 0.8.2                                ...           6 years ago
  • 0.8.1                                ...           6 years ago
  • 0.8.0                                ...           6 years ago
  • 0.7.2                                ...           6 years ago
  • 0.7.1                                ...           6 years ago
  • 0.7.0                                ...           6 years ago
  • 0.6.0                                ...           6 years ago
  • 0.5.3                                ...           6 years ago
  • 0.5.2                                ...           6 years ago
  • 0.5.1                                ...           6 years ago
  • 0.5.0                                ...           6 years ago
  • 0.4.2                                ...           7 years ago
  • 0.4.1                                ...           9 years ago
  • 0.4.0                                ...           9 years ago
  • 0.3.3                                ...           9 years ago
  • 0.3.2                                ...           9 years ago
  • 0.3.1                                ...           9 years ago
  • 0.3.0                                ...           9 years ago
  • 0.2.1                                ...           10 years ago
  • 0.2.0                                ...           10 years ago
  • 0.1.3                                ...           11 years ago
  • 0.1.2                                ...           11 years ago
  • 0.1.1                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
Downloads
Total 66
Today 0
This Week 0
This Month 33
Last Day 0
Last Week 3
Last Month 7
Dependencies (1)
Dev Dependencies (7)

© 2010 - cnpmjs.org x YWFE | Home | YWFE