Debian Codegraph @

docs/content/en/content-management/markdown-attributes.md

raw
1 ---
2 title: Markdown attributes
3 description: Use Markdown attributes to add HTML attributes when rendering Markdown to HTML.
4 categories: []
5 keywords: []
6 ---
7
8 ## Overview
9
10 Hugo supports Markdown attributes on images and block elements including blockquotes, fenced code blocks, headings, horizontal rules, lists, paragraphs, and tables.
11
12 For example:
13
14 ```md
15 This is a paragraph.
16 {class="foo bar" id="baz"}
17 ```
18
19 With `class` and `id` attributes you can also use short-form notation:
20
21 ```md
22 This is a paragraph.
23 {.foo .bar #baz}
24 ```
25
26 Hugo renders both of the examples above to:
27
28 ```html
29 <p class="foo bar" id="baz">This is a paragraph.</p>
30 ```
31
32 With `class` and `id` attributes, whether you use long-form or short-form notation, the resulting values are available in [render hook templates][] via the `Attributes` method. For example:
33
34 ```go-html-template
35 {{ .Attributes.class }} → foo bar
36 {{ .Attributes.id }} → baz
37 ```
38
39 ## Block elements
40
41 Update your project configuration to enable Markdown attributes for block-level elements.
42
43 {{< code-toggle file=hugo >}}
44 [markup.goldmark.parser.attribute]
45 title = true # default is true
46 block = true # default is false
47 {{< /code-toggle >}}
48
49 ## Standalone images
50
51 By default, when the [Goldmark][] Markdown renderer encounters a standalone image element (no other elements or text on the same line), it wraps the image element within a paragraph element per the [CommonMark][] specification.
52
53 If you were to place an attribute list beneath an image element, Hugo would apply the attributes to the surrounding paragraph, not the image.
54
55 To apply attributes to a standalone image element, you must disable the default wrapping behavior:
56
57 {{< code-toggle file=hugo >}}
58 [markup.goldmark.parser]
59 wrapStandAloneImageWithinParagraph = false # default is true
60 {{< /code-toggle >}}
61
62 ## Usage
63
64 You may add [global HTML attributes][], or HTML attributes specific to the current element type. Consistent with its content security model, Hugo removes HTML event attributes such as `onclick` and `onmouseover`.
65
66 > [!NOTE]
67 > Within fenced code blocks, Hugo interprets the `style` attribute as a syntax highlighting [option][option] rather than a global HTML attribute.
68
69 The attribute list consists of one or more key-value pairs, separated by spaces or commas, wrapped by braces. You must quote string values that contain spaces. Unlike HTML, boolean attributes must have both key and value.
70
71 For example:
72
73 ```md
74 > This is a blockquote.
75 {class="foo bar" hidden=hidden}
76 ```
77
78 Hugo renders this to:
79
80 ```html
81 <blockquote class="foo bar" hidden="hidden">
82 <p>This is a blockquote.</p>
83 </blockquote>
84 ```
85
86 In most cases, place the attribute list beneath the markup element. For headings and fenced code blocks, place the attribute list on the right.
87
88 Element | Position of attribute list
89 :-----------------|:--------------------------
90 blockquote | bottom
91 fenced code block | right
92 heading | right
93 horizontal rule | bottom
94 image | bottom
95 list | bottom
96 paragraph | bottom
97 table | bottom
98
99 For example:
100
101 ````md
102 ## Section 1 {class=foo}
103
104 ```sh {class=foo linenos=inline}
105 declare a=1
106 echo "${a}"
107 ```
108
109 This is a paragraph.
110 {class=foo}
111 ````
112
113 As shown above, the attribute list for fenced code blocks is not limited to HTML attributes. You can also configure syntax highlighting by passing one or more of [these options][option].
114
115 [CommonMark]: https://spec.commonmark.org/current/
116 [Goldmark]: https://github.com/yuin/goldmark
117 [global HTML attributes]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes
118 [option]: /functions/transform/highlight/#options
119 [render hook templates]: /render-hooks/introduction/