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

Passthrough 渲染钩子

创建一个 passthrough 渲染钩子来覆盖由 Goldmark passthrough 扩展捕获的文本片段的渲染。
New in v0.132.0

概述

Hugo 使用 Goldmark 将 Markdown 渲染为 HTML。Goldmark 支持自定义扩展来扩展其核心功能。Goldmark 的 passthrough 扩展 捕获并保留分隔文本片段内的原始 Markdown,包括分隔符本身。这些被称为 passthrough 元素。

根据您选择的定界符,Hugo 将 passthrough 元素分类为 块 或 内联 。考虑这个人为编造的例子:

content/sample.md
这是一个

\[block\]

带有开始和结束块定界符的 passthrough 元素。

这是一个 \(inline\) 带有开始和结束内联定界符的 passthrough 元素。

更新您的站点配置以启用 passthrough 扩展并为每种 passthrough 元素类型( block 或 inline )定义开始和结束定界符。例如:

hugo.
     
markup:
  goldmark:
    extensions:
      passthrough:
        delimiters:
          block:
          - - \[
            - \]
          - - $$
            - $$
          inline:
          - - \(
            - \)
        enable: true
[markup]
  [markup.goldmark]
    [markup.goldmark.extensions]
      [markup.goldmark.extensions.passthrough]
        enable = true
        [markup.goldmark.extensions.passthrough.delimiters]
          block = [['\[', '\]'], ['$$', '$$']]
          inline = [['\(', '\)']]
{
   "markup": {
      "goldmark": {
         "extensions": {
            "passthrough": {
               "delimiters": {
                  "block": [
                     [
                        "\\[",
                        "\\]"
                     ],
                     [
                        "$$",
                        "$$"
                     ]
                  ],
                  "inline": [
                     [
                        "\\(",
                        "\\)"
                     ]
                  ]
               },
               "enable": true
            }
         }
      }
   }
}

在上面的示例中,有两组 block 定界符。您可以在 Markdown 中使用任何一个。

Goldmark passthrough 扩展通常与 MathJax 或 KaTeX 显示引擎一起使用,以渲染用 LaTeX 或 Tex 编写的 数学表达式 。

要启用 passthrough 元素的自定义渲染,请创建一个渲染钩子。

上下文

Passthrough 渲染钩子模板接收以下 [上下文]:

属性

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

Hugo 为 块 passthrough 元素填充 Attributes 映射。Markdown 属性不适用于 内联 元素。

内部内容

(string) passthrough 元素的内部内容,不包括定界符。

序号

(int) 页面上 passthrough 元素的基于零的序号。

页面

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

页面内部内容

(page) 对通过 RenderShortcodes 方法嵌套的页面的引用。 查看详情 。

位置

(string) passthrough 元素在页面内容中的位置。

类型

(string) passthrough 元素类型, block 或 inline 。

示例

作为使用 MathJax 或 KaTeX 显示引擎渲染数学表达式的替代方法,创建一个 passthrough 渲染钩子,该钩子调用 transform.ToMath 函数:

layouts/_default/_markup/render-passthrough.html
{{ if eq .Type "block" }}
  {{ $opts := dict "displayMode" true }}
  {{ transform.ToMath .Inner $opts }}
{{ else }}
  {{ transform.ToMath .Inner }}
{{ end }}

尽管您可以像上面那样使用一个带有条件逻辑的模板,但您也可以为每种 Type 的 passthrough 元素创建单独的模板:

layouts/
└── _default/
    └── _markup/
        ├── render-passthrough-block.html
        └── render-passthrough-inline.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

  • 代码块
  • 块引用
  • 表格
  • 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 管道
  • 命令行界面
  • 故障排除
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护