HUGO

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio 加星
  • 关于
    • 本节内容
    • 介绍
    • 特性
    • 隐私
    • 安全
    • 许可证
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 入门
    • 本节内容
    • 快速开始
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 配置构建
    • 术语表
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • 前言
    • 构建选项
    • 页面资源
    • 图像处理
    • 短代码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类法
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 介绍
    • 模板类型
    • 查找顺序
    • 基础模板
    • 主页模板
    • 单页模板
    • 章节模板
    • 分类法模板
    • 术语模板
    • 局部模板
    • 内容视图模板
    • 短代码模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 嵌入式模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go 模板
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • 方法
    • 本节内容
    • Duration
    • 菜单
    • 菜单项
    • 页面
    • 分页器
    • 页面
    • 资源
    • 短代码
    • 站点
    • 分类法
    • 时间
  • 渲染钩子
    • 本节内容
    • 介绍
    • 引用
    • 代码块
    • 标题
    • 图像
    • 链接
    • 透传
    • 表格
  • 短代码
    • 本节内容
    • 评论
    • 详细信息
    • 图示
    • Gist
    • 高亮
    • Instagram
    • 参数
    • QR
    • Ref
    • Relref
    • Vimeo
    • X
    • YouTube
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo 管道
    • 本节内容
    • 介绍
    • 将 Sass 转译为 CSS
    • PostCSS
    • 后处理
    • JavaScript 构建
    • 资源压缩
    • 连接资源
    • 指纹和 SRI 哈希
    • 从字符串创建资源
    • 从模板创建资源
  • CLI
  • 问题排查
    • 本节内容
    • 审计
    • 日志
    • 检查
    • 弃用
    • 性能
    • 常见问题解答
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • 搜索
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo 部署
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Azure Static Web Apps 上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitHub Pages 上托管
    • 在 GitLab Pages 上托管
    • 在 KeyCDN 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
渲染钩子

引用渲染钩子

创建一个引用渲染钩子以覆盖 Markdown 引用到 HTML 的渲染。
v0.132.0 中的新增功能

上下文

引用渲染钩子模板接收以下上下文

AlertType

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

AlertTitle
v0.134.0 中的新增功能

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

AlertSign
v0.134.0 中的新增功能

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

Attributes

(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
            }
         }
      }
   }
}
Ordinal

(int) 页面上引用的从零开始的序号。

Page

(page) 对当前页面的引用。

PageInner

(page) 通过RenderShortcodes方法嵌套的页面的引用。查看详细信息。

Position

(string) 引用在页面内容中的位置。

Text

(template.HTML) 引用文本,如果Type为 alert,则排除第一行。请参阅下面的警报部分。

Type

(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.com.cn" caption="Some caption"}

警报

警报也称为标注或提示,是用于强调关键信息的块引用。

基本语法

使用基本的 Markdown 语法,每个警报的第一行都是一个警报指示符,由一个感叹号后跟警报类型组成,并用方括号括起来。例如

content/example.md
> [!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 语法,您可以选择性地包含警报符号和/或警报标题。警报符号是 + 或 - 之一,通常用于指示警报在图形上是否可折叠。例如

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"
}

尽管您可以像上面显示的那样使用一个带有条件逻辑的模板,但您也可以为每个 Type 的块引用创建单独的模板

layouts/
└── _default/
    └── _markup/
        ├── render-blockquote-alert.html
        └── render-blockquote-regular.html

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 的值,并且始终返回一个值。

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

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

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

另请参阅

  • 代码块
  • 透传
  • 表格
  • 图像
  • 标题

本页内容

  • 上下文
  • 示例
  • 警报
  • PageInner 详细信息
最后更新时间:2024 年 9 月 4 日: 更新 blockquotes.md (ccb1b97cb)
改进此页面
由 Hugo 作者
Hugo Logo
  • 提交问题
  • 获取帮助
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo 赞助商

您的公司?
 

Hugo 徽标的版权归 © Steve Francia 2013–2025 所有。

Hugo Gopher 基于 Renée French 的原创作品。

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于
  • 安装
  • 入门
  • 快速参考
  • 内容管理
  • 模板
  • 函数
  • 方法
  • 渲染钩子
  • 短代码
  • Hugo 模块
  • Hugo 管道
  • CLI
  • 问题排查
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护