From 49cc2d5bbd37c089f3cd5bfa3a990cc2c9e1f205 Mon Sep 17 00:00:00 2001
From: Yann Van Crombrugge <59556820+vancromy@users.noreply.github.com>
Date: Wed, 29 Jan 2025 08:50:12 +0000
Subject: [PATCH 1/3] feat: Add heading and toc_label settings
---
docs/usage/configuration/headings.md | 52 ++++++++++++++++---
src/mkdocstrings_handlers/python/handler.py | 4 ++
.../material/_base/attribute.html.jinja | 16 +++---
.../templates/material/_base/class.html.jinja | 24 ++++-----
.../_base/docstring/parameters.html.jinja | 2 +-
.../material/_base/function.html.jinja | 18 +++----
.../material/_base/module.html.jinja | 20 +++----
7 files changed, 88 insertions(+), 48 deletions(-)
diff --git a/docs/usage/configuration/headings.md b/docs/usage/configuration/headings.md
index 467779e4..c6d94f3e 100644
--- a/docs/usage/configuration/headings.md
+++ b/docs/usage/configuration/headings.md
@@ -57,6 +57,42 @@ plugins:
////
///
+## `heading`
+
+- **:octicons-package-24: Type [`str`][] :material-equal: `""`{ title="default value" }**
+
+
+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.
+
+!!! warning "Not advised to be used as a general configuration option"
+ 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.
+
+```md title="in docs/some_page.md (local configuration)"
+::: path.to.module
+ options:
+ heading: "My fancy module"
+```
+
+## `toc_label`
+
+- **:octicons-package-24: Type [`str`][] :material-equal: `""`{ title="default value" }**
+
+
+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.
+
+!!! warning "Not advised to be used as a general configuration option"
+ 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.
+
+!!! 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.
+
+```md title="in docs/some_page.md (local configuration)"
+::: path.to.module
+ options:
+ heading: "My fancy module"
+ toc_label: "My fancy module"
+```
+
## `parameter_headings`
[:octicons-tag-24: Insiders 1.6.0](../../insiders/changelog.md#1.6.0)
@@ -286,15 +322,15 @@ More text.
type: preview
//// tab | With ToC entry
-**Table of contents**
-[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
-[`object`](#permalink-to-object){ title="#permalink-to-object" }
-[Other heading](#permalink-to-other-heading){ title="#permalink-to-other-heading" }
+**Table of contents**
+[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
+[`object`](#permalink-to-object){ title="#permalink-to-object" }
+[Other heading](#permalink-to-other-heading){ title="#permalink-to-other-heading" }
////
//// tab | Without ToC entry
-**Table of contents**
-[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
+**Table of contents**
+[Some heading](#permalink-to-some-heading){ title="#permalink-to-some-heading" }
[Other heading](#permalink-to-other-heading){ title="#permalink-to-other-heading" }
////
///
@@ -400,7 +436,7 @@ plugins:
Show the full Python path of every object.
Same as for [`show_root_members_full_path`][],
-but for every member, recursively. This option takes precedence over
+but for every member, recursively. This option takes precedence over
[`show_root_members_full_path`][]:
`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
These category headings will appear in the table of contents,
allowing you to link to them using their permalinks.
-WARNING: **Not recommended with deeply nested object**
+WARNING: **Not recommended with deeply nested object**
When injecting documentation for deeply nested objects,
you'll quickly run out of heading levels, and the objects
at the bottom of the tree risk all getting documented
diff --git a/src/mkdocstrings_handlers/python/handler.py b/src/mkdocstrings_handlers/python/handler.py
index 4171fd76..c3fb83be 100644
--- a/src/mkdocstrings_handlers/python/handler.py
+++ b/src/mkdocstrings_handlers/python/handler.py
@@ -107,6 +107,8 @@ class PythonHandler(BaseHandler):
"show_inheritance_diagram": False,
"show_submodules": False,
"group_by_category": True,
+ "heading": "",
+ "toc_label": "",
"heading_level": 2,
"members_order": rendering.Order.alphabetical.value,
"docstring_section_style": "table",
@@ -141,6 +143,8 @@ class PythonHandler(BaseHandler):
The modules must be listed as an array of strings. Default: `None`.
Attributes: Headings options:
+ heading (str): A custom string to override the autogenerated heading of the root object
+ toc_label (str): A custom string to override the autogenerated toc label of the root object
heading_level (int): The initial heading level to use. Default: `2`.
parameter_headings (bool): Whether to render headings for parameters (therefore showing parameters in the ToC). Default: `False`.
show_root_heading (bool): Show the heading of the object at the root of the documentation tree
diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja
index 7f3707b6..c13bb641 100644
--- a/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja
+++ b/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja
@@ -12,7 +12,7 @@ Context:
{% block logs scoped %}
{#- Logging block.
-
+
This block can be used to log debug messages, deprecation messages, warnings, etc.
-#}
{{ log.debug("Rendering " + attribute.path) }}
@@ -44,12 +44,12 @@ Context:
{% block heading scoped %}
{#- Heading block.
-
+
This block renders the heading for the attribute.
-#}
{% if config.show_symbol_type_heading %}{% endif %}
{% if config.separate_signature %}
- {{ attribute_name }}
+ {{ config.heading if config.heading and root else attribute_name }}
{% else %}
{%+ filter highlight(language="python", inline=True) %}
{{ attribute_name }}{% if attribute.annotation and config.show_signature_annotations %}: {{ attribute.annotation }}{% endif %}
@@ -60,7 +60,7 @@ Context:
{% block labels scoped %}
{#- Labels block.
-
+
This block renders the labels for the attribute.
-#}
{% with labels = attribute.labels %}
@@ -72,7 +72,7 @@ Context:
{% block signature scoped %}
{#- Signature block.
-
+
This block renders the signature for the attribute.
-#}
{% if config.separate_signature %}
@@ -88,7 +88,7 @@ Context:
{% filter heading(heading_level,
role="data" if attribute.parent.kind.value == "module" else "attr",
id=html_id,
- toc_label=('
'|safe if config.show_symbol_type_toc else '') + attribute.name,
+ toc_label=('
'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else attribute_name),
hidden=True,
) %}
{% endfilter %}
@@ -99,14 +99,14 @@ Context:
{% endif %}
{% if config.separate_signature %}
- {{ class_name }}
+ {{ config.heading if config.heading and root else class_name }}
{% elif config.merge_init_into_class and "__init__" in all_members %}
{% with function = all_members["__init__"] %}
{%+ filter highlight(language="python", inline=True) %}
@@ -62,7 +62,7 @@ Context:
{% block labels scoped %}
{#- Labels block.
-
+
This block renders the labels for the class.
-#}
{% with labels = class.labels %}
@@ -74,7 +74,7 @@ Context:
{% block signature scoped %}
{#- Signature block.
-
+
This block renders the signature for the class.
Overloads of the `__init__` method are rendered if `merge_init_into_class` is enabled.
The actual `__init__` method signature is only rendered if `separate_signature` is also enabled.
@@ -106,7 +106,7 @@ Context:
{% filter heading(heading_level,
role="class",
id=html_id,
- toc_label=('
'|safe if config.show_symbol_type_toc else '') + class.name,
+ toc_label=('
'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else class.name),
hidden=True,
) %}
{% endfilter %}
@@ -117,14 +117,14 @@ Context:
{% endif %}
{% if config.separate_signature %}
- {{ function_name }}
+ {{ config.heading if config.heading and root else function_name }}
{% else %}
{%+ filter highlight(language="python", inline=True) %}
{{ function_name }}{% include "signature"|get_template with context %}
@@ -64,7 +64,7 @@ Context:
{% block labels scoped %}
{#- Labels block.
-
+
This block renders the labels for the function.
-#}
{% with labels = function.labels %}
@@ -76,7 +76,7 @@ Context:
{% block signature scoped %}
{#- Signature block.
-
+
This block renders the signature for the function,
as well as its overloaded signatures if any.
-#}
@@ -103,7 +103,7 @@ Context:
heading_level,
role="function",
id=html_id,
- toc_label=(('
')|safe if config.show_symbol_type_toc else '') + function.name,
+ toc_label=(('
')|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else function.name),
hidden=True,
) %}
{% endfilter %}
@@ -114,14 +114,14 @@ Context:
'|safe if config.show_symbol_type_toc else '') + module.name,
+ toc_label=('
'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else module.name),
) %}
{% block heading scoped %}
{#- Heading block.
-
+
This block renders the heading for the module.
-#}
{% if config.show_symbol_type_heading %}
{% endif %}
{% if config.separate_signature %}
- {{ module_name }}
+ {{ config.heading if config.heading and root else module_name }}
{% else %}
{{ module_name }}
{% endif %}
@@ -56,7 +56,7 @@ Context:
{% block labels scoped %}
{#- Labels block.
-
+
This block renders the labels for the module.
-#}
{% with labels = module.labels %}
@@ -71,7 +71,7 @@ Context:
{% filter heading(heading_level,
role="module",
id=html_id,
- toc_label=('
'|safe if config.show_symbol_type_toc else '') + module.name,
+ toc_label=('
'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else module.name),
hidden=True,
) %}
{% endfilter %}
@@ -82,14 +82,14 @@ Context: