# posthtml-parser
**Repository Path**: mirrors_posthtml/posthtml-parser
## Basic Information
- **Project Name**: posthtml-parser
- **Description**: Parse HTML/XML to PostHTMLTree
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-09-25
- **Last Updated**: 2025-12-03
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
PostHTML Parser
Parse HTML/XML to [PostHTML AST](https://github.com/posthtml/posthtml-parser#posthtml-ast-format)
[![Version][npm-version-shield]][npm]
[![Build][github-ci-shield]][github-ci]
[![License][license-shield]][license]
[![Coverage][coverage-shield]][coverage]
## Installation
```sh
npm install posthtml-parser
```
## Usage
Input HTML:
```html
Cat
```
Parse with `posthtml-parser`:
```js
import fs from 'fs'
import { parser } from 'posthtml-parser'
const html = fs.readFileSync('path/to/input.html', 'utf-8')
console.log(parser(html))
```
Resulting PostHTML AST:
```js
[
{
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 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. Strings represent plain text content, while objects represent HTML tags.
Tag objects generally look 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.
- 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: '>'}]`
Adds processing of custom directives.
The property ```name``` in custom directives can be of `String` or `RegExp` type.
### `xmlMode`
Type: `Boolean`\
Default: `false`
Indicates whether special tags (`