Hugo HTML模板的漂亮和可视代码设置

问题描述 投票:5回答:1

我通常喜欢将Prettier与Visual Code结合使用。但是,Prettier在为Hugo编辑HTML模板时让我发疯,因为它不会保留读者友好的格式:

  {{ 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">

相反,它转换为:

  {{ 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"
  />

如何定制Prettier以更好地处理模板逻辑? (此后我就停用了它。)

html visual-studio-code vscode-settings hugo prettier
1个回答
0
投票

更漂亮地破坏了我们的GoHugo html文件也使我非常恼火。以下插件可修复此行为。

prettier-plugin-go-template


如果使用普通的*.html文件,则需要注意自述文件的GoHugo部分:

要与GoHugo和基本.html文件一起使用,您必须覆盖 .prettierrc文件中使用的解析器:

{
  "overrides": [
    {
      "files": ["*.html"],
      "options": {
        "parser": "go-template"
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.