» npm install html-format
» npm install html-prettify
» npm install html-formatter
» npm install @cucumber/html-formatter
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.
If you are looking for alternatives (perhaps because a project has been abandoned, or you don't like the API), you can use NPM's website to search for others. Here are a few options: https://www.npmjs.com/search?q=html%20prettify I sorted by popularity and picked the first one, pretty.
The code ends up looking pretty similar to the accepted answer:
import pretty from 'pretty';
var data = '<h2><strong><a href="http://awesome.com">AwesomeCom</a></strong><span>is awesome</span></h2>';
var prettyData = pretty(data);
process.stdout.write(prettyData)
» npm install js-beautify
» npm install html-to-formatted-text
» npm install gulp-format-html
» npm install @mkhalidkhan/standard-html-formatter
I want a tool that formats HTML files from the command line
– I recommend js-beautify, as suggested in this answer.
I prefer to install it globally:
npm install --global js-beautify
I typically want to format all *.html files in the current folder. 1
js-beautify *.html --type html --replace --indent-size 1 --max-preserve-newlines 0
The above fulfills all eight requirements of the question, including the three optional ones.
References
- The documentation of the NPM package
js-beautify– version 1.14.9 - Answer using
js-beautify
1 The command may be written shorter as :
js-beautify --type html -r -s 1 -m 0 *.html
This formats the HTML to my liking. Check out the Options to find your favorite settings.
Try html tidy.
Tidy is a console application for macOS, Linux, Windows, UNIX, and more. It corrects and cleans up HTML and XML documents by fixing markup errors .
It has many options and is available on many platforms, and online.
Installation on Windows and integration of its output into an editor such as Textpad was kind of tricky but it can be done . https://www.google.com/search?q=html+tidy
I am not sure about your requirement "indent 1 character" but I think this can be worked around by indenting with tabs, and then global substitution with of tab with 1 blank.