Skip to content

Commit

Permalink
Fix Table of Contents blocks not having correct start/end line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Oct 17, 2020
1 parent a1e83c4 commit 197614e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/Extension/TableOfContents/TableOfContentsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 197614e

Please sign in to comment.