home.


Tagged: npm


Formatting/Beautify HTML and Javascript from the command line

First, install the js-beautify package from npm.

sudo npm -g install js-beautify

Now make some dodgily formatted HTML with javascript

echo '<html></body></body><script> var i = 0; var j = 1; </script></html>' > dodgy.html

js-beautify takes -f - to find things from the command line, and --type html to say the javascript is embedded in some HTML. Finally we pipe to less for viewing.

cat dodgy.html | js-beautify -f - --type html

The output is:

<html>
</body>
</body>
<script>
    var i = 0;
    var j = 1;
</script>
</html>

You can add -o dodgy.html to replace the original text in dodgy.html.

unix javascript html beautify npm

Page 1 of 1