Skip to content

Commit 7951c93

Browse files
committed
👌 offset nested headers by current heading level
This makes it so nested headings (in blockquotes, admonitions, etc.) generates rubrics that are offset by the current heading level, meaning that: ```markdown # Title ## Sub-title > Quote: > # Title in quote > Content. ``` will generate a rubric level 3 (level 1 + offset by 2 by the "Sub-title") inside the blockquote. This also makes those rubrics support the Sphinx option `heading-level`, for properly generating `<h1>`, `<h2>`, etc.
1 parent 9b76910 commit 7951c93

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

myst_parser/mdit_to_docutils/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,11 @@ def render_heading(self, token: SyntaxTreeNode) -> None:
825825
parent_of_temp_root
826826
or isinstance(self.current_node, nodes.document | nodes.section)
827827
):
828+
level_offset = max(self._level_to_section.keys())
829+
level += level_offset
828830
# if this is not the case, we create a rubric node instead
829831
rubric = nodes.rubric(token.content, "", level=level)
832+
rubric["heading-level"] = level
830833
self.add_line_and_source_path(rubric, token)
831834
self.copy_attributes(token, rubric, ("class", "id"))
832835
with self.current_node_context(rubric, append=True):

tests/test_renderers/fixtures/docutil_syntax_elements.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,19 @@ Heading Levels:
113113

114114
Nested heading
115115
.
116+
# Main heading
116117
> # heading
118+
> ## sub-heading
117119
.
118120
<document source="notset">
119-
<block_quote>
120-
<rubric ids="heading" level="1" names="heading">
121-
heading
121+
<section ids="main-heading" names="main\ heading">
122+
<title>
123+
Main heading
124+
<block_quote>
125+
<rubric heading-level="2" ids="heading" level="2" names="heading">
126+
heading
127+
<rubric heading-level="3" ids="sub-heading" level="3" names="sub-heading">
128+
sub-heading
122129
.
123130

124131
Block Code:

tests/test_renderers/fixtures/sphinx_syntax_elements.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,19 @@ Heading Levels:
113113

114114
Nested heading
115115
.
116+
# Main heading
116117
> # heading
118+
> ## sub-heading
117119
.
118120
<document source="<src>/index.md">
119-
<block_quote>
120-
<rubric ids="heading" level="1" names="heading">
121-
heading
121+
<section ids="main-heading" names="main\ heading">
122+
<title>
123+
Main heading
124+
<block_quote>
125+
<rubric heading-level="2" ids="heading" level="2" names="heading">
126+
heading
127+
<rubric heading-level="3" ids="sub-heading" level="3" names="sub-heading">
128+
sub-heading
122129
.
123130

124131
Nested heading in object

0 commit comments

Comments
 (0)