Format HTML automatically from command line (Similar to eslint)

Viewed 2114

Is it possible to format HTML automatically with a tool in a similar way that eslint formats javascript? Why does it seem that there isn't many customizable options that you can integrate as part of your development pipeline?

I would wish to format HTML in the following way automatically with a command ran from the terminal:

<input
  class="input-style"
  placeholder="Replace me!"
/>

So for example I could npm run html-lint and it would fix the syntax in html files and warn about cases it cant fix.

4 Answers

js-beautify also works on HTML.

npm install js-beautify
js-beautify --type html file.html

Notice all this beautifying makes the file size increase substantially. The indentation is great for revision and editing, not so much for hosting. For that reason you might find html-minifier equally useful.

I personally think tidy is a fantastic options for tidying up HTML files. Checkout Tidy

maybe what you are looking for is prettier, this also supports CLI, even you can also make config, see the complete documentation here. Prettier CLI

I hope this helps.

Related