https://github.com/dogma-io/dogma-html-parser
HTML parser and compiler
https://github.com/dogma-io/dogma-html-parser
ast compile compiler html html-parser parse parser
Last synced: 3 days ago
JSON representation
HTML parser and compiler
- Host: GitHub
- URL: https://github.com/dogma-io/dogma-html-parser
- Owner: dogma-io
- Created: 2017-09-11T14:13:55.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-16T18:30:19.000Z (almost 9 years ago)
- Last Synced: 2025-09-05T09:02:27.566Z (10 months ago)
- Topics: ast, compile, compiler, html, html-parser, parse, parser
- Language: JavaScript
- Size: 121 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# dogma-html-parser
HTML parser and compiler.
## Installation
```bash
npm install dogma-html-parser
```
## Usage
### Parser
To parse an HTML string into an AST you can do the following:
```js
import {parse} from 'dogma-html-parser'
const html = '
Foo bar'
const ast = parse(html)
```
The above example will set the constant **ast** to the following AST:
```json
{
"attributes": {
"class": "test"
},
"children": [
{
"text": "Foo bar",
"type": "text"
}
],
"name": "div",
"type": "element"
}
```
It is worth noting the parser tries to be forgiving in terms of invalid
whitespace, and by default will strip out unnecessary whitespace that doesn't
actually get rendered by browsers. For example if you parse then compile the
following:
```html
< div > Test < /div >
```
You'll end up with:
```html
Test
```
### Compiler
The compiler simply takes an AST and converts it to an HTML string like so:
```js
import {compile} from 'dogma-html-parser'
const ast = {
attributes: {
'class': 'test',
},
children: [
{
text: 'Foo bar',
type: 'text',
},
],
name: 'div',
type: 'element',
}
const html = compile(ast)
```
The above example will set the constant **html** to the following HTML string:
```html
Foo bar
```