MCPcopy
hub / github.com/posthtml/posthtml

github.com/posthtml/posthtml @v0.16.7 sqlite

repository ↗ · DeepWiki ↗ · release v0.16.7 ↗
38 symbols 92 edges 17 files 12 documented · 32%
README

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

Name Description
posthtml-parser Parser HTML/XML to PostHTMLTree
posthtml-render Render PostHTMLTree to HTML/XML

Create to your project

npm init posthtml

Install

npm i -D posthtml

Usage

API

Sync

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)





Super Title




Awesome Text





: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'

const html = `
  <html>
    <body>


OMG


    </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>

Directives

import posthtml from 'posthtml'

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

const result = posthtml()
  .use(require('posthtml-custom-elements')())
  .process(html, {
    directives: [
      { name: '?php', start: '<', end: '>' }
    ]
  })
  .html

console.log(result)





<?php echo $title; ?>




<?php echo $article; ?>





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 tap from 'gulp-tap'
import posthtml from 'gulp-posthtml'
import { task, src, dest } from 'gulp'

task('html', () => {
  let path

  const plugins = [ require('posthtml-include')({ root: `${path}` }) ]
  const options = {}

  src('src/**/*.html')
    .pipe(tap((file) => path = file.path))
    .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

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

export default config

v2.x

webpack.config.js

import { LoaderOptionsPlugin } from '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: require('posthtml-pug'),
            plugins: [
              require('posthtml-bem')()
            ]
          }
        }
      }
    })
  ]
}

export default config

Rollup

$ npm i rollup-plugin-posthtml -D
# or
$ npm i rollup-plugin-posthtml-template -D
import { join } from 'path';

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 {
  entry: join(__dirname, 'main.js'),
  dest: join(__dirname, 'bundle.js'),
  format: 'iife',
  plugins: [
    posthtml({
      parser: sugarml(),
      plugins: [include()],
      template: true  // only rollup-plugin-posthtml-template
    })
  ]
};

Parser

import pug from 'posthtml-pug'

posthtml().process(html, { parser: pug(options) }).then((result) => result.html)
Name Description
posthtml-pug Pug Parser
sugarml SugarML 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

Backers

Thank you to all our backers! 🙏 [Become a backer]

Extension points exported contracts — how you extend this code

PostHTML (Interface)
(no doc) [1 implementers]
types/posthtml.d.ts
NodeAPI (Interface)
(no doc)
types/posthtml.d.ts
RawNode (Interface)
(no doc)
types/posthtml.d.ts
Node (Interface)
(no doc)
types/posthtml.d.ts
Options (Interface)
(no doc)
types/posthtml.d.ts

Core symbols most depended-on inside this repo

use
called by 41
types/posthtml.d.ts
process
called by 37
types/posthtml.d.ts
traverse
called by 3
lib/api.js
plugin
called by 3
test/api.js
_treeExtendApi
called by 2
lib/index.js
isPromise
called by 2
lib/index.js
lazyResult
called by 2
lib/index.js
compare
called by 2
lib/api.js

Shape

Function 23
Method 7
Interface 6
Class 2

Languages

TypeScript100%

Modules by API surface

lib/index.js12 symbols
types/posthtml.d.ts8 symbols
lib/api.js5 symbols
test/options.js2 symbols
test/api.js2 symbols
test/text.js1 symbols
test/test.js1 symbols
test/source.js1 symbols
test/process.js1 symbols
test/plugins.js1 symbols
test/messages.js1 symbols
test/doctype.js1 symbols

Dependencies from manifests, versioned

@commitlint/cli16.2.1 · 1×
@commitlint/config-angular16.2.1 · 1×
c87.7.3 · 1×
chai4.3.4 · 1×
chai-as-promised7.1.1 · 1×
chai-subset1.6.0 · 1×
conventional-changelog-cli2.1.1 · 1×
husky7.0.1 · 1×
jsdoc-to-markdown7.0.1 · 1×
lint-staged12.3.4 · 1×
mocha9.0.3 · 1×
posthtml-parser0.11.0 · 1×

For agents

$ claude mcp add posthtml \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact