2

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.

2
  • Related: stackoverflow.com/questions/5133408/… Commented Jul 5, 2019 at 21:33
  • @Casburn Related indeed but not a duplicate, this question asks about a specific library not about a generic solution/suggested library as in the link above. Commented Jul 5, 2019 at 21:40

1 Answer 1

2

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() :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.