PostHTML 是一个使用 JS 插件转换 HTML/XML 的工具。
PostHTML 是一个用于通过 JS 插件转换 HTML/XML 的工具。PostHTML 本身非常小巧,仅包含一个 HTML 解析器、一个 HTML 节点树 API 以及一个节点树字符串化器。
所有 HTML 转换均由插件完成。这些插件只是简单的纯 JS 函数,它们接收一个 HTML 节点树,对其进行转换,然后返回修改后的树。
有关 PostHTML 的更多详细信息,请参阅文档。
| 名称 | 描述 |
|---|---|
| posthtml-parser | 将 HTML/XML 解析为 PostHTMLTree |
| posthtml-render | 将 PostHTMLTree 渲染为 HTML/XML |
npm init posthtml同步
import posthtml from 'posthtml'
const html = `
<component>
<title>Super Title</title>
<text>Awesome Text</text>
</component>
`
const result = posthtml()
.use(require('posthtml-custom-elements')())
.process(html, { sync: true })
.html
console.log(result)
</div>:warning: 异步插件无法在同步模式下使用,并会抛出错误。建议尽可能使用 PostHTML 的异步模式。
异步
import posthtml from 'posthtml'
const html = `
<html>
<body>
<p class="wow">OMG</p>
</body>
</html>
`
posthtml(
[
require('posthtml-to-svg-tags')(),
require('posthtml-extend-attrs')({
attrsTree: {
'.wow' : {
id: 'wow_id',
fill: '#4A83B4',
'fill-rule': 'evenodd',
'font-family': 'Verdana'
}
}
})
])
.process(html/*, options */)
.then((result) => console.log(result.html))<svg xmlns="http://www.w3.org/2000/svg">
<text
class="wow"
id="wow_id"
fill="#4A83B4"
fill-rule="evenodd" font-family="Verdana">
OMG
</text>
</svg>指令
import posthtml from 'posthtml'
const php = `
<component>
<title><?php echo $title; ?></title>
<text><?php echo $article; ?></暂无开放 Issues,或尚未同步最近议题。