HUGO 中文文档

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio Star
  • 关于
    • 本节内容
    • 简介
    • 特性
    • 隐私
    • 安全
    • 许可证
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 快速上手
    • 本节内容
    • 快速入门
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 术语表
    • 配置构建
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • Front matter (前置 matter)
    • 构建选项
    • 页面资源
    • 图片处理
    • 短代码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类法
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学公式
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 简介
    • 模板类型
    • 查找顺序
    • 基模板
    • 首页模板
    • 单个模板
    • 章节模板
    • 分类模板
    • 术语模板
    • 局部模板
    • 内容视图模板
    • 短代码模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 内嵌模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • css
    • fmt
    • go 模板
    • hugo
    • js
    • lang
    • openapi3
    • os
    • urls
    • 全局
    • 加密
    • 反射
    • 变形
    • 变换
    • 哈希
    • 图像
    • 图表函数
    • 字符串
    • 安全函数
    • 局部模板函数
    • 数学
    • 数据
    • 时间
    • 模板
    • 比较
    • 类型转换
    • 编码
    • 调试
    • 资源
    • 路径
    • 集合
  • 方法
    • 本节内容
    • Duration
    • Menu
    • Page
    • Pager
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
    • 菜单项
    • 页面
  • 渲染钩子
    • 本节内容
    • 简介
    • 块引用
    • 代码块
    • 标题
    • Images
    • 链接
    • Passthrough
    • 表格
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo 管道
    • 本节内容
    • 简介
    • 将 Sass 编译为 CSS
    • PostCSS
    • PostProcess
    • JavaScript 构建
    • 资源压缩
    • 连接资产
    • 指纹和 SRI 哈希
    • 从字符串创建资源
    • 从模板创建资源
  • 命令行界面
  • 故障排除
    • 本节内容
    • Audit
    • 日志记录
    • 检查
    • 弃用
    • 性能
    • FAQs
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • Search
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo 部署
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitLab Pages 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
    • 托管在 Azure 静态 Web 应用上
    • 托管在 GitHub Pages 上
    • 托管在 KeyCDN 上
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
RENDER HOOKS

块引用渲染钩子

创建块引用渲染钩子以覆盖 Markdown 块引用到 HTML 的渲染。
New in v0.132.0

上下文

块引用渲染钩子模板接收以下[上下文]:

警报类型

(string) 当 Type 为 alert 时适用,这是转换为小写的警报类型。参见下面的 警报 部分。

警报标题
New in v0.134.0

(template.HTML) 当 Type 为 alert 时适用,这是警报标题。参见下面的 警报 部分。

警报标志
New in v0.134.0

(string) 当 Type 为 alert 时适用,这是警报标志。通常用于指示警报是否可图形化折叠,它可以是 + 、 - 或空字符串之一。参见下面的 警报 部分。

属性

(map) Markdown 属性 ,如果您按如下方式配置您的站点,则可用:

hugo.
     
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 块引用。要创建执行相同操作的渲染钩子:

layouts/_default/_markup/render-blockquote.html
<blockquote>
  {{ .Text }}
</blockquote>

要将块引用渲染为带有可选引用和标题的 HTML figure 元素:

layouts/_default/_markup/render-blockquote.html
<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 语法,每个警报的第一行都是一个警报指示符,它由一个感叹号后跟警报类型组成,并用方括号括起来。例如:

content/example.md
> [!NOTE]
> 用户应该知道的有用信息,即使是浏览内容时也是如此。

> [!TIP]
> 有助于更好地或更轻松地做事的有用建议。

> [!IMPORTANT]
> 用户需要知道才能实现其目标的关键信息。

> [!WARNING]
> 需要用户立即注意以避免问题的紧急信息。

> [!CAUTION]
> 建议注意某些操作的风险或负面结果。

基本语法与 GitHub 、 Obsidian 和 Typora 兼容。

扩展语法

使用扩展的 Markdown 语法,您可以选择包含警报标志和/或警报标题。警报标志是 + 或 - 之一,通常用于指示警报是否可图形化折叠。例如:

content/example.md
> [!WARNING]+ Radiation hazard
> Do not approach or handle without protective gear.

扩展语法与 Obsidian 兼容。

扩展语法与 GitHub 或 Typora 不兼容。如果您包含警报标志或警报标题,这些应用程序会将 Markdown 渲染为块引用。

示例

此块引用渲染钩子会在存在警报指示符时渲染多语言警报,否则它会根据 CommonMark 规范渲染块引用。

layouts/_default/_markup/render-blockquote.html
{{ $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 文件中创建这些条目:

i18n/en.toml.
     
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.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 不相关,则 PageInner 回退到 Page 的值,并始终返回值。

PageInner 方法仅与调用 RenderShortcodes 方法的短代码相关,并且必须使用 {{%..%}} 表示法调用短代码。

作为一个实际示例,Hugo的嵌入式链接和图像渲染钩子使用 PageInner 方法来解析Markdown链接和图像目标。查看每个源代码:

  • 嵌入式链接渲染钩子
  • 嵌入式图像渲染钩子

See also

  • Passthrough
  • 代码块
  • 表格
  • Images
  • 标题

On this page

  • 上下文
  • 示例
  • 警报
  • PageInner 详情
Last updated: January 10, 2025: 添加 gtm 谷歌代码管理 (6220bf5)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays
 

Hugo Sponsors

Your Company?
 

The Hugo logos are copyright © Steve Francia 2013–2025.

The Hugo Gopher is based on an original work by Renée French.

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于
  • 安装
  • 快速上手
  • 快速参考
  • 内容管理
  • 模板
  • 函数
  • 方法
  • 渲染钩子
  • Hugo 模块
  • Hugo 管道
  • 命令行界面
  • 故障排除
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护