From 10aa89e993a07b917c7376f4b9a000ff13b6d276 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Wed, 19 Jun 2024 13:17:19 +0300 Subject: [PATCH 1/2] ci: update github workflow --- .github/workflows/nodejs.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 4312dad..725cc1f 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -1,4 +1,5 @@ -name: Actions Status +name: build + on: pull_request: types: [opened, synchronize] @@ -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 }} From 965792d4244324e52c1cda1172d8f6ef43173bca Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Wed, 19 Jun 2024 13:19:36 +0300 Subject: [PATCH 2/2] docs: update readme.md --- readme.md | 173 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 108 insertions(+), 65 deletions(-) diff --git a/readme.md b/readme.md index 5098122..427f59b 100644 --- a/readme.md +++ b/readme.md @@ -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 +
+ PostHTML +

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 +Input HTML: + ```html - Cat + Cat ``` + +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 - - Cat - -``` +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 (`