jsbeautifier.org for node
npm install js-beautify-ejsxvoidElements: [], that can be used to specify which elements should be considered void
This little beautifier will reformat and reindent bookmarklets, ugly
JavaScript, unpack scripts packed by Dean Edward’s popular packer,
as well as deobfuscate scripts processed by
javascriptobfuscator.com.
JS Beautifier is hosted on two CDN services: cdnjs and rawgit.
To pull from one of these services include one set of the script tags below in your document:
``html
`
Disclaimer: These are free services, so there are no uptime or support guarantees.
`bash`
$ pip install jsbeautifier
$ js-beautify file.js
Beautified output goes to stdout.
To use jsbeautifier as a library is simple:
` python`
import jsbeautifier
res = jsbeautifier.beautify('your javascript string')
res = jsbeautifier.beautify_file('some_file.js')
...or, to specify some options:
` python`
opts = jsbeautifier.default_options()
opts.indent_size = 2
res = jsbeautifier.beautify('some javascript', opts)
As an alternative to the Python script, you may install the NPM package js-beautify. When installed globally, it provides an executable js-beautify script. As with the Python script, the beautified result is sent to stdout unless otherwise configured.
`bash`
$ npm -g install js-beautify
$ js-beautify foo.js
You can also use js-beautify as a node library (install locally, the npm default):
`bash`
$ npm install js-beautify
`js
var beautify = require('js-beautify').js_beautify,
fs = require('fs');
fs.readFile('foo.js', 'utf8', function (err, data) {
if (err) {
throw err;
}
console.log(beautify(data, { indent_size: 2 }));
});
`
These are the command-line flags for both Python and JS scripts:
`text
CLI Options:
-f, --file Input file(s) (Pass '-' for stdin)
-r, --replace Write output in-place, replacing input
-o, --outfile Write output to file (default stdout)
--config Path to config file
--type [js|css|html] ["js"]
-q, --quiet Suppress logging to stdout
-h, --help Show this help
-v, --version Show the version
Beautifier Options:
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-t, --indent-with-tabs Indent with tabs, overrides -s and -c
-e, --eol Character(s) to use as line terminators.
[first newline in file, otherwise "\n]
-n, --end-with-newline End output with newline
--editorconfig Use EditorConfig to set up the options
-l, --indent-level Initial indentation level [0]
-p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)
-m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]
-P, --space-in-paren Add padding spaces within paren, ie. f( a, b )
-E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )
-j, --jslint-happy Enable jslint-stricter mode
-a, --space-after-anon-function Add a space before an anonymous function's parens, ie. function ()
-b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]
-B, --break-chained-methods Break chained method calls across subsequent lines
-k, --keep-array-indentation Preserve array indentation
-x, --unescape-strings Decode printable characters encoded in xNN notation
-w, --wrap-line-length Wrap lines at next opportunity after N characters [0]
-X, --e4x Pass E4X xml literals through untouched
--good-stuff Warm the cockles of Crockford's heart
-C, --comma-first Put commas at the beginning of new line instead of end
-O, --operator-position Set operator position (before-newline|after-newline|preserve-newline) [before-newline]
`
Which correspond to the underscored option keys for both library interfaces
defaults per CLI options
`json`
{
"indent_size": 4,
"indent_char": " ",
"indent_with_tabs": false,
"eol": "\n",
"end_with_newline": false,
"indent_level": 0,
"preserve_newlines": true,
"max_preserve_newlines": 10,
"space_in_paren": false,
"space_in_empty_paren": false,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"break_chained_methods": false,
"keep_array_indentation": false,
"unescape_strings": false,
"wrap_line_length": 0,
"e4x": false,
"comma_first": false,
"operator_position": "before-newline"
}
defaults not exposed in the cli
`json`
{
"eval_code": false,
"space_before_conditional": true
}
Notice not all defaults are exposed via the CLI. Historically, the Python and
JS APIs have not been 100% identical. For example, space_before_conditional is
currently JS-only, and not addressable from the CLI script. There are still a
few other additional cases keeping us from 100% API-compatibility.
In addition to CLI arguments, you may pass config to the JS executable via:
* any jsbeautify_-prefixed environment variablesJSON
* a -formatted file indicated by the --config parameter.jsbeautifyrc
* a file containing JSON data at any level of the filesystem above $PWD
Configuration sources provided earlier in this stack will override later ones.
The settings are a shallow tree whose values are inherited for all languages, but
can be overridden. This works for settings passed directly to the API in either implementation.
In the Javascript implementation, settings loaded from a config file, such as .jsbeautifyrc,
can also use inheritance/overriding.
Below is an example configuration tree showing all the supported locations
for language override nodes. We'll use indent_size to discuss how this configuration
would behave, but any number of settings can be inherited or overridden:
`json`
{
"indent_size": 4,
"html": {
"end_with_newline": true,
"js": {
"indent_size": 2
},
"css": {
"indent_size": 2
}
},
"css": {
"indent_size": 1
},
"js": {
"preserve-newlines": true
}
}
Using the above example would have the following result:
* HTML files
* Inherit indent_size of 4 spaces from the top-level setting.end_with_newline
* The files would also end with a newline.
* JavaScript and CSS inside HTML
* Inherit the HTML settingindent_size
* Override their indentation to 2 spaces
* CSS files
* Override the top-level setting to an of 1 space.indent_size
* JavaScript files
* Inherit of 4 spaces from the top-level settingpreserve-newlines
* Set to true
In addition to the js-beautify executable, css-beautify and html-beautifyjs-beautify --css
are also provided as an easy interface into those scripts. Alternatively, or js-beautify --html will accomplish the same thing, respectively.
`js
// Programmatic access
var beautify_js = require('js-beautify'); // also available under "js" export
var beautify_css = require('js-beautify').css;
var beautify_html = require('js-beautify').html;
// All methods accept two arguments, the string to be beautified, and an options object.
`
The CSS & HTML beautifiers are much simpler in scope, and possess far fewer options.
`text
CSS Beautifier Options:
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-t, --indent-with-tabs Indent with tabs, overrides -s and -c
-e, --eol Character(s) to use as line terminators. (default newline - "\\n")
-n, --end-with-newline End output with newline
-L, --selector-separator-newline Add a newline between multiple selectors
-N, --newline-between-rules Add a newline between CSS rules
HTML Beautifier Options:
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-t, --indent-with-tabs Indent with tabs, overrides -s and -c
-e, --eol Character(s) to use as line terminators. (default newline - "\\n")
-n, --end-with-newline End output with newline
-p, --preserve-newlines Preserve existing line-breaks (--no-preserve-newlines disables)
-m, --max-preserve-newlines Maximum number of line-breaks to be preserved in one chunk [10]
-I, --indent-inner-html Indent
Directives to Ignore or Preserve sections (Javascript only)
Beautifier for supports directives in comments inside the file.
This allows you to tell the beautifier to preserve the formatting of or completely ignore part of a file.
The example input below will remain changed after beautification
`js
// Use preserve when the content is not javascript, but you don't want it reformatted.
/ beautify preserve:start /
{
browserName: 'internet explorer',
platform: 'Windows 7',
version: '8'
}
/ beautify preserve:end /// Use ignore when the content is not parsable as javascript.
var a = 1;
/ beautify ignore:start /
{This is some strange{template language{using open-braces?
/ beautify ignore:end /
``You are free to use this in any way you want, in case you find this
useful or working for you but you must keep the copyright notice and license. (MIT)
* Created by Einar Lielmanis,
* Python version flourished by Stefano Sanfilippo
* Command-line for node.js by Daniel Stockman
* Maintained and expanded by Liam Newman
Thanks also to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, Dave
Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull,
Mathias Bynens, Vittorio Gambaletta and others.
(README.md: js-beautify@1.6.12)