Debian Codegraph @

docs/content/en/render-hooks/passthrough.md

raw
1 ---
2 title: Passthrough render hooks
3 linkTitle: Passthrough
4 description: Create passthrough render hook templates to override the rendering of text snippets captured by the Goldmark Passthrough extension.
5 categories: []
6 keywords: []
7 ---
8
9 ## Overview
10
11 Hugo uses [Goldmark][] to render Markdown to HTML. Goldmark supports custom extensions to extend its core functionality. The [Passthrough][] extension captures and preserves raw Markdown within delimited snippets of text, including the delimiters themselves. These are known as _passthrough elements_.
12
13 Depending on your choice of delimiters, Hugo will classify a passthrough element as either _block_ or _inline_. Consider this contrived example:
14
15 ```md {file="content/example.md"}
16 This is a
17
18 \[block\]
19
20 passthrough element with opening and closing block delimiters.
21
22 This is an \(inline\) passthrough element with opening and closing inline delimiters.
23 ```
24
25 Update your project configuration to enable the Passthrough extension and define opening and closing delimiters for each passthrough element type, either `block` or `inline`. For example:
26
27 {{< code-toggle file=hugo >}}
28 [markup.goldmark.extensions.passthrough]
29 enable = true
30 [markup.goldmark.extensions.passthrough.delimiters]
31 block = [['\[', '\]'], ['$$', '$$']]
32 inline = [['\(', '\)']]
33 {{< /code-toggle >}}
34
35 In the example above there are two sets of `block` delimiters. You may use either one in your Markdown.
36
37 The Passthrough extension is often used in conjunction with the MathJax or KaTeX display engine to render [mathematical expressions][] written in the LaTeX markup language.
38
39 To enable custom rendering of passthrough elements, create a passthrough render hook.
40
41 ## Context
42
43 Passthrough _render hook_ templates receive the following [context](g):
44
45 `Attributes`
46 : (`map`) The [Markdown attributes][], available if you configure your site as follows:
47
48 {{< code-toggle file=hugo >}}
49 [markup.goldmark.parser.attribute]
50 block = true
51 {{< /code-toggle >}}
52
53 Hugo populates the `Attributes` map for _block_ passthrough elements. Markdown attributes are not applicable to _inline_ elements.
54
55 `Inner`
56 : (`string`) The inner content of the passthrough element, excluding the delimiters.
57
58 `Ordinal`
59 : (`int`) The zero-based ordinal of the passthrough element on the page.
60
61 `Page`
62 : (`page`) A reference to the current page.
63
64 `PageInner`
65 : (`page`) A reference to a page nested via the [`RenderShortcodes`][] method. [See details](#pageinner-details).
66
67 `Position`
68 : (`string`) The position of the passthrough element within the page content.
69
70 `Type`
71 : (`string`) The passthrough element type, either `block` or `inline`.
72
73 ## Example
74
75 Instead of client-side JavaScript rendering of mathematical markup using MathJax or KaTeX, create a passthrough render hook which calls the [`transform.ToMath`][] function.
76
77 ```go-html-template {file="layouts/_markup/render-passthrough.html" copy=true}
78 {{- $opts := dict "output" "htmlAndMathml" "displayMode" (eq .Type "block") }}
79 {{- with try (transform.ToMath .Inner $opts) }}
80 {{- with .Err }}
81 {{- 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 }}
82 {{- else }}
83 {{- .Value }}
84 {{- $.Page.Store.Set "hasMath" true }}
85 {{- end }}
86 {{- end -}}
87 ```
88
89 Then, in your _base_ template, conditionally include the KaTeX CSS within the head element:
90
91 ```go-html-template {file="layouts/baseof.html" copy=true}
92 <head>
93 {{ $noop := .WordCount }}
94 {{ if .Page.Store.Get "hasMath" }}
95 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-vlBdW0r3AcZO/HboRPznQNowvexd3fY8qHOWkBi5q7KGgqJ+F48+DceybYmrVbmB" crossorigin="anonymous">
96 {{ end }}
97 </head>
98 ```
99
100 In the above, note the use of a [noop](g) statement to force content rendering before we check the value of `hasMath` with the `Store.Get` method.
101
102 Although you can use one template with conditional logic as shown above, you can also create separate templates for each [`Type`](#type) of passthrough element:
103
104 ```tree
105 layouts/
106 └── _markup/
107 ├── render-passthrough-block.html
108 └── render-passthrough-inline.html
109 ```
110
111 {{% include "/_common/render-hooks/pageinner.md" %}}
112
113 [Goldmark]: https://github.com/yuin/goldmark
114 [Markdown attributes]: /content-management/markdown-attributes/
115 [Passthrough]: /configuration/markup/#passthrough
116 [`RenderShortcodes`]: /methods/page/rendershortcodes/
117 [`transform.ToMath`]: /functions/transform/tomath/
118 [mathematical expressions]: /content-management/mathematics/