引用渲染钩子
上下文
引用渲染钩子模板接收以下上下文
AlertType
(string
) 当Type
为 alert
时适用,这是转换为小写的警报类型。请参阅下面的警报部分。
AlertTitle
v0.134.0 中的新增功能(template.HTML
) 当Type
为 alert
时适用,这是警报标题。请参阅下面的警报部分。
AlertSign
v0.134.0 中的新增功能(string
) 当Type
为 alert
时适用,这是警报符号。通常用于指示警报是否可以图形折叠,它是 +
、-
或空字符串之一。请参阅下面的警报部分。
Attributes
(map
) Markdown 属性,如果您按如下方式配置您的站点,则可用
markup:
goldmark:
parser:
attribute:
block: true
[markup]
[markup.goldmark]
[markup.goldmark.parser]
[markup.goldmark.parser.attribute]
block = true
{
"markup": {
"goldmark": {
"parser": {
"attribute": {
"block": true
}
}
}
}
}
Ordinal
(int
) 页面上引用的从零开始的序号。
Page
(page
) 对当前页面的引用。
PageInner
(page
) 通过RenderShortcodes
方法嵌套的页面的引用。查看详细信息。
Position
(string
) 引用在页面内容中的位置。
Text
(template.HTML
) 引用文本,如果Type
为 alert
,则排除第一行。请参阅下面的警报部分。
Type
(bool
) 引用类型。如果引用具有警报指示符,则返回 alert
,否则返回 regular
。请参阅下面的警报部分。
示例
在默认配置下,Hugo 根据 CommonMark 规范渲染 Markdown 块引用。要创建一个执行相同操作的渲染钩子
<blockquote>
{{ .Text }}
</blockquote>
要将块引用渲染为带有可选引文和标题的 HTML figure
元素
<figure>
<blockquote {{ with .Attributes.cite }}cite="{{ . }}"{{ end }}>
{{ .Text }}
</blockquote>
{{ with .Attributes.caption }}
<figcaption class="blockquote-caption">
{{ . | safeHTML }}
</figcaption>
{{ end }}
</figure>
然后在您的 Markdown 中
> Some text
{cite="https://gohugo.com.cn" caption="Some caption"}
警报
警报也称为标注或提示,是用于强调关键信息的块引用。
基本语法
使用基本的 Markdown 语法,每个警报的第一行都是一个警报指示符,由一个感叹号后跟警报类型组成,并用方括号括起来。例如
> [!NOTE]
> Useful information that users should know, even when skimming content.
> [!TIP]
> Helpful advice for doing things better or more easily.
> [!IMPORTANT]
> Key information users need to know to achieve their goal.
> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.
> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.
基本语法与 GitHub、Obsidian 和 Typora 兼容。
扩展语法
使用扩展的 Markdown 语法,您可以选择性地包含警报符号和/或警报标题。警报符号是 +
或 -
之一,通常用于指示警报在图形上是否可折叠。例如
> [!WARNING]+ Radiation hazard
> Do not approach or handle without protective gear.
扩展语法与 Obsidian 兼容。
示例
如果存在警报指示符,此块引用渲染钩子将渲染多语言警报,否则它将根据 CommonMark 规范渲染块引用。
{{ $emojis := dict
"caution" ":exclamation:"
"important" ":information_source:"
"note" ":information_source:"
"tip" ":bulb:"
"warning" ":information_source:"
}}
{{ if eq .Type "alert" }}
<blockquote class="alert alert-{{ .AlertType }}">
<p class="alert-heading">
{{ transform.Emojify (index $emojis .AlertType) }}
{{ with .AlertTitle }}
{{ . }}
{{ else }}
{{ or (i18n .AlertType) (title .AlertType) }}
{{ end }}
</p>
{{ .Text }}
</blockquote>
{{ else }}
<blockquote>
{{ .Text }}
</blockquote>
{{ end }}
要覆盖标签,请在 i18n 文件中创建这些条目
caution: Caution
important: Important
note: Note
tip: Tip
warning: Warning
caution = 'Caution'
important = 'Important'
note = 'Note'
tip = 'Tip'
warning = 'Warning'
{
"caution": "Caution",
"important": "Important",
"note": "Note",
"tip": "Tip",
"warning": "Warning"
}
尽管您可以像上面显示的那样使用一个带有条件逻辑的模板,但您也可以为每个 Type
的块引用创建单独的模板
layouts/
└── _default/
└── _markup/
├── render-blockquote-alert.html
└── render-blockquote-regular.html
PageInner 详细信息
v0.125.0 中的新增功能PageInner
的主要用例是解析相对于包含的 Page
的链接和 页面资源。例如,创建一个“include”短代码,从多个内容文件组合页面,同时保留脚注和目录的全局上下文
{{ 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 中调用短代码
{{% include "/posts/p2" %}}
在渲染 /posts/p2
时触发的任何渲染钩子都将得到
- 调用
Page
时为/posts/p1
- 调用
PageInner
时为/posts/p2
如果 PageInner
不相关,则会回退到 Page
的值,并且始终返回一个值。
作为一个实际示例,Hugo 的嵌入链接和图像渲染钩子使用 PageInner
方法来解析 markdown 链接和图像目标。请参阅每个的源代码