posthtml/posthtml
소개
PostHTML은 JS 플러그인으로 HTML/XML을 변환하는 도구입니다
웹 및 앱 개발측정된 스타 증가
- in 24h
- 0
- in 7 days
- -2
- in 15 days
- -1
- in 1 month
- 0
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalPostHTML 
PostHTML is a tool for transforming HTML/XML with JS plugins. PostHTML itself is very small. It includes only a HTML parser, a HTML node tree API and a node tree stringifier.
All HTML transformations are made by plugins. And these plugins are just small plain JS functions, which receive a HTML node tree, transform it, and return a modified tree.
See the documentation for the processor API, tree format and plugin guides.
For usage questions, visit Discussions. Report bugs and propose features through Issues.
Dependencies
| Name | Description |
|---|---|
| posthtml-parser | Parser HTML/XML to PostHTMLTree |
| posthtml-render | Render PostHTMLTree to HTML/XML |
Install
Requires Node.js ^20.19.0 || >=22.12.0, matching the synchronous loading requirements of posthtml-parser and its htmlparser2 backend.
npm i -D posthtml
Usage
API
The examples below use ES modules. Save them as .mjs files or set "type": "module" in your project's package.json, and install the plugins used in each example.
Sync
import posthtml from 'posthtml'
const customElements = () => (tree) => {
tree.match({ tag: /^(component|title|text)$/ }, (node) => ({
tag: 'div',
attrs: { class: node.tag },
content: node.content
}))
}
const html = `
<component>
<title>Super Title</title>
<text>Awesome Text</text>
</component>
`
const result = posthtml()
.use(customElements())
.process(html, { sync: true })
.html
console.log(result)
<div class="component">
<div class="title">Super Title</div>
<div class="text">Awesome Text</div>
</div>
:warning: Async Plugins can't be used in sync mode and will throw an Error. It's recommended to use PostHTML asynchronously whenever possible.
Async
import posthtml from 'posthtml'
import include from 'posthtml-include'
import expressions from 'posthtml-expressions'
const html = `
<include src="header.html"></include>
<h1>{{ title }}</h1>
`
posthtml([
include({ root: './src' }),
expressions({ locals: { title: 'Hello PostHTML' } })
])
.process(html/*, options */)
.then((result) => console.log(result.html))
<header>Site header</header>
<h1>Hello PostHTML</h1>
Directives
import posthtml from 'posthtml'
import expressions from 'posthtml-expressions'
const php = `
<h1>{{ title }}</h1>
<p><?php echo $article; ?></p>
`
posthtml([expressions({ locals: { title: 'Hello PostHTML' } })])
.process(php, {
directives: [
{ name: '?php', start: '<', end: '>' }
]
})
.then((result) => console.log(result.html))
<h1>Hello PostHTML</h1>
<p><?php echo $article; ?></p>
CLI
npm i posthtml-cli
"scripts": {
"posthtml": "posthtml -o output.html -i input.html -c config.json"
}
npm run posthtml
Gulp
npm i -D gulp-posthtml
import gulp from 'gulp'
import posthtml from 'gulp-posthtml'
import include from 'posthtml-include'
const { task, src, dest } = gulp
task('html', () => {
const plugins = [include({ root: 'src' })]
const options = {}
return src('src/**/*.html')
.pipe(posthtml(plugins, options))
.pipe(dest('build/'))
})
Webpack
npm i -D html-loader posthtml-loader
webpack.config.js
import include from 'posthtml-include'
export default {
module: {
rules: [
{
test: /\.html$/,
use: [
'html-loader',
{
loader: 'posthtml-loader',
options: {
plugins: [include({ root: 'src' })]
}
}
]
}
]
}
}
Custom parser and renderer
PostHTML uses posthtml-parser and posthtml-render by default. Pass parser or render in the process options to replace them.
Plugins
In case you want to develop your own plugin, we recommend using posthtml-plugin-starter to get started.
See the plugins catalog.
Contributing
Use Node.js 24 from .nvmrc and run npm run check. See the contributor guide for commands and releases, and AGENTS.md for coordinated work on PostHTML, its parser and renderer.
Maintainers
|
Ivan Demidov |
Ivan Voischev |
Contributors
관련 프로젝트 살펴보기
불러오는 중…