Skip to content

Commit 7cabacf

Browse files
authored
feat: Add heading and toc_label options
Issue-mkdocstrings-725: mkdocstrings/mkdocstrings#725 PR-236: #236
1 parent add0043 commit 7cabacf

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

docs/usage/configuration/headings.md

+33
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,39 @@ 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 object (i.e. the object specified directly after the identifier `:::`). This will override the default heading generated by the plugin.
66+
67+
WARNING: **Not advised to be used as a global configuration option.** This option is not advised to be used as a global 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.
68+
69+
```md title="in docs/some_page.md (local configuration)"
70+
::: path.to.module
71+
options:
72+
heading: "My fancy module"
73+
```
74+
75+
## `toc_label`
76+
77+
- **:octicons-package-24: Type [`str`][] :material-equal: `""`{ title="default value" }**
78+
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
79+
80+
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.
81+
82+
WARNING: **Not advised to be used as a global configuration option.** This option is not advised to be used as a global 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.
83+
84+
NOTE: **Use with/without `heading`.** 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.
85+
86+
```md title="in docs/some_page.md (local configuration)"
87+
::: path.to.module
88+
options:
89+
heading: "My fancy module"
90+
toc_label: "My fancy module"
91+
```
92+
6093
## `parameter_headings`
6194

6295
[:octicons-tag-24: Insiders 1.6.0](../../insiders/changelog.md#1.6.0)

src/mkdocstrings_handlers/python/handler.py

+4
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",
@@ -143,6 +145,8 @@ class PythonHandler(BaseHandler):
143145
The modules must be listed as an array of strings. Default: `None`.
144146
145147
Attributes: Headings options:
148+
heading (str): A custom string to override the autogenerated heading of the root object.
149+
toc_label (str): A custom string to override the autogenerated toc label of the root object.
146150
heading_level (int): The initial heading level to use. Default: `2`.
147151
parameter_headings (bool): Whether to render headings for parameters (therefore showing parameters in the ToC). Default: `False`.
148152
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Context:
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 %}
@@ -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 %}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Context:
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) %}
@@ -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 %}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Context:
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 %}
@@ -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 %}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Context:
3838
role="module",
3939
id=html_id,
4040
class="doc doc-heading",
41-
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-module"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + module.name,
41+
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-module"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else module.name),
4242
) %}
4343

4444
{% block heading scoped %}
@@ -48,7 +48,7 @@ Context:
4848
-#}
4949
{% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-module"></code>{% endif %}
5050
{% if config.separate_signature %}
51-
<span class="doc doc-object-name doc-module-name">{{ module_name }}</span>
51+
<span class="doc doc-object-name doc-module-name">{{ config.heading if config.heading and root else module_name }}</span>
5252
{% else %}
5353
<code>{{ module_name }}</code>
5454
{% endif %}
@@ -71,7 +71,7 @@ Context:
7171
{% filter heading(heading_level,
7272
role="module",
7373
id=html_id,
74-
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-module"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + module.name,
74+
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-module"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else module.name),
7575
hidden=True,
7676
) %}
7777
{% endfilter %}

0 commit comments

Comments
 (0)