Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a BBEdit text filter for indenting (and beautifying) JavaScript.
#!/usr/local/bin/node
// PBS 12/5/13
// This is a BBEdit text filter for indenting (and beautifying) JavaScript.
// It goes in ~/Library/Application Support/BBEdit/Text Filters/
//
// On my machine I assigned it a keyboard shortcut: cmd-'
//
// It requires the js-beautify Node module: https://github.com/einars/js-beautify
//
// The trick, in the script, was providing the full path to the globally-installed js-beautify module.
//
// I figured out how to write a BBEdit/Node text filter from the very nice example here:
// http://www.mtaa.net/mtaaRR/news/twhid/geek/use_node_js_with_bbedit_text_filters_feature.html
var jsbeautify = require('/usr/local/lib/node_modules/js-beautify').js_beautify;
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
var s = jsbeautify(chunk, {
indent_size: 2,
brace_style: "end-expand"
});
process.stdout.write(s);
})
@twhid

This comment has been minimized.

Copy link

@twhid twhid commented Apr 3, 2014

Glad it was of some use :-) Trying to write a filter using UglifyJS right now but running into issues... oy.

@greg-raven

This comment has been minimized.

Copy link

@greg-raven greg-raven commented Apr 18, 2021

I don't use NodeJS, so this is the approach I used:

#!/bin/sh

## requires pip3 install jsbeautify
## requires pip3 install cssbeautify
## no htmlbeautify for pip installs
## no Homebrew version
## online at https://beautifier.io
## and https://github.com/beautify-web/js-beautify

## echo $BB_DOC_LANGUAGE

if [ "$BB_DOC_LANGUAGE" == "CSS" ]; then
	css-beautify
elif [ "$BB_DOC_LANGUAGE" == "JavaScript" ]; then
	js-beautify --jslint-happy
elif [ "$BB_DOC_LANGUAGE" == "JSON" ]; then
	python3 -m json.tool
else
	echo "unknown language: $BB_DOC_LANGUAGE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment