• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parse HTML/XML to PostHTMLTree

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

More Repositories

1

posthtml

PostHTML is a tool to transform HTML/XML with JS plugins
JavaScript
2,937
star
2

htmlnano

Modular HTML minifier, built on top of the PostHTML
JavaScript
237
star
3

posthtml-include

Include HTML Plugin
HTML
188
star
4

posthtml-expressions

Use variables, JS-like expressions, and even markup-powered logic in your HTML.
JavaScript
123
star
5

posthtml-minify-classnames

๐Ÿ“ฉ Rewrites classnames and ids in HTML and CSS to reduce file size.
JavaScript
84
star
6

posthtml-modules

Modules Plugin
JavaScript
84
star
7

posthtml-css-modules

Use CSS modules in HTML
JavaScript
54
star
8

posthtml-postcss

PostCSS Plugin
JavaScript
48
star
9

posthtml-extend

Template extending (Jade-like)
JavaScript
47
star
10

posthtml-render

Render PostHTMLTree to HTML/XML
TypeScript
44
star
11

posthtml-loader

PostHTML for Webpack
JavaScript
42
star
12

posthtml-web-component

Server Side Web Component Render.
JavaScript
35
star
13

posthtml-cli

CLI for PostHTML
JavaScript
29
star
14

posthtml-img-autosize

Auto setting the width and height of <img>
JavaScript
28
star
15

posthtml-beautify

A posthtml plugin to beautify you html files. Online service -
HTML
28
star
16

sugarml

SugarML Parser
JavaScript
24
star
17

posthtml-content

Apply functions to tags through custom attributes
JavaScript
23
star
18

posthtml-inline-css

CSS Inliner
JavaScript
22
star
19

posthtml-mso

Makes writing Outlook conditionals in HTML emails easy.
JavaScript
21
star
20

posthtml-postcss-modules

PostCSS CSS Modules Plugin
JavaScript
20
star
21

posthtml-hint

HTML HINT Plugin
JavaScript
19
star
22

gulp-posthtml

PostHTML for Gulp
JavaScript
18
star
23

posthtml-pug

Pug Parser
JavaScript
17
star
24

posthtml-retext

PostHTML Retext plugin
JavaScript
17
star
25

awesome-posthtml

A curated list of awesome things related to PostHTML
16
star
26

posthtml-custom-elements

Custom Elements Plugin
JavaScript
16
star
27

posthtml-plugins

PostHTML Plugins Catalog
HTML
15
star
28

posthtml.org

Website for posthtml.
HTML
15
star
29

posthtml-components

A PostHTML plugin for creating components with HTML-friendly syntax inspired by Laravel Blade. Slots, stack/push, props, custom tag and much more.
HTML
15
star
30

posthtml-w3c

W3C HTML Validation for PostHTML
JavaScript
15
star
31

posthtml-textr

Textr for PostHTML
JavaScript
13
star
32

posthtml-attrs-parser

PostHTML helper that provides a better API to work with tag attributes.
JavaScript
13
star
33

posthtml-fetch

PostHTML plugin for fetching and displaying remote content.
JavaScript
12
star
34

posthtml-head-elements

Build HTML head elements from a JSON file
HTML
11
star
35

posthtml-match-helper

A helper to expand CSS selectors into PostHTML matcher objects
JavaScript
11
star
36

posthtml-style-to-file

Save DOM styles to CSS file
JavaScript
11
star
37

posthtml-prism

PostHTML plugin for code syntax highlighting with Prism.
JavaScript
11
star
38

posthtml-external-link

Add "rel='external noopenner nofollow'" and "target=_blank" to all external links
TypeScript
11
star
39

posthtml-cache

Add a nanoid to links in you tags
JavaScript
11
star
40

express-posthtml

PostHTML for Express
JavaScript
10
star
41

posthtml-highlight

๐ŸŽจ Syntax Highlighting for PostHTML
TypeScript
9
star
42

posthtml-insert-at

PostHTML plugin to append or prepend HTML to a selector
TypeScript
9
star
43

posthtml-jsxhtml-freemarker

Experimental plugin to convert normalised jsx into java freemarker template
JavaScript
9
star
44

posthtml-tidy

Tidy Plugin
JavaScript
9
star
45

posthtml-jsx

JSX Renderer
JavaScript
8
star
46

posthtml-markdownit

A PostHTML plugin to transform Markdown using markdown-it
JavaScript
8
star
47

posthtml-safe-class-names

Replace escaped characters in CSS selectors and HTML class names.
JavaScript
7
star
48

koa-posthtml

PostHTML for Koa
JavaScript
7
star
49

posthtml-hash

PostHTML plugin for hashing file names
TypeScript
7
star
50

posthtml-spaceless

Spaceless tag to remove whitespace between HTML tags
JavaScript
7
star
51

metalsmith-posthtml

PostHTML for Metalsmith
JavaScript
6
star
52

posthtml-base-url

Prepend a string to source paths in your HTML.
JavaScript
6
star
53

posthtml-sugar-srcset

This shortens the description of the value of srcset.
JavaScript
6
star
54

posthtml-remove-tags

A posthtml plugin for removing tags
JavaScript
6
star
55

posthtml-each

Repeat your HTML-elements with attribute
JavaScript
6
star
56

posthtml-noopener

PostHTML plugin to add 'rel="noopener noreferrer"' to links that open in a new tab
TypeScript
5
star
57

posthtml-extra-attributes

Add new attributes to elements in your HTML
JavaScript
5
star
58

posthtml-mixins

๐Ÿ“ฎ Mixins allow you to create reusable blocks of code.
TypeScript
5
star
59

posthtml-webp

Add webp supporting in your html
JavaScript
5
star
60

rollup-plugin-posthtml

PostHTML plugin for Rollup
JavaScript
5
star
61

posthtml-urls

PostHTML plugin for transforming URLs.
JavaScript
5
star
62

electron-posthtml

PostHTML for Electron
JavaScript
4
star
63

grunt-posthtml

PostHMTL Grunt Plugin
HTML
4
star
64

posthtml-toc

๐Ÿ“‘Table of contents
JavaScript
4
star
65

posthtml-doctype

Doctype Plugin
JavaScript
3
star
66

posthtml-url-parameters

Add parameters to URLs with PostHTML.
JavaScript
3
star
67

posthtml-rtl

A flexible utility to convert HTML to RTL (right to left) and vice versa.
JavaScript
3
star
68

posthtml-reporter

Reporter Plugin
JavaScript
3
star
69

posthtml-stream

Stream Wrapper for PostHTML
JavaScript
3
star
70

posthtml-replace

JavaScript
3
star
71

project-stub

HTML
3
star
72

posthtml-md2html

Render markdown inside html elements.
JavaScript
3
star
73

posthtml-load-plugins

Autoload Plugins for PostHTML
JavaScript
3
star
74

posthtml-nonce

A posthtml plugin create whitelist for specific inline scripts using a cryptographic nonce
JavaScript
3
star
75

posthtml-postcss-treeshaker

posthtml plugin to tree shake css styles using postcss
JavaScript
3
star
76

posthtml-postcss-merge-longhand

Merge longhand properties from inline CSS into shorthand with PostCSS.
JavaScript
3
star
77

posthtml-lint

PostHTML plugin to lint static markup
TypeScript
2
star
78

posthtml-include-md

Include markdown plugin for PostHTML
JavaScript
2
star
79

gulp-htmlnano

Minify HTML with htmlnano
JavaScript
2
star
80

posthtml-plugins-idea

2
star
81

posthtml-rename-attrs

Programmatically rename HTML attributes
JavaScript
2
star
82

posthtml-atomizer

Generate Atomic CSS with Atomizer via PostHTML
JavaScript
2
star
83

posthtml-load-config

Autoload Config for PostHTML
JavaScript
2
star
84

posthtml-inline-favicon

PostHTML plugin to inline favicons
HTML
2
star
85

posthtml-plugin-ts-boilerplate

Create new PostHTML plugins on TypeScript
TypeScript
1
star
86

posthtml-plugin-starter

A starter project for building PostHTML plugins.
JavaScript
1
star
87

posthtml-import

Import HTML Plugin
JavaScript
1
star
88

posthtml-noscript

PostHTML plugin to insert noscript content
TypeScript
1
star
89

posthtml-color-shorthand-hex-to-six-digit

Posthtml plugin convert shorthand hex color codes into full
JavaScript
1
star
90

posthtml-load-options

Autoload Options for PostHTML
JavaScript
1
star
91

posthtml-component

component PostHTML plugin
JavaScript
1
star
92

hapi-posthtml

PostHTML for Hapi
JavaScript
1
star
93

create-posthtml

Add PostHTML to your project
JavaScript
1
star
94

posthtml-i18n

Internalization with PostHTML
JavaScript
1
star
95

posthtml-class-to-css-module

A posthtml plugin for clone class to attribute css-module
JavaScript
1
star
96

esm

webpack HTML Module Helpers
JavaScript
1
star