标题渲染钩子
上下文
标题渲染钩子模板接收以下 上下文
锚点
(string) 标题元素的 id 属性。
属性
(map) Markdown 属性,如果您按如下方式配置您的站点,则可用
hugo。
 
 
 
markup:
  goldmark:
    parser:
      attribute:
        title: true
[markup]
  [markup.goldmark]
    [markup.goldmark.parser]
      [markup.goldmark.parser.attribute]
        title = true
{
   "markup": {
      "goldmark": {
         "parser": {
            "attribute": {
               "title": true
            }
         }
      }
   }
}
级别
(int) 标题级别,1 到 6。
页面
(page) 对当前页面的引用。
PageInner
v0.125.0 版本新增(page) 通过 RenderShortcodes 方法嵌套的页面的引用。查看详情。
PlainText
(string) 标题文本作为纯文本。
Text
(template.HTML) 标题文本。
示例
在其默认配置中,Hugo 根据 CommonMark 规范 渲染 Markdown 标题,并添加了自动 id 属性。要创建一个执行相同操作的渲染钩子
layouts/_default/_markup/render-heading.html
<h{{ .Level }} id="{{ .Anchor }}" {{- with .Attributes.class }} class="{{ . }}" {{- end }}>
  {{- .Text -}}
</h{{ .Level }}>要在每个标题的右侧添加一个锚点链接
layouts/_default/_markup/render-heading.html
<h{{ .Level }} id="{{ .Anchor }}" {{- with .Attributes.class }} class="{{ . }}" {{- end }}>
  {{ .Text }}
  <a href="#{{ .Anchor }}">#</a>
</h{{ .Level }}>PageInner 详情
v0.125.0 版本新增PageInner 的主要用例是解析相对于包含的 Page 的链接和 页面资源。例如,创建一个“include”短代码,以便从多个内容文件组成一个页面,同时保留脚注和目录的全局上下文
layouts/shortcodes/include.html
{{ with .Get 0 }}
  {{ with $.Page.GetPage . }}
    {{- .RenderShortcodes }}
  {{ else }}
    {{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
{{ end }}然后在您的 Markdown 中调用该短代码
content/posts/p1.md
{{% include "/posts/p2" %}}在渲染 /posts/p2 时触发的任何渲染钩子都将获取
- 调用 Page时为/posts/p1
- 调用 PageInner时为/posts/p2
如果 PageInner 不相关,则会回退到 Page 的值,并且始终返回一个值。
一个实际的例子是,Hugo 内嵌的链接和图像渲染钩子使用 PageInner 方法来解析 markdown 链接和图像的目标地址。 请参阅各自的源代码。