Passthrough render hooks
Usage
A passthrough render hook is a Markdown render hook that overrides the rendering of passthrough elements. Use it to convert the raw content of each element to HTML, such as rendering mathematical expressions written in the LaTeX markup language.
Hugo uses Goldmark to render Markdown to HTML. The Goldmark Passthrough extension captures and preserves raw Markdown within delimited snippets of text, including the delimiters themselves. These are known as passthrough elements. Without a passthrough render hook, Hugo renders passthrough elements unchanged.
Depending on your choice of delimiters, Hugo classifies a passthrough element as either block or inline. For example:
This is a
\[block\]
passthrough element with opening and closing block delimiters.
This is an \(inline\) passthrough element with opening and closing inline delimiters.To enable the Passthrough extension, update your project configuration and define the opening and closing delimiters for each passthrough element type, either block or inline. For example:
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
}
}
}
}
}
In the example above there are two sets of block delimiters. You can use either one in your Markdown.
The Passthrough extension is often used with the MathJax or KaTeX display engine to render mathematical expressions written in the LaTeX markup language.
Context
Passthrough render hook templates receive the following context:
Attributes- (
map) The Markdown attributes, available if you configure your project as follows: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 populates the
Attributesmap for block passthrough elements. Markdown attributes are not applicable to inline elements. Inner- (
string) The inner content of the passthrough element, excluding the delimiters. Ordinal- (
int) The zero-based ordinal of the passthrough element on the page. Page- (
page) A reference to the current page. PageInner- (
page) A reference to the nested page when rendering content inserted by theRenderShortcodesmethod. Otherwise, a reference to the current page. Position- (
text.Position) The position of the passthrough element within the page content. Calculating the position can be expensive, so call this method only when you log an error or warning. Type- (
string) The passthrough element type, eitherblockorinline.
Template selection
Hugo selects a passthrough render hook template from a _markup directory using the template lookup order. Name the template render-passthrough.html, and optionally include page kind, custom layout, output format, and language identifiers in the file name, such as render-passthrough.rss.xml. Place the _markup directory at any level of the layouts directory to limit the template to pages in a given section.
To use a different template for a given passthrough element Type, append the type to the base name. Hugo uses render-passthrough.html for all other types:
layouts/
└── _markup/
├── render-passthrough-block.html
└── render-passthrough.htmlExamples
To render mathematical markup to HTML when you build your project, instead of rendering it in the browser with MathJax or KaTeX, call the transform.ToMath function:
{{- $opts := dict "output" "htmlAndMathml" "displayMode" (eq .Type "block") }}
{{- with try (transform.ToMath .Inner $opts) }}
{{- with .Err }}
{{- errorf "Unable to render mathematical markup to HTML using the transform.ToMath function. The KaTeX display engine threw the following error: %s: see %s." . $.Position }}
{{- else }}
{{- .Value }}
{{- $.Page.Store.Set "hasMath" true }}
{{- end }}
{{- end -}}Then, in your base template, conditionally include the KaTeX CSS within the head element. The noop statement forces Hugo to render the content before the template checks the value of hasMath with the Store.Get method:
<head>
{{ $noop := .WordCount }}
{{ if .Page.Store.Get "hasMath" }}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.18.4/dist/katex.min.css" integrity="sha384-u1zONI5gPXUx0UKI62c75/zww972y0v2rSK5ZYlVdS6xEuWDeZWUI66v6t1gvlXJ" crossorigin="anonymous">
{{ end }}
</head>