使用Node.js模块压缩HTML [英] Minify HTML using a Node.js module

查看:188
本文介绍了使用Node.js模块压缩HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量中的HTML,并且在呈现它之前,我想缩小它.我知道有一些控制台缩小器,例如:

I have HTML in a variable and before render it and I want to minify it. 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);

但是我不知道这样的some-minifier库...

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

推荐答案

您指定的模块html-minifier已经满足您的要求.这是它的用法:

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);

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

请注意,该库将输入解析为HTML,而不是XHTML.

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

这篇关于使用Node.js模块压缩HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆