File tree Expand file tree Collapse file tree 4 files changed +30
-31
lines changed Expand file tree Collapse file tree 4 files changed +30
-31
lines changed Original file line number Diff line number Diff line change 1515
1616use League \CommonMark \Event \DocumentParsedEvent ;
1717use League \CommonMark \Extension \DescriptionList \Node \DescriptionList ;
18+ use League \CommonMark \Node \NodeIterator ;
1819
1920final class ConsecutiveDescriptionListMerger
2021{
2122 public function __invoke (DocumentParsedEvent $ event ): void
2223 {
23- $ walker = $ event ->getDocument ()->walker ();
24- while ($ e = $ walker ->next ()) {
25- // Wait until we're exiting a description list node
26- if ($ e ->isEntering ()) {
27- continue ;
28- }
29-
30- $ node = $ e ->getNode ();
24+ foreach ($ event ->getDocument ()->iterator (NodeIterator::FLAG_BLOCKS_ONLY ) as $ node ) {
3125 if (! $ node instanceof DescriptionList) {
3226 continue ;
3327 }
3428
35- if (! ($ next = $ node ->next ()) instanceof DescriptionList) {
29+ if (! ($ prev = $ node ->previous ()) instanceof DescriptionList) {
3630 continue ;
3731 }
3832
39- // There's another description list next ; merge it into the current one
40- foreach ($ next ->children () as $ child ) {
41- $ node ->appendChild ($ child );
33+ // There's another description list behind this one ; merge the current one into that
34+ foreach ($ node ->children () as $ child ) {
35+ $ prev ->appendChild ($ child );
4236 }
4337
44- $ next ->detach ();
45-
46- $ walker ->resumeAt ($ node , false );
38+ $ node ->detach ();
4739 }
4840 }
4941}
Original file line number Diff line number Diff line change 1919use League \CommonMark \Extension \DescriptionList \Node \DescriptionTerm ;
2020use League \CommonMark \Node \Block \Paragraph ;
2121use League \CommonMark \Node \Inline \Newline ;
22+ use League \CommonMark \Node \NodeIterator ;
2223
2324final class LooseDescriptionHandler
2425{
2526 public function __invoke (DocumentParsedEvent $ event ): void
2627 {
27- $ walker = $ event ->getDocument ()->walker ();
28- while ($ e = $ walker ->next ()) {
29- // Wait until we're exiting a node
30- if ($ e ->isEntering ()) {
31- continue ;
32- }
33-
34- $ description = $ e ->getNode ();
28+ foreach ($ event ->getDocument ()->iterator (NodeIterator::FLAG_BLOCKS_ONLY ) as $ description ) {
3529 if (! $ description instanceof Description) {
3630 continue ;
3731 }
Original file line number Diff line number Diff line change @@ -71,19 +71,25 @@ public function onDocumentParsed(DocumentParsedEvent $event): void
7171
7272 private function insertBeforeFirstLinkedHeading (Document $ document , TableOfContents $ toc ): void
7373 {
74- foreach ($ document ->iterator () as $ node ) {
75- if ($ node instanceof HeadingPermalink && ($ parent = $ node ->parent ()) instanceof Heading) {
76- $ parent ->insertBefore ($ toc );
74+ foreach ($ document ->iterator (NodeIterator::FLAG_BLOCKS_ONLY ) as $ node ) {
75+ if (! $ node instanceof Heading) {
76+ continue ;
77+ }
78+
79+ foreach ($ node ->children () as $ child ) {
80+ if ($ child instanceof HeadingPermalink) {
81+ $ node ->insertBefore ($ toc );
7782
78- return ;
83+ return ;
84+ }
7985 }
8086 }
8187 }
8288
8389 private function replacePlaceholders (Document $ document , TableOfContents $ toc ): void
8490 {
8591 foreach ($ document ->iterator (NodeIterator::FLAG_BLOCKS_ONLY ) as $ node ) {
86- // Add the block once we find a placeholder (and we're about to leave it)
92+ // Add the block once we find a placeholder
8793 if (! $ node instanceof TableOfContentsPlaceholder) {
8894 continue ;
8995 }
Original file line number Diff line number Diff line change 2525use League \CommonMark \Extension \TableOfContents \Normalizer \NormalizerStrategyInterface ;
2626use League \CommonMark \Extension \TableOfContents \Normalizer \RelativeNormalizerStrategy ;
2727use League \CommonMark \Node \Block \Document ;
28+ use League \CommonMark \Node \NodeIterator ;
2829use League \CommonMark \Node \RawMarkupContainerInterface ;
2930use League \CommonMark \Node \StringContainerHelper ;
3031use League \Config \Exception \InvalidConfigurationException ;
@@ -130,9 +131,15 @@ private function createToc(Document $document): TableOfContents
130131 */
131132 private function getHeadingLinks (Document $ document ): iterable
132133 {
133- foreach ($ document ->iterator () as $ node ) {
134- if ($ node instanceof HeadingPermalink) {
135- yield $ node ;
134+ foreach ($ document ->iterator (NodeIterator::FLAG_BLOCKS_ONLY ) as $ node ) {
135+ if (! $ node instanceof Heading) {
136+ continue ;
137+ }
138+
139+ foreach ($ node ->children () as $ child ) {
140+ if ($ child instanceof HeadingPermalink) {
141+ yield $ child ;
142+ }
136143 }
137144 }
138145 }
You can’t perform that action at this time.
0 commit comments