Skip to content

Commit b07c601

Browse files
authored
Merge pull request #103 from posthtml/0.12.1
0.12.1
2 parents af04aca + 965792d commit b07c601

File tree

2 files changed

+112
-68
lines changed

2 files changed

+112
-68
lines changed

.github/workflows/nodejs.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
name: Actions Status
1+
name: build
2+
23
on:
34
pull_request:
45
types: [opened, synchronize]
@@ -20,10 +21,10 @@ jobs:
2021

2122
steps:
2223
- name: Clone repository
23-
uses: actions/checkout@v2
24+
uses: actions/checkout@v4
2425

2526
- name: Set Node.js version
26-
uses: actions/setup-node@v1
27+
uses: actions/setup-node@v4
2728
with:
2829
node-version: ${{ matrix.node }}
2930

readme.md

Lines changed: 108 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,173 @@
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-
6-
Parse HTML/XML to [PostHTML AST](https://github.com/posthtml/posthtml-parser#posthtml-ast-format).
7-
More about [PostHTML](https://github.com/posthtml/posthtml#readme)
8-
9-
## Install
10-
11-
[NPM](http://npmjs.com) install
12-
```
13-
$ npm install posthtml-parser
1+
<div align="center">
2+
<img width="150" height="150" alt="PostHTML" src="https://posthtml.github.io/posthtml/logo.svg">
3+
<h1>PostHTML Parser</h1>
4+
5+
Parse HTML/XML to [PostHTML AST](https://github.com/posthtml/posthtml-parser#posthtml-ast-format)
6+
7+
[![Version][npm-version-shield]][npm]
8+
[![Build][github-ci-shield]][github-ci]
9+
[![License][license-shield]][license]
10+
[![Coverage][coverage-shield]][coverage]
11+
</div>
12+
13+
## Installation
14+
15+
```sh
16+
npm install posthtml-parser
1417
```
1518

1619
## Usage
1720

18-
#### Input HTML
21+
Input HTML:
22+
1923
```html
2024
<a class="animals" href="#">
21-
<span class="animals__cat" style="background: url(cat.png)">Cat</span>
25+
<span class="animals__cat" style="background: url(cat.png)">Cat</span>
2226
</a>
2327
```
28+
29+
Parse with `posthtml-parser`:
30+
2431
```js
25-
import { parser } from 'posthtml-parser'
2632
import fs from 'fs'
33+
import { parser } from 'posthtml-parser'
2734

2835
const html = fs.readFileSync('path/to/input.html', 'utf-8')
2936

30-
console.log(parser(html)) // Logs a PostHTML AST
37+
console.log(parser(html))
3138
```
3239

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-
```
40+
Resulting PostHTML AST:
3941

40-
#### Result PostHTMLTree
4142
```js
42-
[{
43+
[
44+
{
4345
tag: 'a',
4446
attrs: {
45-
class: 'animals',
46-
href: '#'
47+
class: 'animals',
48+
href: '#'
4749
},
4850
content: [
49-
'\n ',
50-
{
51-
tag: 'span',
52-
attrs: {
53-
class: 'animals__cat',
54-
style: 'background: url(cat.png)'
55-
},
56-
content: ['Cat']
51+
'\n ',
52+
{
53+
tag: 'span',
54+
attrs: {
55+
class: 'animals__cat',
56+
style: 'background: url(cat.png)'
5757
},
58-
'\n'
58+
content: ['Cat']
59+
},
60+
'\n'
5961
]
60-
}]
62+
}
63+
]
6164
```
6265

6366
## PostHTML AST Format
6467

65-
Any 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.
68+
Any parser used with PostHTML should return a standard PostHTML [Abstract Syntax Tree](https://www.wikiwand.com/en/Abstract_syntax_tree) (AST).
6669

67-
Tag objects generally look something like this:
70+
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.
71+
72+
Tag objects generally look like this:
6873

6974
```js
7075
{
71-
tag: 'div',
72-
attrs: {
73-
class: 'foo'
74-
},
75-
content: ['hello world!']
76+
tag: 'div',
77+
attrs: {
78+
class: 'foo'
79+
},
80+
content: ['hello world!']
7681
}
7782
```
7883

79-
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.
84+
Tag objects can contain three keys:
85+
86+
- The `tag` key takes the name of the tag as the value. This can include custom tags.
87+
- 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.
88+
- 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.
8089

8190
## Options
8291

8392
### `directives`
84-
Type: `Array`
93+
94+
Type: `Array`\
8595
Default: `[{name: '!doctype', start: '<', end: '>'}]`
86-
Description: *Adds processing of custom directives. Note: The property ```name``` in custom directives can be ```String``` or ```RegExp``` type*
96+
97+
Adds processing of custom directives.
98+
99+
The property ```name``` in custom directives can be of `String` or `RegExp` type.
87100

88101
### `xmlMode`
89-
Type: `Boolean`
102+
103+
Type: `Boolean`\
90104
Default: `false`
91-
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.*
105+
106+
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.
107+
108+
For feeds and other XML content (documents that don't consist of HTML), set this to `true`.
92109

93110
### `decodeEntities`
94-
Type: `Boolean`
111+
112+
Type: `Boolean`\
95113
Default: `false`
96-
Description: *If set to true, entities within the document will be decoded.*
114+
115+
If set to `true`, entities within the document will be decoded.
97116

98117
### `lowerCaseTags`
99-
Type: `Boolean`
118+
119+
Type: `Boolean`\
100120
Default: `false`
101-
Description: *If set to true, all tags will be lowercased. If `xmlMode` is disabled.*
121+
122+
If set to `true`, all tags will be lowercased. If `xmlMode` is disabled.
102123

103124
### `lowerCaseAttributeNames`
104-
Type: `Boolean`
125+
126+
Type: `Boolean`\
105127
Default: `false`
106-
Description: *If set to true, all attribute names will be lowercased. This has noticeable impact on speed.*
128+
129+
If set to `true`, all attribute names will be lowercased.
130+
131+
**This has noticeable impact on speed.**
107132

108133
### `recognizeCDATA`
109-
Type: `Boolean`
134+
135+
Type: `Boolean`\
110136
Default: `false`
111-
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.*
137+
138+
If set to `true`, CDATA sections will be recognized as text even if the `xmlMode` option is not enabled.
139+
140+
If `xmlMode` is set to `true`, then CDATA sections will always be recognized as text.
112141

113142
### `recognizeSelfClosing`
114-
Type: `Boolean`
143+
144+
Type: `Boolean`\
115145
Default: `false`
116-
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.*
146+
147+
If set to `true`, self-closing tags will trigger the `onclosetag` event even if `xmlMode` is not set to `true`.
148+
149+
If `xmlMode` is set to `true`, then self-closing tags will always be recognized.
117150

118151
### `sourceLocations`
119-
Type: `Boolean`
152+
153+
Type: `Boolean`\
120154
Default: `false`
121-
Description: *If set to true, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.*
155+
156+
If set to `true`, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.
122157

123158
### `recognizeNoValueAttribute`
124-
Type: `Boolean`
159+
160+
Type: `Boolean`\
125161
Default: `false`
126-
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*
127162

128-
## License
163+
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.
164+
129165

130-
[MIT](LICENSE)
166+
[npm]: https://www.npmjs.com/package/posthtml-parser
167+
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-parser.svg
168+
[github-ci]: https://github.com/posthtml/posthtml-parser/actions
169+
[github-ci-shield]: https://github.com/posthtml/posthtml-parser/actions/workflows/nodejs.yml/badge.svg
170+
[license]: ./LICENSE
171+
[license-shield]: https://img.shields.io/npm/l/posthtml-parser.svg
172+
[coverage]: https://coveralls.io/r/posthtml/posthtml-parser?branch=master
173+
[coverage-shield]: https://coveralls.io/repos/posthtml/posthtml-parser/badge.svg?branch=master

0 commit comments

Comments
 (0)