prettybat: Add support for SVG formatting using prettier

This commit is contained in:
Ethan P 2021-04-06 15:51:41 -07:00
parent 860638715a
commit 54db67408e
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 12 additions and 3 deletions

View File

@ -30,6 +30,7 @@ See `man bat` for more information.
| JSON | [prettier](https://prettier.io/) |
| YAML | [prettier](https://prettier.io/) |
| HTML | [prettier](https://prettier.io/) |
| SVG | [prettier](https://prettier.io/) |
| Rust | [rustfmt](https://github.com/rust-lang/rustfmt) |
| Bash | [shfmt](https://github.com/mvdan/sh) |
| C | [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) |

View File

@ -32,7 +32,7 @@ formatter_prettier_supports() {
.ts | .tsx | \
.css | .scss | .sass | \
.graphql | .gql | \
.html | \
.html | .svg | \
.json | \
.md | \
.yml)
@ -44,7 +44,14 @@ formatter_prettier_supports() {
}
formatter_prettier_process() {
prettier --stdin --stdin-filepath "$1" 2>/dev/null
# Rewrite the file extension to hackily support SVG.
local file="$1"
local fext="$(extname "$file")"
case "$fext" in
.svg) file="$(basename -- "$file" "$fext").html" ;;
esac
prettier --stdin --stdin-filepath "$file" 2>/dev/null
return $?
}
@ -130,6 +137,7 @@ map_language_to_extension() {
css) ext=".css" ;;
scss) ext=".scss" ;;
sass) ext=".sass" ;;
svg ) ext=".svg" ;;
html | htm | shtml | xhtml) ext=".html" ;;
json) ext=".json" ;;
md | mdown | markdown) ext=".md" ;;
@ -222,7 +230,7 @@ process_file() {
# shellcheck disable=SC2094 disable=SC2181
if [[ "$file" = "-" ]]; then
data_raw="$(cat -)"
data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<<"$data_raw")"
data_formatted="$("formatter_${formatter}_process" "STDIN${fext}" 2>/dev/null <<<"$data_raw")"
if [[ $? -ne 0 ]]; then
print_warning "'STDIN': Unable to format with '%s'" "$formatter"