Skip to content

Commit 49cc2d5

Browse files
committed
feat: Add heading and toc_label settings
1 parent 941d0e5 commit 49cc2d5

File tree

7 files changed

+88
-48
lines changed

7 files changed

+88
-48
lines changed

docs/usage/configuration/headings.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,42 @@ plugins:
5757
////
5858
///
5959
60+
## `heading`
61+
62+
- **:octicons-package-24: Type [`str`][] :material-equal: `""`{ title="default value" }**
63+
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
64+
65+
A custom string to use as the heading of the root of the object (i.e. the object specfied directly after the identifier `:::`). This will override the default heading generated by the plugin.
66+
67+
!!! warning "Not advised to be used as a general configuration option"
68+
This option is not advised to be used as a general configuration option, as it will override the default heading for all objects. It is recommended to use it only in specific cases where you want to override the heading for a specific object.
69+
70+
```md title="in docs/some_page.md (local configuration)"
71+
::: path.to.module
72+
options:
73+
heading: "My fancy module"
74+
```
75+
76+
## `toc_label`
77+
78+
- **:octicons-package-24: Type [`str`][] :material-equal: `""`{ title="default value" }**
79+
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
80+
81+
A custom string to use as the label in the Table of Contents for the root object (i.e. the one specified directly after the identifier `:::`). This will override the default label generated by the plugin.
82+
83+
!!! warning "Not advised to be used as a general configuration option"
84+
This option is not advised to be used as a general configuration option, as it will override the default label for all objects. It is recommended to use it only in specific cases where you want to override the label for a specific object.
85+
86+
!!! note "Use with/without `heading`"
87+
If you use this option without specifying a custom `heading`, the default heading will be used in the page, but the label in the Table of Contents will be the one you specified. By providing both an option for `heading` and `toc_label`, we leave the customization entirely up to you.
88+
89+
```md title="in docs/some_page.md (local configuration)"
90+
::: path.to.module
91+
options:
92+
heading: "My fancy module"
93+
toc_label: "My fancy module"
94+
```
95+
6096
## `parameter_headings`
6197

6298
[:octicons-tag-24: Insiders 1.6.0](../../insiders/changelog.md#1.6.0)
@@ -286,15 +322,15 @@ More text.
286322
type: preview
287323

288324
//// tab | With ToC entry
289-
**Table of contents**
290-
[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
291-
[`object`](#permalink-to-object){ title="#permalink-to-object" }
292-
[Other heading](#permalink-to-other-heading){ title="#permalink-to-other-heading" }
325+
**Table of contents**
326+
[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
327+
[`object`](#permalink-to-object){ title="#permalink-to-object" }
328+
[Other heading](#permalink-to-other-heading){ title="#permalink-to-other-heading" }
293329
////
294330

295331
//// tab | Without ToC entry
296-
**Table of contents**
297-
[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
332+
**Table of contents**
333+
[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
298334
[Other heading](#permalink-to-other-heading){ title="#permalink-to-other-heading" }
299335
////
300336
///
@@ -400,7 +436,7 @@ plugins:
400436
Show the full Python path of every object.
401437

402438
Same as for [`show_root_members_full_path`][],
403-
but for every member, recursively. This option takes precedence over
439+
but for every member, recursively. This option takes precedence over
404440
[`show_root_members_full_path`][]:
405441

406442
`show_root_members_full_path` | `show_object_full_path` | Direct root members path
@@ -454,7 +490,7 @@ When [grouped by categories][group_by_category], show a heading for each categor
454490
These category headings will appear in the table of contents,
455491
allowing you to link to them using their permalinks.
456492

457-
WARNING: **Not recommended with deeply nested object**
493+
WARNING: **Not recommended with deeply nested object**
458494
When injecting documentation for deeply nested objects,
459495
you'll quickly run out of heading levels, and the objects
460496
at the bottom of the tree risk all getting documented

src/mkdocstrings_handlers/python/handler.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class PythonHandler(BaseHandler):
107107
"show_inheritance_diagram": False,
108108
"show_submodules": False,
109109
"group_by_category": True,
110+
"heading": "",
111+
"toc_label": "",
110112
"heading_level": 2,
111113
"members_order": rendering.Order.alphabetical.value,
112114
"docstring_section_style": "table",
@@ -141,6 +143,8 @@ class PythonHandler(BaseHandler):
141143
The modules must be listed as an array of strings. Default: `None`.
142144
143145
Attributes: Headings options:
146+
heading (str): A custom string to override the autogenerated heading of the root object
147+
toc_label (str): A custom string to override the autogenerated toc label of the root object
144148
heading_level (int): The initial heading level to use. Default: `2`.
145149
parameter_headings (bool): Whether to render headings for parameters (therefore showing parameters in the ToC). Default: `False`.
146150
show_root_heading (bool): Show the heading of the object at the root of the documentation tree

src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Context:
1212

1313
{% block logs scoped %}
1414
{#- Logging block.
15-
15+
1616
This block can be used to log debug messages, deprecation messages, warnings, etc.
1717
-#}
1818
{{ log.debug("Rendering " + attribute.path) }}
@@ -44,12 +44,12 @@ Context:
4444

4545
{% block heading scoped %}
4646
{#- Heading block.
47-
47+
4848
This block renders the heading for the attribute.
4949
-#}
5050
{% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-attribute"></code>{% endif %}
5151
{% if config.separate_signature %}
52-
<span class="doc doc-object-name doc-attribute-name">{{ attribute_name }}</span>
52+
<span class="doc doc-object-name doc-attribute-name">{{ config.heading if config.heading and root else attribute_name }}</span>
5353
{% else %}
5454
{%+ filter highlight(language="python", inline=True) %}
5555
{{ attribute_name }}{% if attribute.annotation and config.show_signature_annotations %}: {{ attribute.annotation }}{% endif %}
@@ -60,7 +60,7 @@ Context:
6060

6161
{% block labels scoped %}
6262
{#- Labels block.
63-
63+
6464
This block renders the labels for the attribute.
6565
-#}
6666
{% with labels = attribute.labels %}
@@ -72,7 +72,7 @@ Context:
7272

7373
{% block signature scoped %}
7474
{#- Signature block.
75-
75+
7676
This block renders the signature for the attribute.
7777
-#}
7878
{% if config.separate_signature %}
@@ -88,7 +88,7 @@ Context:
8888
{% filter heading(heading_level,
8989
role="data" if attribute.parent.kind.value == "module" else "attr",
9090
id=html_id,
91-
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-attribute"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + attribute.name,
91+
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-attribute"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else attribute_name),
9292
hidden=True,
9393
) %}
9494
{% endfilter %}
@@ -99,14 +99,14 @@ Context:
9999
<div class="doc doc-contents {% if root %}first{% endif %}">
100100
{% block contents scoped %}
101101
{#- Contents block.
102-
102+
103103
This block renders the contents of the attribute.
104104
It contains other blocks that users can override.
105105
Overriding the contents block allows to rearrange the order of the blocks.
106106
-#}
107107
{% block docstring scoped %}
108108
{#- Docstring block.
109-
109+
110110
This block renders the docstring for the attribute.
111111
-#}
112112
{% with docstring_sections = attribute.docstring.parsed %}

src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Context:
1111

1212
{% block logs scoped %}
1313
{#- Logging block.
14-
14+
1515
This block can be used to log debug messages, deprecation messages, warnings, etc.
1616
-#}
1717
{{ log.debug("Rendering " + class.path) }}
@@ -43,12 +43,12 @@ Context:
4343

4444
{% block heading scoped %}
4545
{#- Heading block.
46-
46+
4747
This block renders the heading for the class.
4848
-#}
4949
{% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-class"></code>{% endif %}
5050
{% if config.separate_signature %}
51-
<span class="doc doc-object-name doc-class-name">{{ class_name }}</span>
51+
<span class="doc doc-object-name doc-class-name">{{ config.heading if config.heading and root else class_name }}</span>
5252
{% elif config.merge_init_into_class and "__init__" in all_members %}
5353
{% with function = all_members["__init__"] %}
5454
{%+ filter highlight(language="python", inline=True) %}
@@ -62,7 +62,7 @@ Context:
6262

6363
{% block labels scoped %}
6464
{#- Labels block.
65-
65+
6666
This block renders the labels for the class.
6767
-#}
6868
{% with labels = class.labels %}
@@ -74,7 +74,7 @@ Context:
7474

7575
{% block signature scoped %}
7676
{#- Signature block.
77-
77+
7878
This block renders the signature for the class.
7979
Overloads of the `__init__` method are rendered if `merge_init_into_class` is enabled.
8080
The actual `__init__` method signature is only rendered if `separate_signature` is also enabled.
@@ -106,7 +106,7 @@ Context:
106106
{% filter heading(heading_level,
107107
role="class",
108108
id=html_id,
109-
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-class"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + class.name,
109+
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-class"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else class.name),
110110
hidden=True,
111111
) %}
112112
{% endfilter %}
@@ -117,14 +117,14 @@ Context:
117117
<div class="doc doc-contents {% if root %}first{% endif %}">
118118
{% block contents scoped %}
119119
{#- Contents block.
120-
120+
121121
This block renders the contents of the class.
122122
It contains other blocks that users can override.
123123
Overriding the contents block allows to rearrange the order of the blocks.
124124
-#}
125125
{% block bases scoped %}
126126
{#- Class bases block.
127-
127+
128128
This block renders the bases for the class.
129129
-#}
130130
{% if config.show_bases and class.bases %}
@@ -138,7 +138,7 @@ Context:
138138

139139
{% block docstring scoped %}
140140
{#- Docstring block.
141-
141+
142142
This block renders the docstring for the class.
143143
-#}
144144
{% with docstring_sections = class.docstring.parsed %}
@@ -161,15 +161,15 @@ Context:
161161

162162
{% block summary scoped %}
163163
{#- Summary block.
164-
164+
165165
This block renders auto-summaries for classes, methods, and attributes.
166166
-#}
167167
{% include "summary"|get_template with context %}
168168
{% endblock summary %}
169169

170170
{% block source scoped %}
171171
{#- Source block.
172-
172+
173173
This block renders the source code for the class.
174174
-#}
175175
{% if config.show_source %}
@@ -205,7 +205,7 @@ Context:
205205

206206
{% block children scoped %}
207207
{#- Children block.
208-
208+
209209
This block renders the children (members) of the class.
210210
-#}
211211
{% set root = False %}

src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Context:
99

1010
{% block logs scoped %}
1111
{#- Logging block.
12-
12+
1313
This block can be used to log debug messages, deprecation messages, warnings, etc.
1414
-#}
1515
{{ log.debug("Rendering parameters section") }}

src/mkdocstrings_handlers/python/templates/material/_base/function.html.jinja

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Context:
1111

1212
{% block logs scoped %}
1313
{#- Logging block.
14-
14+
1515
This block can be used to log debug messages, deprecation messages, warnings, etc.
1616
-#}
1717
{{ log.debug("Rendering " + function.path) }}
@@ -49,12 +49,12 @@ Context:
4949

5050
{% block heading scoped %}
5151
{#- Heading block.
52-
52+
5353
This block renders the heading for the function.
5454
-#}
5555
{% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-{{ symbol_type }}"></code>{% endif %}
5656
{% if config.separate_signature %}
57-
<span class="doc doc-object-name doc-function-name">{{ function_name }}</span>
57+
<span class="doc doc-object-name doc-function-name">{{ config.heading if config.heading and root else function_name }}</span>
5858
{% else %}
5959
{%+ filter highlight(language="python", inline=True) %}
6060
{{ function_name }}{% include "signature"|get_template with context %}
@@ -64,7 +64,7 @@ Context:
6464

6565
{% block labels scoped %}
6666
{#- Labels block.
67-
67+
6868
This block renders the labels for the function.
6969
-#}
7070
{% with labels = function.labels %}
@@ -76,7 +76,7 @@ Context:
7676

7777
{% block signature scoped %}
7878
{#- Signature block.
79-
79+
8080
This block renders the signature for the function,
8181
as well as its overloaded signatures if any.
8282
-#}
@@ -103,7 +103,7 @@ Context:
103103
heading_level,
104104
role="function",
105105
id=html_id,
106-
toc_label=(('<code class="doc-symbol doc-symbol-toc doc-symbol-' + symbol_type + '"></code>&nbsp;')|safe if config.show_symbol_type_toc else '') + function.name,
106+
toc_label=(('<code class="doc-symbol doc-symbol-toc doc-symbol-' + symbol_type + '"></code>&nbsp;')|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else function.name),
107107
hidden=True,
108108
) %}
109109
{% endfilter %}
@@ -114,14 +114,14 @@ Context:
114114
<div class="doc doc-contents {% if root %}first{% endif %}">
115115
{% block contents scoped %}
116116
{#- Contents block.
117-
117+
118118
This block renders the contents of the function.
119119
It contains other blocks that users can override.
120120
Overriding the contents block allows to rearrange the order of the blocks.
121121
-#}
122122
{% block docstring scoped %}
123123
{#- Docstring block.
124-
124+
125125
This block renders the docstring for the function.
126126
-#}
127127
{% with docstring_sections = function.docstring.parsed %}
@@ -131,7 +131,7 @@ Context:
131131

132132
{% block source scoped %}
133133
{#- Source block.
134-
134+
135135
This block renders the source code for the function.
136136
-#}
137137
{% if config.show_source and function.source %}

0 commit comments

Comments
 (0)