posthtml-render

Renders PostHTML Tree to HTML/XML

MIT 30 个版本
安装
npm install posthtml-render
yarn add posthtml-render
pnpm add posthtml-render
bun add posthtml-render
README

npm node tests coverage

PostHTML

PostHTML Render

Renders a PostHTML Tree to HTML/XML

Install

npm i -D posthtml-render

Usage

NodeJS

import { render } from ''posthtml-render;

const tree = [];

const node = {};

node.tag = 'ul';
node.attrs = { class: 'list' };
node.content = [
 'one',
 'two',
 'three'
].map((content) => ({ tag: 'li', content }));

tree.push(node);

const html = render(tree, options);

<ul class="list">
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

Options

Name Type Default Description
singleTags {Array<String|RegExp>} [] Specify custom single tags (self closing)
closingSingleTag {String} > Specify the single tag closing format
quoteAllAttributes {Boolean} true Put double quotes around all tags, even when not necessary.
replaceQuote {Boolean} true Replaces quotes in attribute values with &quote;.
quoteStyle {0 or 1 or 2} 2 Specify the style of quote arround the attribute values

singleTags

Specify custom single tags (self closing)

{String}

const render = require('posthtml-render')

const tree = [ { tag: 'name' } ]
const options = { singleTags: [ 'name' ] }

const html = render(tree, options)

result.html

<name>

{RegExp}

const render = require('posthtml-render')

const tree = [ { tag: '%=title%' } ]
const options = { singleTags: [ /^%.*%$/ ] }

const html = render(tree, options)

result.html

<%=title%>

closingSingleTag

Specify the single tag closing format

Formats

const render = require('posthtml-render')

const tree = [ { tag: 'img' } ]
'tag'
const html = render(tree, { closingSingleTag: 'tag' })
<custom></custom>
'slash'
const html = render(tree, { closingSingleTag: 'slash' })
<custom />
'default' (Default)
const html = render(tree)
<img>
'closeAs'
const tree = [ {
  tag: 'custom',
  closeAs: 'default' // Available types: `tag` | `slash` | `default`
} ]
const html = render(tree, { closingSingleTag: 'closeAs' })
<custom>

quoteAllAttributes

Specify if all attributes should be quoted.

true (Default)
<i src="index.js"></i>
false
<i src=index.js></i>

replaceQuote

Replaces quotes in attribute values with &quote;.

true (Default)
<img src="<?php echo $foo[&quote;bar&quote;] ?>">
false
<img src="<?php echo $foo["bar"] ?>">

quoteStyle

2 (Default)

Attribute values are wrapped in double quotes:

<img src="https://example.com/example.png" onload="testFunc("test")">
1

Attribute values are wrapped in single quote:

<img src='https://example.com/example.png' onload='testFunc("test")'>
0

Quote style is based on attribute values (an alternative for replaceQuote option):

<img src="https://example.com/example.png" onload='testFunc("test")'>
版本列表
3.0.0 2021-07-27
2.0.6 2021-06-11
2.0.5 2021-06-10
2.0.4 2021-06-08
2.0.3 2021-06-04
2.0.2 2021-06-03
2.0.1 2021-05-24
2.0.0 2021-05-07
1.4.0 2020-11-18
1.3.1 2021-03-12
1.3.0 2020-11-12
1.2.3 2020-07-28
1.2.2 2020-04-16
1.2.1 2020-04-14
1.2.0 2020-02-25
1.1.5 2019-05-06
1.1.4 2018-05-11
1.1.3 2018-04-04
1.1.2 2018-03-20
1.1.1 2018-03-02
1.1.0 2018-01-18
1.0.7 2018-01-18
1.0.6 2016-02-29
1.0.5 2015-12-18
1.0.4 2015-11-29
1.0.3 2015-10-25
1.0.2 2015-10-21
1.0.1 2015-10-20
1.0.0 2015-10-19
0.0.0 2015-10-15