@@ -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 "" )
0 commit comments