问题
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.)
回答1:
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"
}
}
]
}
来源:https://stackoverflow.com/questions/59431432/prettier-visual-code-settings-for-hugo-html-templates