UNPKG

4.15 kBMarkdownView Raw
1# posthtml-parser
2[![npm version](https://badge.fury.io/js/posthtml-parser.svg)](http://badge.fury.io/js/posthtml-parser)
3[![Build Status](https://travis-ci.org/posthtml/posthtml-parser.svg?branch=master)](https://travis-ci.org/posthtml/posthtml-parser?branch=master)
4[![Coverage Status](https://coveralls.io/repos/posthtml/posthtml-parser/badge.svg?branch=master)](https://coveralls.io/r/posthtml/posthtml-parser?branch=master)
5
6Parse HTML/XML to [PostHTML AST](https://github.com/posthtml/posthtml-parser#posthtml-ast-format).
7More about [PostHTML](https://github.com/posthtml/posthtml#readme)
8
9## Install
10
11[NPM](http://npmjs.com) install
12```
13$ npm install posthtml-parser
14```
15
16## Usage
17
18#### Input HTML
19```html
20<a class="animals" href="#">
21 <span class="animals__cat" style="background: url(cat.png)">Cat</span>
22</a>
23```
24```js
25import parser from 'posthtml-parser'
26import fs from 'fs'
27
28const html = fs.readFileSync('path/to/input.html', 'utf-8')
29
30console.log(parser(html)) // Logs a PostHTML AST
31```
32
33#### input HTML
34```html
35<a class="animals" href="#">
36 <span class="animals__cat" style="background: url(cat.png)">Cat</span>
37</a>
38```
39
40#### Result PostHTMLTree
41```js
42[{
43 tag: 'a',
44 attrs: {
45 class: 'animals',
46 href: '#'
47 },
48 content: [
49 '\n ',
50 {
51 tag: 'span',
52 attrs: {
53 class: 'animals__cat',
54 style: 'background: url(cat.png)'
55 },
56 content: ['Cat']
57 },
58 '\n'
59 ]
60}]
61```
62
63## PostHTML AST Format
64
65Any parser being used with PostHTML should return a standard PostHTML [Abstract Syntax Tree](https://www.wikiwand.com/en/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.
66
67Tag objects generally look something like this:
68
69```js
70{
71 tag: 'div',
72 attrs: {
73 class: 'foo'
74 },
75 content: ['hello world!']
76}
77```
78
79Tag 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.
80
81## Options
82
83### `directives`
84Type: `Array`
85Default: `[{name: '!doctype', start: '<', end: '>'}]`
86Description: *Adds processing of custom directives. Note: The property ```name``` in custom directives can be ```String``` or ```RegExp``` type*
87
88### `xmlMode`
89Type: `Boolean`
90Default: `false`
91Description: *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.*
92
93### `decodeEntities`
94Type: `Boolean`
95Default: `false`
96Description: *If set to true, entities within the document will be decoded.*
97
98### `lowerCaseTags`
99Type: `Boolean`
100Default: `false`
101Description: *If set to true, all tags will be lowercased. If `xmlMode` is disabled.*
102
103### `lowerCaseAttributeNames`
104Type: `Boolean`
105Default: `false`
106Description: *If set to true, all attribute names will be lowercased. This has noticeable impact on speed.*
107
108### `recognizeCDATA`
109Type: `Boolean`
110Default: `false`
111Description: *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.*
112
113### `recognizeSelfClosing`
114Type: `Boolean`
115Default: `false`
116Description: *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.*
117
118## License
119
120[MIT](LICENSE)