Debian Codegraph @

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

raw
1 ---
2 title: Heading render hooks
3 linkTitle: Headings
4 description: Create heading render hook templates to override the rendering of Markdown headings to HTML.
5 categories: []
6 keywords: []
7 ---
8
9 ## Context
10
11 Heading _render hook_ templates receive the following [context](g):
12
13 `Anchor`
14 : (`string`) The `id` attribute of the heading element.
15
16 `Attributes`
17 : (`map`) The [Markdown attributes][], available if you configure your site as follows:
18
19 {{< code-toggle file=hugo >}}
20 [markup.goldmark.parser.attribute]
21 title = true
22 {{< /code-toggle >}}
23
24 `Level`
25 : (`int`) The heading level, 1 through 6.
26
27 `Ordinal`
28 : {{< new-in 0.160.0 />}}
29 : (`int`) The zero-based ordinal of the heading on the page.
30
31 `Page`
32 : (`page`) A reference to the current page.
33
34 `PageInner`
35 : (`page`) A reference to a page nested via the [`RenderShortcodes`][] method. [See details](#pageinner-details).
36
37 `PlainText`
38 : (`string`) The heading text as plain text.
39
40 `Position`
41 : {{< new-in 0.160.0 />}}
42 : (`string`) The position of the heading within the page content.
43
44 `Text`
45 : (`template.HTML`) The heading text.
46
47 ## Examples
48
49 In its default configuration, Hugo renders Markdown headings according to the [CommonMark][] specification with the addition of automatic `id` attributes. To create a render hook that does the same thing:
50
51 ```go-html-template {file="layouts/_markup/render-heading.html" copy=true}
52 <h{{ .Level }} id="{{ .Anchor }}" {{- with .Attributes.class }} class="{{ . }}" {{- end }}>
53 {{- .Text -}}
54 </h{{ .Level }}>
55 ```
56
57 To add an anchor link to the right of each heading:
58
59 ```go-html-template {file="layouts/_markup/render-heading.html" copy=true}
60 <h{{ .Level }} id="{{ .Anchor }}" {{- with .Attributes.class }} class="{{ . }}" {{- end }}>
61 {{ .Text }}
62 <a href="#{{ .Anchor }}">#</a>
63 </h{{ .Level }}>
64 ```
65
66 {{% include "/_common/render-hooks/pageinner.md" %}}
67
68 [CommonMark]: https://spec.commonmark.org/current/
69 [Markdown attributes]: /content-management/markdown-attributes/
70 [`RenderShortcodes`]: /methods/page/rendershortcodes/