Skip to content

Commit 9b76910

Browse files
committed
🐛 support headings in nested parse
This fixes using Markdown headings in Sphinx objects, like the `object`, `describe`, `py:data` directives, etc. This works by adding the heading to the temp root parent instead of at the end of the previous section from the main document. Fixes #1050
1 parent 9364edb commit 9b76910

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

myst_parser/mdit_to_docutils/base.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -416,28 +416,35 @@ def copy_attributes(
416416
continue
417417
node[key] = value
418418

419-
def update_section_level_state(self, section: nodes.section, level: int) -> None:
419+
def update_section_level_state(
420+
self,
421+
section: nodes.section,
422+
level: int,
423+
*,
424+
parent: nodes.Element | None = None,
425+
) -> None:
420426
"""Update the section level state, with the new current section and level."""
421427
# find the closest parent section
422-
parent_level = max(
423-
section_level
424-
for section_level in self._level_to_section
425-
if level > section_level
426-
)
427-
parent = self._level_to_section[parent_level]
428-
429-
# if we are jumping up to a non-consecutive level,
430-
# then warn about this, since this will not be propagated in the docutils AST
431-
if (level > parent_level) and (parent_level + 1 != level):
432-
msg = f"Non-consecutive header level increase; H{parent_level} to H{level}"
433-
if parent_level == 0:
434-
msg = f"Document headings start at H{level}, not H1"
435-
self.create_warning(
436-
msg,
437-
MystWarnings.MD_HEADING_NON_CONSECUTIVE,
438-
line=section.line,
439-
append_to=self.current_node,
428+
if parent is None:
429+
parent_level = max(
430+
section_level
431+
for section_level in self._level_to_section
432+
if level > section_level
440433
)
434+
parent = self._level_to_section[parent_level]
435+
436+
# if we are jumping up to a non-consecutive level,
437+
# then warn about this, since this will not be propagated in the docutils AST
438+
if (level > parent_level) and (parent_level + 1 != level):
439+
msg = f"Non-consecutive header level increase; H{parent_level} to H{level}"
440+
if parent_level == 0:
441+
msg = f"Document headings start at H{level}, not H1"
442+
self.create_warning(
443+
msg,
444+
MystWarnings.MD_HEADING_NON_CONSECUTIVE,
445+
line=section.line,
446+
append_to=self.current_node,
447+
)
441448

442449
# append the new section to the parent
443450
parent.append(section)
@@ -838,7 +845,11 @@ def render_heading(self, token: SyntaxTreeNode) -> None:
838845
new_section["classes"].extend(["tex2jax_ignore", "mathjax_ignore"])
839846

840847
# update the state of the section levels
841-
self.update_section_level_state(new_section, level)
848+
self.update_section_level_state(
849+
new_section,
850+
level,
851+
parent=self.current_node if parent_of_temp_root else None,
852+
)
842853

843854
# create the title for this section
844855
title_node = nodes.title(token.children[0].content if token.children else "")

tests/test_renderers/fixtures/sphinx_syntax_elements.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ Nested heading
121121
heading
122122
.
123123

124+
Nested heading in object
125+
.
126+
```{object} foo
127+
bar
128+
# level 1
129+
wunderbar
130+
## level 2
131+
wunderbar
132+
```
133+
.
134+
<document source="<src>/index.md">
135+
<index entries="">
136+
<desc classes="object" desctype="object" domain="" no-contents-entry="False" no-index="False" no-index-entry="False" no-typesetting="False" nocontentsentry="False" noindex="False" noindexentry="False" objtype="object">
137+
<desc_signature _toc_name="" _toc_parts="()" classes="sig sig-object">
138+
<desc_name classes="sig-name descname" xml:space="preserve">
139+
foo
140+
<desc_content>
141+
<paragraph>
142+
bar
143+
<section ids="level-1" names="level\ 1">
144+
<title>
145+
level 1
146+
<paragraph>
147+
wunderbar
148+
<section ids="level-2" names="level\ 2">
149+
<title>
150+
level 2
151+
<paragraph>
152+
wunderbar
153+
.
154+
124155
Block Code:
125156
.
126157
foo

0 commit comments

Comments
 (0)