diff --git a/src/Extension/TableOfContents/Normalizer/AsIsNormalizerStrategy.php b/src/Extension/TableOfContents/Normalizer/AsIsNormalizerStrategy.php index 082cfe050c..c9e524bded 100644 --- a/src/Extension/TableOfContents/Normalizer/AsIsNormalizerStrategy.php +++ b/src/Extension/TableOfContents/Normalizer/AsIsNormalizerStrategy.php @@ -39,6 +39,8 @@ public function addItem(int $level, ListItem $listItemToAdd): void } $newListBlock = new ListBlock($this->parentListBlock->getListData()); + $newListBlock->setStartLine($listItemToAdd->getStartLine()); + $newListBlock->setEndLine($listItemToAdd->getEndLine()); $this->lastListItem->appendChild($newListBlock); $this->parentListBlock = $newListBlock; $this->lastListItem = null; diff --git a/src/Extension/TableOfContents/Normalizer/RelativeNormalizerStrategy.php b/src/Extension/TableOfContents/Normalizer/RelativeNormalizerStrategy.php index 0834e6dab7..3043d80e8b 100644 --- a/src/Extension/TableOfContents/Normalizer/RelativeNormalizerStrategy.php +++ b/src/Extension/TableOfContents/Normalizer/RelativeNormalizerStrategy.php @@ -46,6 +46,8 @@ public function addItem(int $level, ListItem $listItemToAdd): void // Need to go one level deeper? Add that level if ($lastListItem !== false && $level > $previousLevel) { $targetListBlock = new ListBlock($lastListItem->getListData()); + $targetListBlock->setStartLine($listItemToAdd->getStartLine()); + $targetListBlock->setEndLine($listItemToAdd->getEndLine()); $lastListItem->appendChild($targetListBlock); // Otherwise we're at the right level // If there's no stack we're adding this item directly to the TOC element diff --git a/src/Extension/TableOfContents/TableOfContentsGenerator.php b/src/Extension/TableOfContents/TableOfContentsGenerator.php index ff767348b5..98f8efa2dd 100644 --- a/src/Extension/TableOfContents/TableOfContentsGenerator.php +++ b/src/Extension/TableOfContents/TableOfContentsGenerator.php @@ -54,7 +54,7 @@ public function __construct(string $style, string $normalizationStrategy, int $m public function generate(Document $document): ?TableOfContents { - $toc = $this->createToc(); + $toc = $this->createToc($document); $normalizer = $this->getNormalizer($toc); @@ -103,7 +103,7 @@ public function generate(Document $document): ?TableOfContents return $toc; } - private function createToc(): TableOfContents + private function createToc(Document $document): TableOfContents { $listData = new ListData(); @@ -115,7 +115,12 @@ private function createToc(): TableOfContents throw new InvalidOptionException(\sprintf('Invalid table of contents list style "%s"', $this->style)); } - return new TableOfContents($listData); + $toc = new TableOfContents($listData); + + $toc->setStartLine($document->getStartLine()); + $toc->setEndLine($document->getEndLine()); + + return $toc; } /**