RepositoryJavaScript
posthtml

posthtml/posthtml

PostHTML is a tool to transform HTML/XML with JS plugins

3.0ksao
113fork
3issue
0lượt xem

PostHTML

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.

For more detailed information about PostHTML in general take a look at the docs.

Dependencies

NameDescription
posthtml-parserParser HTML/XML to PostHTMLTree
posthtml-renderRender PostHTMLTree to HTML/XML

Create to your project

npm init posthtml

Install

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'
import customElements from 'posthtml-custom-elements'

const html = `
  <component>
    
    <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 toSvgTags from 'posthtml-to-svg-tags'
import extendAttrs from 'posthtml-extend-attrs'

const html = `
  
    
      <p class="wow">OMG</p>
    
  
`

posthtml(
  [
    toSvgTags(),
    extendAttrs({
      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>

Directives

import posthtml from 'posthtml'
import customElements from 'posthtml-custom-elements'

const php = `
  <component>
    
    <text><?php echo $article; ?></text>
  </component>
`

posthtml()
  .use(customElements())
  .process(php, {
    directives: [
      { name: '?php', start: '<', end: '>' }
    ]
  })
  .then((result) => console.log(result.html))
<div class="component">
  <div class="title"><?php echo $title; ?></div>
  <div class="text"><?php echo $article; ?></div>
</div>

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/'))
})

Check project-stub for an example with Gulp

Grunt

npm i -D grunt-posthtml
posthtml: {
  options: {
    use: [
      require('posthtml-doctype')({ doctype: 'HTML 5' }),
      require('posthtml-include')({ root: './', encoding: 'utf-8' })
    ]
  },
  build: {
    files: [
      {
        dot: true,
        cwd: 'html/',
        src: ['*.html'],
        dest: 'tmp/',
        expand: true,
      }
    ]
  }
}

Webpack

npm i -D html-loader posthtml-loader

v1.x

webpack.config.js

import pug from 'posthtml-pug'
import bem from 'posthtml-bem'

const config = {
  module: {
    loaders: [
      {
        test: /\.html$/,
        loader: 'html!posthtml'
      }
    ]
  },
  posthtml: (ctx) => ({
    parser: pug,
    plugins: [
      bem()
    ]
  })
}

export default config

v2.x

webpack.config.js

import webpack from 'webpack'
import pug from 'posthtml-pug'
import bem from 'posthtml-bem'

const { LoaderOptionsPlugin } = webpack

const config = {
  module: {
    rules: [
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: true }
          },
          {
            loader: 'posthtml-loader'
          }
        ]
      }
    ]
  },
  plugins: [
    new LoaderOptionsPlugin({
      options: {
        posthtml(ctx) {
          return {
            parser: pug,
            plugins: [
              bem()
            ]
          }
        }
      }
    })
  ]
}

export default config

Rollup

$ npm i rollup-plugin-posthtml -D
# or
$ npm i rollup-plugin-posthtml-template -D
import posthtml from 'rollup-plugin-posthtml-template'
// or
// import posthtml from 'rollup-plugin-posthtml'

import sugarml from 'posthtml-sugarml'  // npm i posthtml-sugarml -D
import include from 'posthtml-include'  // npm i posthtml-include -D

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'iife'
  },
  plugins: [
    posthtml({
      parser: sugarml(),
      plugins: [include()],
      template: true  // only rollup-plugin-posthtml-template
    })
  ]
}

Parser

import posthtml from 'posthtml'
import pug from 'posthtml-pug'

posthtml().process(html, { parser: pug(options) }).then((result) => result.html)
NameDescription
posthtml-pugPug Parser
sugarmlSugarML Parser

Plugins

In case you want to develop your own plugin, we recommend using posthtml-plugin-starter to get started.

Maintainers


Ivan Demidov

Ivan Voischev

Contributors

View all contributors on GitHub.

Dự án tương tự

Khám phá các dự án và mô hình AI khác có sự tương đồng.

RepositoryTypeScript
fb55/htmlparser2

The fast & forgiving HTML and XML parser

4.8k
Dec 1, 2026
RepositoryPHP
Imangazaliev/DiDOM

Simple and fast HTML and XML parser

2.2k
Jan 28, 2026
Repository
getify/You-Dont-Know-JS

A book series (2 published editions) on the JS language.

184.8k
Oct 31, 2026
RepositoryJavaScript
twbs/bootstrap

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.

175.1k
Dec 1, 2026
RepositoryPython
huggingface/transformers

🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.

169.5k
Dec 1, 2026
RepositoryTypeScript
morluto/rea

Reverse engineer anything with agents, from app behavior down to native binaries.

487.0k
Dec 2, 2026
RepositoryTypeScript
firecrawl/firecrawl

Firecrawl is the knowledge infrastructure to make your agents smarter. It connects your agents to the web, and to Alexandria, our library of everything else.

215.1k
Dec 2, 2026
Modelsentence-similarity
sentence-transformers/all-MiniLM-L6-v2

all-MiniLM-L6-v2 This is a sentence-transformers(https://www.SBERT.net) model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search. Usage (Sentence-Transformers) Using t...

♥0
157.6M
Jun 1, 2026
RepositoryTypeScript
cheeriojs/cheerio

The fast, flexible, and elegant library for parsing and manipulating HTML and XML.

30.6k
Dec 2, 2026