I generally like using Prettier with Visual Code. However, Prettier is making me crazy while editing HTML templates for Hugo because it will not preserve the reader friendly formatting of this:
{{ with .Site.Params.author }}<meta name="author" content="{{ . }}">{{ end }}
{{ hugo.Generator }}
{{ "<!-- plugins -->" | safeHTML }}
{{ range .Site.Params.plugins.css }}
<link rel="stylesheet" href="{{ .URL | absURL }} ">
{{ end }}
{{ "<!-- Main Stylesheet -->" | safeHTML }}
{{ $styles := resources.Get "scss/style.scss" | toCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.Permalink }}" integrity="{{ $styles.Data.Integrity }}" media="screen">
Instead it is transformed to:
{{ with .Site.Params.author }}
<meta name="author" content="{{ . }}" />
{{ end }} {{ hugo.Generator }} {{ "
<!-- plugins -->
" | safeHTML }} {{ range .Site.Params.plugins.css }}
<link rel="stylesheet" href="{{ .URL | absURL }} " />
{{ end }} {{ "
<!-- Main Stylesheet -->
" | safeHTML }} {{ $styles := resources.Get "scss/style.scss" | toCSS | minify
| fingerprint }}
<link
rel="stylesheet"
href="{{ $styles.Permalink }}"
integrity="{{ $styles.Data.Integrity }}"
media="screen"
/>
How can Prettier be customized to better handle template logic? (I have since resorted to disabling it.)
I also got really annoyed by prettier breaking our GoHugo html files. The plugin below fixes the behavior.
prettier-plugin-go-template
If you're using plain *.html
files, you'll want to pay attention to the GoHugo
section of the Readme:
To use it with GoHugo and basic
.html
files, you'll have to override the used parser inside your.prettierrc
file:
{
"overrides": [
{
"files": ["*.html"],
"options": {
"parser": "go-template"
}
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With