块引用渲染钩子
上下文
块引用渲染钩子模板接收以下[上下文]:
警报类型
(string
) 当 Type
为 alert
时适用,这是转换为小写的警报类型。参见下面的 警报 部分。
警报标题
New in v0.134.0(template.HTML
) 当 Type
为 alert
时适用,这是警报标题。参见下面的 警报 部分。
警报标志
New in v0.134.0(string
) 当 Type
为 alert
时适用,这是警报标志。通常用于指示警报是否可图形化折叠,它可以是 +
、 -
或空字符串之一。参见下面的 警报 部分。
属性
(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
}
}
}
}
}
序号
(int
) 页面上块引用的基于零的序号。
页面
(page
) 对当前页面的引用。
页面内部
(page
) 通过 RenderShortcodes
方法嵌套的页面的引用。 参见详细信息 。
位置
(string
) 块引用在页面内容中的位置。
文本
(template.HTML
) 块引用文本,如果 Type
为 alert
,则不包括第一行。参见下面的 警报 部分。
类型
(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.io" caption="Some caption"}
警报
也称为 提示 或 注意事项 ,警报是用于强调关键信息的块引用。
基本语法
使用基本的 Markdown 语法,每个警报的第一行都是一个警报指示符,它由一个感叹号后跟警报类型组成,并用方括号括起来。例如:
> [!NOTE]
> 用户应该知道的有用信息,即使是浏览内容时也是如此。
> [!TIP]
> 有助于更好地或更轻松地做事的有用建议。
> [!IMPORTANT]
> 用户需要知道才能实现其目标的关键信息。
> [!WARNING]
> 需要用户立即注意以避免问题的紧急信息。
> [!CAUTION]
> 建议注意某些操作的风险或负面结果。
基本语法与 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"
}
尽管您可以使用上面所示的带有条件逻辑的一个模板,但您也可以为每种类型的块引用创建单独的模板:
layouts/
└── _default/
└── _markup/
├── render-blockquote-alert.html
└── render-blockquote-regular.html
PageInner 详情
New in v0.125.0PageInner
的主要用例是解析相对于已包含的 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
不相关,则 PageInner
回退到 Page
的值,并始终返回值。
作为一个实际示例,Hugo的嵌入式链接和图像渲染钩子使用 PageInner
方法来解析Markdown链接和图像目标。查看每个源代码: