Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

posthtml-parser

posthtml5mMIT0.12.0TypeScript support: included

Parse HTML/XML to PostHTMLTree

html, xml, parser, posthtml, posthtmltree

readme

posthtml-parser

npm version Build Status Coverage Status

Parse HTML/XML to PostHTML AST. More about PostHTML

Install

NPM install

$ npm install posthtml-parser

Usage

Input HTML

<a class="animals" href="#">
    <span class="animals__cat" style="background: url(cat.png)">Cat</span>
</a>
import { parser } from 'posthtml-parser'
import fs from 'fs'

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

console.log(parser(html)) // Logs a PostHTML AST

input HTML

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

Result PostHTMLTree

[{
    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 being 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. Any strings represent plain text content to be written to the output. Any objects represent HTML tags.

Tag objects generally look something 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. Finally, 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: '>'}] Description: Adds processing of custom directives. Note: The property name in custom directives can be String or RegExp type

xmlMode

Type: Boolean Default: false Description: 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 Description: If set to true, entities within the document will be decoded.

lowerCaseTags

Type: Boolean Default: false Description: If set to true, all tags will be lowercased. If xmlMode is disabled.

lowerCaseAttributeNames

Type: Boolean Default: false Description: If set to true, all attribute names will be lowercased. This has noticeable impact on speed.

recognizeCDATA

Type: Boolean Default: false Description: If set to true, CDATA sections will be recognized as text even if the xmlMode option is not enabled. NOTE: If xmlMode is set to true then CDATA sections will always be recognized as text.

recognizeSelfClosing

Type: Boolean Default: false Description: If set to true, self-closing tags will trigger the onclosetag event even if xmlMode is not set to true. NOTE: If xmlMode is set to true then self-closing tags will always be recognized.

sourceLocations

Type: Boolean Default: false Description: 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 Description: 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

License

MIT

changelog

0.12.0 (2023-10-25)

  • style: add prettier config (40020ea)
  • style: after lint (4a25b30)
  • style: format ts (18f3949)
  • style: format ts (b98a3e0)
  • style: xo to eslint & updat edep dev & update husky (016c1ec)
  • perf: drop support old nodejs (6284a1d)
  • build: remove clinton (1aca332)
  • build: remove clinton config (33961ca)
  • build: remove unuset pkg (6931aee)
  • test: migrate to vitest (b41b243)

0.11.0 (2022-02-25)

  • 0.11.0 (0db4129)
  • build: update deps (d1cdcc6)
  • test: add tests for recognizeNoValueAttribute option (a2a2aa3)
  • docs: add description for recognizeNoValueAttribute option (f9b165d)
  • feat: add recognizeNoValueAttribute option for issue #75 (b992f28), closes #75

0.10.2 (2022-01-17)

0.10.1 (2021-08-30)

0.10.0 (2021-07-28)

0.9.1 (2021-07-26)

  • 0.9.1 (a03017e)
  • Update readme.md (7081062)
  • test: dom nest parsing issues #76 (fdfe82e), closes #76
  • test: remove only (78b7fc7)
  • style: after prettier (ccbfd69)
  • style: after prettier (72f2092)
  • build: set object-curly-spacing always (47dceb6)
  • build(deps): bump handlebars from 4.7.6 to 4.7.7 (e6bc6cd)
  • build(deps): bump hosted-git-info from 2.8.8 to 2.8.9 (2137591)
  • build(deps): bump lodash from 4.17.20 to 4.17.21 (e9b8d04)
  • build(deps): bump normalize-url from 4.5.0 to 4.5.1 (9e0d1f9)
  • perf: type for replace (94ebb8d)

0.9.0 (2021-05-11)

  • 0.9.0 (d725e83)
  • Add description to docs (d61007b)
  • Move location tracker to a separate file and refactor into class (7d6f76b)
  • feat: add optional support for source locations (6faf50d)

0.8.5 (2021-05-06)

0.8.4 (2021-05-06)

0.8.3 (2021-05-06)

0.8.2 (2021-05-06)

0.8.1 (2021-05-05)

0.8.0 (2021-04-16)

  • 0.8.0 (9676edd)
  • ci: drop support for node 10 (found not to be working) (585d552)
  • build: add prepare script (1dc46fc)

0.7.2 (2021-04-12)

  • 0.7.2 (7beaee9)
  • fix: correctly parse text content after comments (818e2f5)
  • build(deps): bump y18n from 4.0.0 to 4.0.1 (6b52ac1)

0.7.1 (2021-02-15)

0.7.0 (2021-02-09)

  • 0.7.0 (5887ed4)
  • build: default build cjs (41b7b25)
  • build: not generate dts (662f8d9)
  • build: perf config for ts (a3b5bc4)
  • build: refactor builder/lint/test system (baa7bcc)
  • build(deps): bump ini from 1.3.5 to 1.3.8 (471a77f)
  • docs: default import description (c824343)
  • perf: export default declare (eb63940)
  • perf: target to node 10 (3f0a707)
  • style: after update xo (dc66abe)
  • test: rewrite to typscript (63a3415)
  • refactor: rewrite to typscript (b1450a8)

0.6.0 (2020-11-13)

0.5.3 (2020-11-13)

  • 0.5.3 (88475c2)
  • build: down grade xo is broken (9631e7d)
  • fix: downgrade htmlpareser2 to 3* version, issue #53 (7353f6b), closes #53

0.5.2 (2020-10-30)

  • 0.5.2 (c8641cd)
  • fix: converting html entity name, close #51 (4a8be73), closes #51
  • test: converting html entity name, issue #51 (6bd5975), closes #51

0.5.1 (2020-10-27)

  • 0.5.1 (b6b4a8b)
  • style: 2 space, close issue #33 (cdd7dbd), closes #33
  • style: after lint (23dd2a0)
  • build: change eslint to xo (5233a43)
  • build: change lint to eslint (f6eef50)
  • build: update bump version script (7b7cfbf)
  • build: update dep dev (8601e40)
  • perf: concate with prev content string (fe7ddb0)
  • perf: downgrade code for old node (b160b9d)
  • perf: migrate to parser v5 (c2349ad)
  • ci: change nyc to c8 (1638a07)
  • ci: forgot config for c8 (ba45ba6)
  • ci: migrate to github action from travis (05e3768)
  • test: contents are split with '<' in comment, issue #18 (cbeb319), closes #18
  • test: contents are split with '<' in comment, issue #45 (74169cd), closes #45
  • fix: contents are split with '<' in comment, close #18, close #45 (8e64082), closes #18 #45
  • refactor: migrate to es-next syntax (d89bf85)
  • docs: simple update (0c10d9d)

0.5.0 (2020-08-22)

  • 0.5.0 (be05a9f)
  • build: forgot conventional-changelog-cli (8ff437a)
  • build: update changelog (d0a07dc)
  • build: update dep dev (f8efe55)
  • build: update lint/build system (54adc49)
  • build(deps): bump handlebars from 4.0.11 to 4.5.3 (8773f14)
  • revert: incorrect changes (0601fbd)
  • style: lintstage lint update (43398d5)
  • ci: drop old node support (cf95d62)
  • ci: drop support old node (d415fb9)
  • fix: incorrect merge defaultOptions, close #47 (11ba7fb), closes #47
  • test: update for, #41 (3cfc5c6), closes #41

0.4.2 (2019-11-08)

0.4.1 (2018-02-16)

0.4.0 (2018-01-23)

  • build RegEx from RegEx.source (4bc2807)
  • fix code style (01f8cad)
  • lowercase and reassign 'name' var (6f046c8)
  • refactor directive parser (f7d296c)
  • split directive checking per condition (1b800f7)
  • v0.4.0 (3bad693)
  • directive: add support for regular expression custom directive. (9403660)

0.3.3 (2018-01-19)

0.3.2 (2018-01-18)

  • 0.3.1 (dd11797)
  • revert default value for option (8b9e4ed)
  • use htmlparser2/lib/Parser directly (24c5e4e)
  • v0.3.2 (21d117d)
  • test: fix test (cad515c)
  • fix: incoming options not merge with default options (8eaa928)

0.3.1 (2017-12-27)

  • fix: if directive in content (5e1650a)
  • fix: try fix for old node (3086a08)
  • fix(index): parse obj in attrs, close #22 (7e32484), closes #22
  • revert(index): collect attrs (7277faf)
  • style: fix lint error (6d1a2d5)
  • style(test): update title (17b0c04)
  • test: for custom process directive (db31f89)
  • test: for directives and tag (0b0f098)
  • test: update test for custom directives (25ba6d0)
  • test(test): add test for issue #11 (aa8d0e3), closes #11
  • test(test): for issue #22 (a44f4fe), closes #22
  • chore: bump (6abbc6d)
  • chore(index): update htmlparser2 (b3e4c2d)
  • chore(package): fix htmlparse2 version (67edc28)
  • perf: for loop directives (3d7ef5a)
  • perf: forgot export default directives (924cfca)
  • feat: parse directives according incoming options (7e8e624)
  • docs: desctiption for option directives (12c0db9)

0.2.1 (2017-02-14)

  • Bump version to 0.2.1 (00b41b0)
  • Disable lowerCaseAttributeNames option by default (2145089)

0.2.0 (2016-07-19)

0.1.3 (2016-03-04)

0.1.2 (2016-03-03)

0.1.1 (2015-11-02)

0.1.0 (2015-10-20)