Minify HTML using a Node.js module

匆匆过客 提交于 2019-12-07 06:41:10

问题


I have HTML in a variable and before render it and I want to minify it. I know there are console minifiers such as:

  • html-minifier

But I want to minify in code, like this:

var minifier = require ('some-minifier');
var notMinifiedHtml = "<html>...</html>";
var minifiedHtml = minifier(notMinifiedHtml);

But I don't know such some-minifier library...


回答1:


The module you specified, html-minifier, already does what you're asking for. This is how it's used:

var minify = require('html-minifier').minify;
var input = '<!-- foo --><div>baz</div><!-- bar\n\n moo -->';
var output = minify(input, options);

The options object requires at least one of the boolean flags shown below. If no flags are specified, the minifier will just return the string that was passed in as input.

removeComments
removeCommentsFromCDATA
collapseWhitespace
collapseBooleanAttributes
removeAttributeQuotes
removeRedundantAttributes
useShortDoctype
removeEmptyAttributes
removeOptionalTags
removeEmptyElements

Note that the library parses the input as HTML, not XHTML.



来源:https://stackoverflow.com/questions/19018921/minify-html-using-a-node-js-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!