Debian Codegraph @

docs/content/en/render-hooks/code-blocks.md

raw
1 ---
2 title: Code block render hooks
3 linkTitle: Code blocks
4 description: Create code block render hook templates to override the rendering of Markdown code blocks to HTML.
5 categories: []
6 keywords: []
7 ---
8
9 ## Markdown
10
11 This Markdown example contains a fenced code block:
12
13 ````md {file="content/example.md"}
14 ```sh {class="my-class" id="my-codeblock" lineNos=inline tabWidth=2}
15 declare a=1
16 echo "$a"
17 exit
18 ```
19 ````
20
21 A fenced code block consists of:
22
23 - A leading [code fence][]
24 - An optional [info string][]
25 - A code sample
26 - A trailing code fence
27
28 In the example above, the info string contains:
29
30 - The language of the code sample (the first word)
31 - An optional space-delimited or comma-delimited list of attributes (everything within braces)
32
33 The attributes in the info string can be generic attributes or highlighting options.
34
35 In the example above, the _generic attributes_ are `class` and `id`. In the absence of special handling within a code block render hook, Hugo adds each generic attribute to the HTML element surrounding the rendered code block. Consistent with its content security model, Hugo removes HTML event attributes such as `onclick` and `onmouseover`. Generic attributes are typically global HTML attributes, but you may include custom attributes as well.
36
37 In the example above, the _highlighting options_ are `lineNos` and `tabWidth`. Hugo renders the code sample using its built-in syntax highlighter. You can control the appearance of the rendered code by specifying one or more [highlighting options][].
38
39 > [!NOTE]
40 > Although `style` is a global HTML attribute, when used in an info string it is a highlighting option.
41
42 ## Context
43
44 Code block _render hook_ templates receive the following [context](g):
45
46 `Attributes`
47 : (`map`) The generic attributes from the info string.
48
49 `Inner`
50 : (`string`) The content between the leading and trailing code fences, excluding the info string.
51
52 `Options`
53 : (`map`) The highlighting options from the info string.
54
55 `Ordinal`
56 : (`int`) The zero-based ordinal of the code block on the page.
57
58 `Page`
59 : (`page`) A reference to the current page.
60
61 `PageInner`
62 : (`page`) A reference to a page nested via the [`RenderShortcodes`][] method. [See details](#pageinner-details).
63
64 `Position`
65 : (`text.Position`) The position of the code block within the page content.
66
67 `Type`
68 : (`string`) The first word of the info string, typically the code language.
69
70 ## Examples
71
72 By default, Hugo renders fenced code blocks using its built-in syntax highlighter and wraps the result. To create a render hook that does the same thing:
73
74 ```go-html-template {file="layouts/_markup/render-codeblock.html" copy=true}
75 {{ $result := transform.HighlightCodeBlock . }}
76 {{ $result.Wrapped }}
77 ```
78
79 To fall back to plain text when the language is not specified or not supported by the highlighter:
80
81 ```go-html-template {file="layouts/_markup/render-codeblock.html" copy=true}
82 {{- $opts := dict }}
83 {{- if not (transform.CanHighlight .Type) }}
84 {{- $opts = dict "type" "text" }}
85 {{- end }}
86 {{- $result := transform.HighlightCodeBlock . $opts }}
87 {{- $result.Wrapped }}
88 ```
89
90 Although you can use one template with conditional logic to control the behavior on a per-language basis, you can also create language-specific templates.
91
92 ```tree
93 layouts/
94 └── _markup/
95 ├── render-codeblock-mermaid.html
96 ├── render-codeblock-python.html
97 └── render-codeblock.html
98 ```
99
100 For example, to create a code block render hook to render [Mermaid][] diagrams:
101
102 ```go-html-template {file="layouts/_markup/render-codeblock-mermaid.html" copy=true}
103 <pre class="mermaid">
104 {{ .Inner | htmlEscape | safeHTML }}
105 </pre>
106 {{ .Page.Store.Set "hasMermaid" true }}
107 ```
108
109 Then include this snippet at the _bottom_ of your base template, before the closing `body` tag:
110
111 ```go-html-template {file="layouts/baseof.html" copy=true}
112 {{ if .Store.Get "hasMermaid" }}
113 <script type="module">
114 import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
115 mermaid.initialize({ startOnLoad: true });
116 </script>
117 {{ end }}
118 ```
119
120 See the [diagrams][] page for details.
121
122 ## Embedded
123
124 Hugo includes an [embedded code block render hook][] to render [GoAT diagrams][].
125
126 {{% include "/_common/render-hooks/pageinner.md" %}}
127
128 [GoAT diagrams]: /content-management/diagrams/#goat-diagrams-ascii
129 [Mermaid]: https://mermaid.js.org/
130 [`RenderShortcodes`]: /methods/page/rendershortcodes/
131 [code fence]: https://spec.commonmark.org/current/#code-fence
132 [diagrams]: /content-management/diagrams/#mermaid-diagrams
133 [embedded code block render hook]: <{{% eturl render-codeblock-goat %}}>
134 [highlighting options]: /functions/transform/highlight/#options
135 [info string]: https://spec.commonmark.org/current/#info-string