简体   繁体   English

使用Node.js模块缩小HTML

[英]Minify HTML using a Node.js module

I have HTML in a variable and before render it and I want to minify it. 我在变量中有HTML,然后渲染它,我想缩小它。 I know there are console minifiers such as: 我知道有控制台缩小器,例如:

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... 但我不知道这样some-minifier图库......

The module you specified, html-minifier, already does what you're asking for. 您指定的模块html-minifier已经满足您的要求。 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. options对象至少需要一个如下所示的布尔标志。 If no flags are specified, the minifier will just return the string that was passed in as input. 如果未指定任何标志,则minifier将仅返回作为输入传入的字符串。

removeComments
removeCommentsFromCDATA
collapseWhitespace
collapseBooleanAttributes
removeAttributeQuotes
removeRedundantAttributes
useShortDoctype
removeEmptyAttributes
removeOptionalTags
removeEmptyElements

Note that the library parses the input as HTML, not XHTML. 请注意,库将输入解析为HTML,而不是XHTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM