Gathering detailed insights and metrics for posthtml
Gathering detailed insights and metrics for posthtml
Gathering detailed insights and metrics for posthtml
Gathering detailed insights and metrics for posthtml
PostHTML is a tool to transform HTML/XML with JS plugins
npm install posthtml
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,940 Stars
629 Commits
116 Forks
50 Watching
15 Branches
57 Contributors
Updated on 25 Nov 2024
JavaScript (91.8%)
HTML (7.57%)
Shell (0.62%)
Cumulative downloads
Total Downloads
Last day
6.6%
153,450
Compared to previous day
Last week
8.6%
849,929
Compared to previous week
Last month
20.6%
3,445,560
Compared to previous month
Last year
2%
35,287,043
Compared to previous year
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.
Name | Status | Description |
---|---|---|
posthtml-parser | Parser HTML/XML to PostHTMLTree | |
posthtml-render | Render PostHTMLTree to HTML/XML |
1npm init posthtml
1npm i -D posthtml
Sync
1import posthtml from 'posthtml' 2 3const html = ` 4 <component> 5 <title>Super Title</title> 6 <text>Awesome Text</text> 7 </component> 8` 9 10const result = posthtml() 11 .use(require('posthtml-custom-elements')()) 12 .process(html, { sync: true }) 13 .html 14 15console.log(result)
1<div class="component"> 2 <div class="title">Super Title</div> 3 <div class="text">Awesome Text</div> 4</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
1import posthtml from 'posthtml' 2 3const html = ` 4 <html> 5 <body> 6 <p class="wow">OMG</p> 7 </body> 8 </html> 9` 10 11posthtml( 12 [ 13 require('posthtml-to-svg-tags')(), 14 require('posthtml-extend-attrs')({ 15 attrsTree: { 16 '.wow' : { 17 id: 'wow_id', 18 fill: '#4A83B4', 19 'fill-rule': 'evenodd', 20 'font-family': 'Verdana' 21 } 22 } 23 }) 24 ]) 25 .process(html/*, options */) 26 .then((result) => console.log(result.html))
1<svg xmlns="http://www.w3.org/2000/svg"> 2 <text 3 class="wow" 4 id="wow_id" 5 fill="#4A83B4" 6 fill-rule="evenodd" font-family="Verdana"> 7 OMG 8 </text> 9</svg>
Directives
1import posthtml from 'posthtml' 2 3const php = ` 4 <component> 5 <title><?php echo $title; ?></title> 6 <text><?php echo $article; ?></text> 7 </component> 8` 9 10const result = posthtml() 11 .use(require('posthtml-custom-elements')()) 12 .process(html, { 13 directives: [ 14 { name: '?php', start: '<', end: '>' } 15 ] 16 }) 17 .html 18 19console.log(result)
1<div class="component"> 2 <div class="title"><?php echo $title; ?></div> 3 <div class="text"><?php echo $article; ?></div> 4</div>
1npm i posthtml-cli
1"scripts": { 2 "posthtml": "posthtml -o output.html -i input.html -c config.json" 3}
1npm run posthtml
1npm i -D gulp-posthtml
1import tap from 'gulp-tap' 2import posthtml from 'gulp-posthtml' 3import { task, src, dest } from 'gulp' 4 5task('html', () => { 6 let path 7 8 const plugins = [ require('posthtml-include')({ root: `${path}` }) ] 9 const options = {} 10 11 src('src/**/*.html') 12 .pipe(tap((file) => path = file.path)) 13 .pipe(posthtml(plugins, options)) 14 .pipe(dest('build/')) 15})
Check project-stub for an example with Gulp
1npm i -D grunt-posthtml
1posthtml: { 2 options: { 3 use: [ 4 require('posthtml-doctype')({ doctype: 'HTML 5' }), 5 require('posthtml-include')({ root: './', encoding: 'utf-8' }) 6 ] 7 }, 8 build: { 9 files: [ 10 { 11 dot: true, 12 cwd: 'html/', 13 src: ['*.html'], 14 dest: 'tmp/', 15 expand: true, 16 } 17 ] 18 } 19}
1npm i -D html-loader posthtml-loader
webpack.config.js
1const config = { 2 module: { 3 loaders: [ 4 { 5 test: /\.html$/, 6 loader: 'html!posthtml' 7 } 8 ] 9 }, 10 posthtml: (ctx) => ({ 11 parser: require('posthtml-pug'), 12 plugins: [ 13 require('posthtml-bem')() 14 ] 15 }) 16} 17 18export default config
webpack.config.js
1import { LoaderOptionsPlugin } from 'webpack' 2 3const config = { 4 module: { 5 rules: [ 6 { 7 test: /\.html$/, 8 use: [ 9 { 10 loader: 'html-loader', 11 options: { minimize: true } 12 }, 13 { 14 loader: 'posthtml-loader' 15 } 16 ] 17 } 18 ] 19 }, 20 plugins: [ 21 new LoaderOptionsPlugin({ 22 options: { 23 posthtml(ctx) { 24 return { 25 parser: require('posthtml-pug'), 26 plugins: [ 27 require('posthtml-bem')() 28 ] 29 } 30 } 31 } 32 }) 33 ] 34} 35 36export default config
1$ npm i rollup-plugin-posthtml -D 2# or 3$ npm i rollup-plugin-posthtml-template -D
1import { join } from 'path'; 2 3import posthtml from 'rollup-plugin-posthtml-template'; 4// or 5// import posthtml from 'rollup-plugin-posthtml'; 6 7import sugarml from 'posthtml-sugarml'; // npm i posthtml-sugarml -D 8import include from 'posthtml-include'; // npm i posthtml-include -D 9 10export default { 11 entry: join(__dirname, 'main.js'), 12 dest: join(__dirname, 'bundle.js'), 13 format: 'iife', 14 plugins: [ 15 posthtml({ 16 parser: sugarml(), 17 plugins: [include()], 18 template: true // only rollup-plugin-posthtml-template 19 }) 20 ] 21};
1import pug from 'posthtml-pug' 2 3posthtml().process(html, { parser: pug(options) }).then((result) => result.html)
Name | Status | Description |
---|---|---|
posthtml-pug | Pug Parser | |
sugarml | SugarML Parser |
In case you want to develop your own plugin, we recommend using posthtml-plugin-starter to get started.
Ivan Demidov |
Ivan Voischev |
Thank you to all our backers! 🙏 [Become a backer]
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 3/11 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More