Prettier & Visual Code settings for Hugo HTML templates

ぐ巨炮叔叔 提交于 2020-07-18 04:41:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!