javascriptjqueryjs-beautify

How to format/beautify HTML, CSS and JavaScript code in core JavaScript?


I am trying to format code using the following JS-based plugin: https://github.com/beautify-web/js-beautify

All the examples are using NodeJS (it seems). I am only looking for a simple vanilla/code JavaScript solution.

Something like this:

let sourceContent = " function test() { ... }"
console.log(js-beautify(sourceContent));

Are there any available plugins out there? A jQuery solution is also fine.


Solution

  • The examples use nodejs but the library supports browsers too, as stated in the readme.

    So add the (js beautify) script tag as mentioned in the readme:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.10.0/beautify-js.min.js"></script>
    

    Then you can use the following example code:

    const sourceContent = "function test() { ... }";
    const beautified = js_beautify(sourceContent);
    

    The web library is mentioned here: https://github.com/beautify-web/js-beautify/blob/master/README.md#web-library

    The available web methods are mentioned here: https://github.com/beautify-web/js-beautify/blob/master/README.md#web-library-1

    So yes, you were very close with js-beautify() instead of js_beautify() :)