简体   繁体   English

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

[英]Prettier & Visual Code settings for Hugo HTML templates

I generally like using Prettier with Visual Code.我通常喜欢将 Prettier 与 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:然而,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">

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?如何自定义 Prettier 以更好地处理模板逻辑? (I have since resorted to disabling it.) (从那以后我就采取了禁用它的方法。)

I also got really annoyed by prettier breaking our GoHugo html files.我也对更漂亮地破坏我们的 GoHugo html 文件感到非常恼火。 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:如果您使用纯*.html文件,您需要注意自述文件的GoHugo部分:

To use it with GoHugo and basic .html files, you'll have to override the used parser inside your .prettierrc file:要将它与 GoHugo 和基本.html文件一起使用,您必须覆盖.prettierrc文件中使用的解析器:

{
  "overrides": [
    {
      "files": ["*.html"],
      "options": {
        "parser": "go-template"
      }
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM