Parse HTML/XML to PostHTMLTree
npm install @lancer/posthtml-parserParse HTML/XML to PostHTML AST.
More about PostHTML
NPM install
```
$ npm install posthtml-parser
#### Input HTML
`html`
Cat`js
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
`html`
Cat
#### Result PostHTMLTree
`js`
[{
tag: 'a',
attrs: {
class: 'animals',
href: '#'
},
content: [
'\n ',
{
tag: 'span',
attrs: {
class: 'animals__cat',
style: 'background: url(cat.png)'
},
content: ['Cat']
},
'\n'
]
}]
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:
`js`
{
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.
Default: [{name: '!doctype', start: '<', end: '>'}]
Description: Adds processing of custom directives. Note: The property `name` in custom directives can be `String` or `RegExp` type $3
Type: Boolean
Default: false
Description: Indicates whether special tags (