Skip to content

Instantly share code, notes, and snippets.

@brentsimmons
Last active April 13, 2024 11:27
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brentsimmons/7819109 to your computer and use it in GitHub Desktop.
Save brentsimmons/7819109 to your computer and use it in GitHub Desktop.
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);
})
@greg-raven
Copy link

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

@kupietools
Copy link

$ pip3 install jsbeautify
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement jsbeautify (from versions: none)
ERROR: No matching distribution found for jsbeautify

@greg-raven
Copy link

$ pip3 install jsbeautify
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement jsbeautify (from versions: none)
ERROR: No matching distribution found for jsbeautify

For what it's worth, I must have run into this, too, because I replaced this procedure with:

npm -g install js-beautify

... and everything seems to run fine.

@greg-raven
Copy link

This is the code for the BASH script I now use:

#!/bin/sh

## requires npm -g install js-beautify
## 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" == "HTML" ]; then
    js-beautify --html
elif [ "$BB_DOC_LANGUAGE" == "PHP in HTML" ]; then
    js-beautify --html
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