@@ -71,31 +71,35 @@ public function leaveNode(Node $node, CompilerContext $compilerContext): Node|nu
7171 return $ lastSection ;
7272 }
7373
74- if ($ node instanceof TitleNode) {
75- $ lastSection = end ($ this ->sectionStack );
76- if ($ lastSection instanceof SectionNode && $ node !== $ lastSection ->getTitle () && $ node ->getLevel () <= $ lastSection ->getTitle ()->getLevel ()) {
77- while (end ($ this ->sectionStack ) instanceof SectionNode && $ node !== end ($ this ->sectionStack )->getTitle () && $ node ->getLevel () <= end ($ this ->sectionStack )->getTitle ()->getLevel ()) {
78- $ lastSection = array_pop ($ this ->sectionStack );
79- }
80-
81- $ newSection = new SectionNode ($ node );
82- if (end ($ this ->sectionStack ) instanceof SectionNode) {
83- end ($ this ->sectionStack )->addChildNode ($ newSection );
84- }
85-
86- $ this ->sectionStack [] = $ newSection ;
87-
88- return $ lastSection ?->getTitle()->getLevel () === 1 ? $ lastSection : null ;
89- } else {
90- $ newSection = new SectionNode ($ node );
91- if ($ lastSection instanceof SectionNode) {
92- $ lastSection ->addChildNode ($ newSection );
93- }
94-
95- $ this ->sectionStack [] = $ newSection ;
74+ if (!$ node instanceof TitleNode) {
75+ // Remove all nodes that will be attached to a section
76+ return null ;
77+ }
78+
79+ $ lastSection = end ($ this ->sectionStack );
80+ if ($ lastSection instanceof SectionNode && $ node !== $ lastSection ->getTitle () && $ node ->getLevel () <= $ lastSection ->getTitle ()->getLevel ()) {
81+ while (end ($ this ->sectionStack ) instanceof SectionNode && $ node !== end ($ this ->sectionStack )->getTitle () && $ node ->getLevel () <= end ($ this ->sectionStack )->getTitle ()->getLevel ()) {
82+ $ lastSection = array_pop ($ this ->sectionStack );
83+ }
84+
85+ $ newSection = new SectionNode ($ node );
86+ // Attach the new section to the last one still on the stack if there still is one
87+ if (end ($ this ->sectionStack ) instanceof SectionNode) {
88+ end ($ this ->sectionStack )->addChildNode ($ newSection );
9689 }
90+
91+ $ this ->sectionStack [] = $ newSection ;
92+
93+ return $ lastSection ?->getTitle()->getLevel () === 1 ? $ lastSection : null ;
9794 }
9895
96+ $ newSection = new SectionNode ($ node );
97+ if ($ lastSection instanceof SectionNode) {
98+ $ lastSection ->addChildNode ($ newSection );
99+ }
100+
101+ $ this ->sectionStack [] = $ newSection ;
102+
99103 return null ;
100104 }
101105
0 commit comments