Debian Codegraph @

docs/extensions/toc.md

raw
1 title: Table of Contents Extension
2
3 Table of Contents
4 =================
5
6 Summary
7 -------
8
9 The Table of Contents extension generates a Table of Contents from a Markdown
10 document and adds it into the resulting HTML document.
11
12 This extension is included in the standard Markdown library.
13
14 !!! Note
15
16 This extension is in __maintenance mode__. We will continue to fix bugs
17 and keep it up-to-date with the core parser, but no new features or
18 changes in behavior will be made. If you need a feature that this
19 extension does not offer, then you have three options (1) find an
20 existing [third-party extension] which meets your needs, (2) [build your
21 own extension], or (3) fork this extension (pursuant to its licensing
22 requirements) and maintain it as a third-party extension.
23
24 [third-party extension]: index.md#third-party-extensions
25 [build your own extension]: api.md
26
27 Syntax
28 ------
29
30 By default, all headers will automatically have unique `id` attributes
31 generated based upon the text of the header. Note this example, in which all
32 three headers would have the same `id`:
33
34 ```md
35 #Header
36 #Header
37 #Header
38 ```
39
40 Results in:
41
42 ```html
43 <h1 id="header">Header</h1>
44 <h1 id="header_1">Header</h1>
45 <h1 id="header_2">Header</h1>
46 ```
47
48 Place a marker in the document where you would like the Table of Contents to
49 appear. Then, a nested list of all the headers in the document will replace the
50 marker. The marker defaults to `[TOC]` so the following document:
51
52 ```md
53 [TOC]
54
55 # Header 1
56
57 ## Header 2
58 ```
59
60 would generate the following output:
61
62 ```html
63 <div class="toc">
64 <ul>
65 <li>
66 <a href="#header-1">Header 1</a>
67 <ul>
68 <li><a href="#header-2">Header 2</a></li>
69 </ul>
70 </li>
71 </ul>
72 </div>
73 <h1 id="header-1">Header 1</h1>
74 <h2 id="header-2">Header 2</h2>
75 ```
76
77 Regardless of whether a `marker` is found in the document (or disabled), the
78 Table of Contents is available as an attribute (`toc`) on the Markdown class.
79 This allows one to insert the Table of Contents elsewhere in their page
80 template. For example:
81
82 ```pycon
83 >>> md = markdown.Markdown(extensions=['toc'])
84 >>> html = md.convert(text)
85 >>> page = render_some_template(context={'body': html, 'toc': md.toc})
86 ```
87
88 The `toc_tokens` attribute is also available on the Markdown class and contains
89 a nested list of dict objects. For example, the above document would result in
90 the following object at `md.toc_tokens`:
91
92 ```python
93 [
94 {
95 'level': 1,
96 'id': 'header-1',
97 'name': 'Header 1',
98 'html': 'Header 1',
99 'data-toc-label': '',
100 'children': [
101 {'level': 2, 'id': 'header-2', 'name': 'Header 2', 'children':[]}
102 ]
103 }
104 ]
105 ```
106
107 Note that the `level` refers to the `hn` level. In other words, `<h1>` is level
108 `1` and `<h2>` is level `2`, etc. Be aware that improperly nested levels in the
109 input may result in odd nesting of the output.
110
111 `name` is the sanitized value which would also be used as a label for the HTML
112 version of the Table of Contents. `html` contains the fully rendered HTML
113 content of the heading and has not been sanitized in any way. This may be used
114 with your own custom sanitation to create custom table of contents.
115
116 ### Custom Labels
117
118 In most cases, the text label in the Table of Contents should match the text of
119 the header. However, occasionally that is not desirable. In that case, if this
120 extension is used in conjunction with the [Attribute Lists Extension] and a
121 `data-toc-label` attribute is defined on the header, then the contents of that
122 attribute will be used as the text label for the item in the Table of Contents.
123 For example, the following Markdown:
124
125 [Attribute Lists Extension]: attr_list.md
126
127 ```md
128 [TOC]
129
130 # Functions
131
132 ## `markdown.markdown(text [, **kwargs])` { #markdown data-toc-label='markdown.markdown' }
133 ```
134 would generate the following output:
135
136 ```html
137 <div class="toc">
138 <ul>
139 <li>
140 <a href="#functions">Functions</a>
141 <ul>
142 <li><a href="#markdown">markdown.markdown</a></li>
143 </ul>
144 </li>
145 </ul>
146 </div>
147 <h1 id="functions">Functions</h1>
148 <h2 id="markdown"><code>markdown.markdown(text [, **kwargs])</code></h2>
149 ```
150
151 Notice that the text in the Table of Contents is much cleaner and easier to read
152 in the context of a Table of Contents. The `data-toc-label` is not included in
153 the HTML header element. Also note that the ID was manually defined in the
154 attribute list to provide a cleaner URL when linking to the header. If the ID is
155 not manually defined, it is always derived from the text of the header, never
156 from the `data-toc-label` attribute.
157
158 The value of the `data-toc-label` attribute is sanitized and stripped of any HTML
159 tags. However, `toc_tokens` will contain the raw content under
160 `data-toc-label`.
161
162 Usage
163 -----
164
165 See [Extensions](index.md) for general extension usage. Use `toc` as the name
166 of the extension.
167
168 See the [Library Reference](../reference.md#extensions) for information about
169 configuring extensions.
170
171 The following options are provided to configure the output:
172
173 * **`marker`**:
174 Text to find and replace with the Table of Contents. Defaults to `[TOC]`.
175
176 Set to an empty string to disable searching for a marker, which may save
177 some time, especially on long documents.
178
179 * **`title`**:
180 Title to insert in the Table of Contents' `<div>`. Defaults to `None`.
181
182 * **`title_class`**:
183 CSS class used for the title contained in the Table of Contents. Defaults to `toctitle`.
184
185 * **`toc_class`**:
186 CSS class(es) used for the `<div>` containing the Table of Contents. Defaults to `toc`.
187
188 * **`anchorlink`**:
189 Set to `True` to cause all headers to link to themselves. Default is `False`.
190
191 * **`anchorlink_class`**:
192 CSS class(es) used for the link. Defaults to `toclink`.
193
194 * **`permalink`**:
195 Set to `True` or a string to generate permanent links at the end of each header.
196 Useful with Sphinx style sheets.
197
198 When set to `True` the paragraph symbol (&para; or "`&para;`") is used as
199 the link text. When set to a string, the provided string is used as the link
200 text.
201
202 * **`permalink_class`**:
203 CSS class(es) used for the link. Defaults to `headerlink`.
204
205 * **`permalink_title`**:
206 Title attribute of the permanent link. Defaults to `Permanent link`.
207
208 * **`permalink_leading`**:
209 Set to `True` if the extension should generate leading permanent links.
210 Default is `False`.
211
212 Leading permanent links are placed at the start of the header tag,
213 before any header content. The default `permalink` behavior (when
214 `permalink_leading` is unset or set to `False`) creates trailing
215 permanent links, which are placed at the end of the header content.
216
217 * **`baselevel`**:
218 Base level for headers. Defaults to `1`.
219
220 The `baselevel` setting allows the header levels to be automatically
221 adjusted to fit within the hierarchy of your HTML templates. For example,
222 suppose the Markdown text for a page should not contain any headers higher
223 than level 3 (`<h3>`). The following will accomplish that:
224
225 :::pycon
226 >>> text = '''
227 ... #Some Header
228 ... ## Next Level'''
229 >>> from markdown.extensions.toc import TocExtension
230 >>> html = markdown.markdown(text, extensions=[TocExtension(baselevel=3)])
231 >>> print html
232 <h3 id="some_header">Some Header</h3>
233 <h4 id="next_level">Next Level</h4>'
234
235 * **`slugify`**:
236 Callable to generate anchors.
237
238 Default: `markdown.extensions.toc.slugify`
239
240 In order to use a different algorithm to define the id attributes, define and
241 pass in a callable which takes the following two arguments:
242
243 * `value`: The string to slugify.
244 * `separator`: The Word Separator.
245
246 The callable must return a string appropriate for use in HTML `id` attributes.
247
248 An alternate version of the default callable supporting Unicode strings is also
249 provided as `markdown.extensions.toc.slugify_unicode`.
250
251 * **`separator`**:
252 Word separator. Character which replaces white space in id. Defaults to "`-`".
253
254 * **`toc_depth`**
255 Define the range of section levels to include in the Table of Contents.
256 A single integer (`b`) defines the bottom section level (`<h1>..<hb>`) only.
257 A string consisting of two digits separated by a hyphen in between (`"2-5"`),
258 define the top (`t`) and the bottom (`b`) (`<ht>..<hb>`). Defaults to `6` (bottom).
259
260 When used with conjunction with `baselevel`, this parameter will not
261 take the fitted hierarchy from `baselevel` into account. That is, if
262 both `toc_depth` and `baselevel` are `3`, then only the highest level
263 will be present in the table. If you set `baselevel` to `3` and
264 `toc_depth` to `"2-6"`, the *first* headline will be `<h3>` and so still
265 included in the Table of Contents. To exclude this first level, you
266 have to set `toc_depth` to `"4-6"`.
267
268 A trivial example:
269
270 ```python
271 markdown.markdown(some_text, extensions=['toc'])
272 ```