diff --git a/doc/prettybat.md b/doc/prettybat.md index 41608bf..83136f4 100644 --- a/doc/prettybat.md +++ b/doc/prettybat.md @@ -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) | diff --git a/src/prettybat.sh b/src/prettybat.sh index de8f061..86277e3 100755 --- a/src/prettybat.sh +++ b/src/prettybat.sh @@ -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"