Skip to content

Commit b2475db

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. A fix in the documentation was necessary, as an example generated a level-4 heading, whose id wasn't generated due to a myst_heading_anchors value too low.
1 parent 9b76910 commit b2475db

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
},
137137
}
138138
myst_number_code_blocks = ["typescript"]
139-
myst_heading_anchors = 2
139+
myst_heading_anchors = 3
140140
myst_footnote_transition = True
141141
myst_dmath_double_inline = True
142142
myst_enable_checkboxes = True

docs/syntax/cross-referencing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ The anchor "slugs" are created according to the [GitHub implementation](https://
7474
For example, using `myst_heading_anchors = 2`:
7575

7676
::::{myst-example}
77-
## A heading with slug
77+
# A heading with slug
7878

79-
## A heading with slug
79+
# A heading with slug
8080

8181
<project:#a-heading-with-slug>
8282

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)