js-beautify
https://github.com/beautify-web/js-beautify#options
Cai chuan
{
// exposed jsbeautifier options
"indent_size": 2,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 4,
"space_in_paren": false,
"space_in_empty_paren": false,
"space_after_anon_function": false,
"jslint_happy": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"eval_code": false,
"unescape_strings": false,
"unindent_chained_methods": false,
"break_chained_methods": false,
"e4x": false,
"wrap_line_length": 0,
"comma_first": false,
"operator_position": "before-newline",
// jsformat options
"format_on_save": false,
"format_selection": true,
"jsbeautifyrc_files": false,
"ignore_sublime_settings": true,
"format_on_save_extensions": ["js", "json"]
}
JS Beautifier
This little beautifier will reformat and re-indent bookmarklets, ugly JavaScript, unpack scripts packed by Dean Edward’s popular packer, as well as partly deobfuscate scripts processed by the npm package javascript-obfuscator.
Open beautifier.io to try it out. Options are available via the UI.
Contributors Needed
I'm putting this front and center above because existing owners have very limited time to work on this project currently. This is a popular project and widely used but it desperately needs contributors who have time to commit to fixing both customer facing bugs and underlying problems with the internal design and implementation.
If you are interested, please take a look at the CONTRIBUTING.md then fix an issue marked with the "Good first issue" label and submit a PR. Repeat as often as possible. Thanks!
Installation
You can install the beautifier for node.js or python.
Node.js JavaScript
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.
You can also use js-beautify as a node library (install locally, the npm default):
Node.js JavaScript (vNext)
The above install the latest stable release. To install beta or RC versions:
Web Library
The beautifier can be added on your page as web library.
JS Beautifier is hosted on two CDN services: cdnjs and rawgit.
To pull the latest version from one of these services include one set of the script tags below in your document:
Older versions are available by changing the version number.
Disclaimer: These are free services, so there are no uptime or support guarantees.
Python
To install the Python version of the beautifier:
Usage
You can beautify javascript using JS Beautifier in your web browser, or on the command-line using node.js or python.
Web Browser
Open beautifier.io. Options are available via the UI.
Web Library
The script tags above expose three functions: js_beautify, css_beautify, and html_beautify.
Node.js JavaScript
When installed globally, the beautifier provides an executable js-beautify script. The beautified result is sent to stdout unless otherwise configured.
To use js-beautify as a node library (after install locally), import and call the appropriate beautifier method for javascript (js), css, or html. All three method signatures are beautify(code, options). code is the string of code to be beautified. options is an object with the settings you would like used to beautify the code.
The configuration option names are the same as the CLI names but with underscores instead of dashes. For example, --indent-size 2 --space-in-empty-paren would be { indent_size: 2, space_in_empty_paren: true }.
Python
After installing, to beautify using Python:
Beautified output goes to stdout by default.
To use jsbeautifier as a library is simple:
...or, to specify some options:
The configuration option names are the same as the CLI names but with underscores instead of dashes. The example above would be set on the command-line as --indent-size 2 --space-in-empty-paren.
Options
These are the command-line flags for both Python and JS scripts:
Which correspond to the underscored option keys for both library interfaces
defaults per CLI options
defaults not exposed in the cli
Notice not all defaults are exposed via the CLI. Historically, the Python and JS APIs have not been 100% identical. There are still a few other additional cases keeping us from 100% API-compatibility.
Loading settings from environment or .jsbeautifyrc (JavaScript-Only)
In addition to CLI arguments, you may pass config to the JS executable via:
any
jsbeautify_-prefixed environment variablesa
JSON-formatted file indicated by the--configparametera
.jsbeautifyrcfile containingJSONdata at any level of the filesystem above$PWD
Configuration sources provided earlier in this stack will override later ones.
Setting inheritance and Language-specific overrides
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:
Using the above example would have the following result:
HTML files
Inherit
indent_sizeof 4 spaces from the top-level setting.The files would also end with a newline.
JavaScript and CSS inside HTML
Inherit the HTML
end_with_newlinesetting.Override their indentation to 2 spaces.
CSS files
Override the top-level setting to an
indent_sizeof 1 space.
JavaScript files
Inherit
indent_sizeof 4 spaces from the top-level setting.Set
preserve-newlinestotrue.
CSS & HTML
In addition to the js-beautify executable, css-beautify and html-beautify are also provided as an easy interface into those scripts. Alternatively, js-beautify --css or js-beautify --html will accomplish the same thing, respectively.
The CSS & HTML beautifiers are much simpler in scope, and possess far fewer options.
Directives
Directives let you control the behavior of the Beautifier from within your source files. Directives are placed in comments inside the file. Directives are in the format /* beautify {name}:{value} */ in CSS and JavaScript. In HTML they are formatted as <!-- beautify {name}:{value} -->.
Ignore directive
The ignore directive makes the beautifier completely ignore part of a file, treating it as literal text that is not parsed. The input below will remain unchanged after beautification:
Preserve directive
NOTE: this directive only works in HTML and JavaScript, not CSS.
The preserve directive makes the Beautifier parse and then keep the existing formatting of a section of code.
The input below will remain unchanged after beautification:
License
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)
Credits
Created by Einar Lielmanis, einar@beautifier.io
Python version flourished by Stefano Sanfilippo a.little.coder@gmail.com
Command-line for node.js by Daniel Stockman daniel.stockman@gmail.com
Maintained and expanded by Liam Newman bitwiseman@beautifier.io
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.10.2
Last updated
Was this helpful?