Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.12.1 #103

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Actions Status
name: build

on:
pull_request:
types: [opened, synchronize]
Expand All @@ -20,10 +21,10 @@ jobs:

steps:
- name: Clone repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set Node.js version
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

Expand Down
173 changes: 108 additions & 65 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,130 +1,173 @@
# posthtml-parser
[![npm version](https://badge.fury.io/js/posthtml-parser.svg)](http://badge.fury.io/js/posthtml-parser)
[![Build Status](https://travis-ci.org/posthtml/posthtml-parser.svg?branch=master)](https://travis-ci.org/posthtml/posthtml-parser?branch=master)
[![Coverage Status](https://coveralls.io/repos/posthtml/posthtml-parser/badge.svg?branch=master)](https://coveralls.io/r/posthtml/posthtml-parser?branch=master)

Parse HTML/XML to [PostHTML AST](https://github.com/posthtml/posthtml-parser#posthtml-ast-format).
More about [PostHTML](https://github.com/posthtml/posthtml#readme)

## Install

[NPM](http://npmjs.com) install
```
$ npm install posthtml-parser
<div align="center">
<img width="150" height="150" alt="PostHTML" src="https://posthtml.github.io/posthtml/logo.svg">
<h1>PostHTML Parser</h1>

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]
</div>

## Installation

```sh
npm install posthtml-parser
```

## Usage

#### Input HTML
Input HTML:

```html
<a class="animals" href="#">
<span class="animals__cat" style="background: url(cat.png)">Cat</span>
<span class="animals__cat" style="background: url(cat.png)">Cat</span>
</a>
```

Parse with `posthtml-parser`:

```js
import { parser } from 'posthtml-parser'
import fs from 'fs'
import { parser } from 'posthtml-parser'

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

console.log(parser(html)) // Logs a PostHTML AST
console.log(parser(html))
```

#### input HTML
```html
<a class="animals" href="#">
<span class="animals__cat" style="background: url(cat.png)">Cat</span>
</a>
```
Resulting PostHTML AST:

#### Result PostHTMLTree
```js
[{
[
{
tag: 'a',
attrs: {
class: 'animals',
href: '#'
class: 'animals',
href: '#'
},
content: [
'\n ',
{
tag: 'span',
attrs: {
class: 'animals__cat',
style: 'background: url(cat.png)'
},
content: ['Cat']
'\n ',
{
tag: 'span',
attrs: {
class: 'animals__cat',
style: 'background: url(cat.png)'
},
'\n'
content: ['Cat']
},
'\n'
]
}]
}
]
```

## PostHTML AST Format

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

Tag objects generally look something like this:
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: '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.
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`

Type: `Array`\
Default: `[{name: '!doctype', start: '<', end: '>'}]`
Description: *Adds processing of custom directives. Note: The property ```name``` in custom directives can be ```String``` or ```RegExp``` type*

Adds processing of custom directives.

The property ```name``` in custom directives can be of `String` or `RegExp` type.

### `xmlMode`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
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.*

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`.

### `decodeEntities`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
Description: *If set to true, entities within the document will be decoded.*

If set to `true`, entities within the document will be decoded.

### `lowerCaseTags`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
Description: *If set to true, all tags will be lowercased. If `xmlMode` is disabled.*

If set to `true`, all tags will be lowercased. If `xmlMode` is disabled.

### `lowerCaseAttributeNames`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
Description: *If set to true, all attribute names will be lowercased. This has noticeable impact on speed.*

If set to `true`, all attribute names will be lowercased.

**This has noticeable impact on speed.**

### `recognizeCDATA`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
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.*

If set to `true`, CDATA sections will be recognized as text even if the `xmlMode` option is not enabled.

If `xmlMode` is set to `true`, then CDATA sections will always be recognized as text.

### `recognizeSelfClosing`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
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.*

If set to `true`, self-closing tags will trigger the `onclosetag` event even if `xmlMode` is not set to `true`.

If `xmlMode` is set to `true`, then self-closing tags will always be recognized.

### `sourceLocations`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
Description: *If set to true, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.*

If set to `true`, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.

### `recognizeNoValueAttribute`
Type: `Boolean`

Type: `Boolean`\
Default: `false`
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*

## License
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.


[MIT](LICENSE)
[npm]: https://www.npmjs.com/package/posthtml-parser
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-parser.svg
[github-ci]: https://github.com/posthtml/posthtml-parser/actions
[github-ci-shield]: https://github.com/posthtml/posthtml-parser/actions/workflows/nodejs.yml/badge.svg
[license]: ./LICENSE
[license-shield]: https://img.shields.io/npm/l/posthtml-parser.svg
[coverage]: https://coveralls.io/r/posthtml/posthtml-parser?branch=master
[coverage-shield]: https://coveralls.io/repos/posthtml/posthtml-parser/badge.svg?branch=master
Loading