Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier & Visual Code settings for Hugo HTML templates

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.)

like image 570
AWhitford Avatar asked Dec 20 '19 21:12

AWhitford


1 Answers

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"
      }
    }
  ]
}
like image 115
NiklasPor Avatar answered Oct 18 '22 14:10

NiklasPor