Debian Codegraph @

docs/extensions/attr_list.md

raw
1 title: Attribute Lists Extension
2
3 # Attribute Lists
4
5 ## Summary
6
7 The Attribute Lists extension adds a syntax to define attributes on the various
8 HTML elements in markdown's output.
9
10 This extension is included in the standard Markdown library.
11
12 !!! Note
13
14 This extension is in __maintenance mode__. We will continue to fix bugs
15 and keep it up-to-date with the core parser, but no new features or
16 changes in behavior will be made. If you need a feature that this
17 extension does not offer, then you have three options (1) find an
18 existing [third-party extension] which meets your needs, (2) [build your
19 own extension], or (3) fork this extension (pursuant to its licensing
20 requirements) and maintain it as a third-party extension.
21
22 [third-party extension]: index.md#third-party-extensions
23 [build your own extension]: api.md
24
25 ## Syntax
26
27 The basic syntax was inspired by Maruku's Attribute Lists feature (see [web archive][Maruku]).
28
29 [Maruku]: https://web.archive.org/web/20170324172643/http://maruku.rubyforge.org/proposal.html
30
31 ### The List
32
33 An example attribute list might look like this:
34
35 ```text
36 {: #someid .someclass somekey='some value' }
37 ```
38
39 A word which starts with a hash (`#`) will set the id of an element.
40
41 A word which starts with a dot (`.`) will be added to the list of classes
42 assigned to an element.
43
44 A key/value pair (`somekey='some value'`) will assign that pair to the element.
45
46 Be aware that while the dot syntax will add to a class, using key/value pairs
47 will always override the previously defined attribute. Consider the following:
48
49 ```text
50 {: #id1 .class1 id=id2 class="class2 class3" .class4 }
51 ```
52
53 The above example would result in the following attributes being defined:
54
55 ```text
56 id="id2" class="class2 class3 class4"
57 ```
58
59 HTML includes support for some attributes to be a single term, like `checked`, for example. Therefore, the attribute
60 list `{: checked }` would result in `checked` if the [output format](../reference.md#output_format) is `html` or
61 `checked="checked"` if the output format is `xhtml`.
62
63 Curly braces can be backslash escaped to avoid being identified as an attribute list.
64
65 ```text
66 \{ not an attribute list }
67 ```
68
69 Opening and closing curly braces which are empty or only contain whitespace are ignored whether they are escaped or
70 not. Additionally, any attribute lists which are not located in the specific locations documented below are ignored.
71
72 The colon after the opening brace is optional, but is supported to maintain consistency with other implementations.
73 Therefore, the following is also a valid attribute list:
74
75 ```text
76 { #someid .someclass somekey='some value' }
77 ```
78
79 In addition, the spaces after the opening brace and before the closing brace are optional. They are recommended as
80 they improve readability, but they are not required.
81
82 The Attribute List extension does not have any knowledge of which keys and/or values are valid in HTML. Therefore, it
83 is up to the document author to ensure that valid keys and values are used. However, the extension will escape any
84 characters in the key which are not valid by replacing them with an underscore. Multiple consecutive invalid
85 characters are reduced to a single underscore.
86
87 ### Block Level
88
89 To define attributes for a block level element, the attribute list should
90 be defined on the last line of the block by itself.
91
92 ```text
93 This is a paragraph.
94 {: #an_id .a_class }
95 ```
96
97 The above results in the following output:
98
99 ```html
100 <p id="an_id" class="a_class">This is a paragraph.</p>
101 ```
102
103 An exception is headers, as they are only ever allowed on one line.
104
105 ```text
106 A setext style header {: #setext}
107 =================================
108
109 ### A hash style header ### {: #hash }
110 ```
111
112 The above results in the following output:
113
114 ```html
115 <h1 id="setext">A setext style header</h1>
116 <h3 id="hash">A hash style header</h3>
117 ```
118
119 !!! seealso "See Also"
120 By default, the [Fenced Code Blocks](./fenced_code_blocks.md#attributes) extension includes limited support for
121 attribute lists. To get [full support](./fenced_code_blocks.md#keyvalue-pairs), both extensions must be enabled.
122
123 ### Inline
124
125 To define attributes on inline elements, the attribute list should be defined
126 immediately after the inline element with no white space.
127
128 ```text
129 [link](http://example.com){: class="foo bar" title="Some title!" }
130 ```
131
132 The above results in the following output:
133
134 ```html
135 <p><a href="http://example.com" class="foo bar" title="Some title!">link</a></p>
136 ```
137
138 If the [tables](./tables.md) extension is enabled, attribute lists can be defined on table cells. To differentiate
139 attributes for an inline element from attributes for the containing cell, the attribute list must be separated from
140 the content by at least one space and be defined at the end of the cell content. As table cells can only ever be on
141 a single line, the attribute list must remain on the same line as the content of the cell.
142
143 ```text
144 | set on td | set on em |
145 |--------------|-------------|
146 | *a* { .foo } | *b*{ .foo } |
147 ```
148
149 The above example results in the following output:
150
151 ```html
152 <table>
153 <thead>
154 <tr>
155 <th>set on td</th>
156 <th>set on em</th>
157 </tr>
158 </thead>
159 <tbody>
160 <tr>
161 <td class="foo"><em>a</em></td>
162 <td><em class="foo">b</em></td>
163 </tr>
164 </tbody>
165 </table>
166 ```
167
168 Note that in the first column, the attribute list is preceded by a space; therefore, it is assigned to the table cell
169 (`<td>` element). However, in the second column, the attribute list is not preceded by a space; therefore, it is
170 assigned to the inline element (`<em>`) which immediately preceded it.
171
172 Attribute lists may also be defined on table header cells (`<th>` elements) in the same manner.
173
174 ### Limitations
175
176 There are a few types of elements which attribute lists do not work with. As a reminder, Markdown is a subset of HTML
177 and anything which cannot be expressed in Markdown can always be expressed with raw HTML directly.
178
179 __Code Blocks:__
180
181 : Code blocks are unique in that they must be able to display Markdown syntax. Therefore, there is no way to
182 determine if an attribute list is intended to be part of the code block or intended to define attributes on the
183 wrapping element. For that reason, the extension ignores code blocks. To define attributes on code blocks, the
184 [codehilite] and [fenced code blocks] extensions provide some options.
185
186 [codehilite]: code_hilite.md
187 [fenced code blocks]: fenced_code_blocks.md
188
189 __Nested Elements:__
190
191 : Markdown provides mechanisms for nesting various block level elements within other elements. However, attribute
192 lists only ever get applied to the immediate parent. There is no way to specify that an attribute list should be
193 applied some number of levels up the document tree. For example, when including an attribute list within a
194 blockquote, the attribute list is only ever applied to the paragraph the list is defined in. There is no way to
195 define attributes on the `blockquote` element itself.
196
197 __Implied Elements:__
198
199 : There are various HTML elements which are not represented in Markdown text, but only implied. For example, the
200 `ul` and `ol` elements do not exist in Markdown. They are only implied by the presence of list items (`li`). There
201 is no way to use an attribute list to define attributes on implied elements, including but not limited to the
202 following: `ul`, `ol`, `dl`, `table`, `thead`, `tbody`, and `tr`.
203
204 ## Usage
205
206 See [Extensions](index.md) for general extension usage. Use `attr_list` as the
207 name of the extension.
208
209 This extension does not accept any special configuration options.
210
211 A trivial example:
212
213 ```python
214 markdown.markdown(some_text, extensions=['attr_list'])
215 ```