Module for Pretty Printing HTML?

2019-03-20 07:20发布

I'm working on a grunt build file which hits a URL and writes the output to a static HTML file. The url I'm hitting has compressed HTML and I'd like to pretty print it before writing to the static file. Are there any good modules for doing this? I've looked around and it seems like Max Ogden's html prettyprinter is my closest option (https://github.com/maxogden/commonjs-html-prettyprinter). Maybe if I combine it with the grunt-shell task or something? Really I'd prefer to just require a module in grunt and say pretty(my-file.html) and then write that using fs but so far that is proving elusive.

1条回答
冷血范
2楼-- · 2019-03-20 08:07

You found all the resources you need. That module does it for you.

var html = require("html");
var data = '<h2><strong><a href="http://awesome.com">AwesomeCom</a></strong><span>is awesome</span></h2>';
var prettyData = html.prettyPrint(data, {indent_size: 2});
process.stdout.write(prettyData)

Look at it's source.


Or you could use child_process to execute the command found in the README: html *.html.

查看更多
登录 后发表回答