Minify HTML using a Node.js module

后端 未结 1 515
醉梦人生
醉梦人生 2021-01-12 10:01

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

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 10:16

    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 = '
    baz
    '; 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.

    0 讨论(0)
提交回复
热议问题