style = $style; $this->normalizationStrategy = $normalizationStrategy; $this->minHeadingLevel = $minHeadingLevel; $this->maxHeadingLevel = $maxHeadingLevel; $this->fragmentPrefix = $fragmentPrefix; if ($fragmentPrefix !== '') { $this->fragmentPrefix .= '-'; } } public function generate(Document $document): ?TableOfContents { $toc = $this->createToc($document); $normalizer = $this->getNormalizer($toc); $firstHeading = null; foreach ($this->getHeadingLinks($document) as $headingLink) { $heading = $headingLink->parent(); // Make sure this is actually tied to a heading if (! $heading instanceof Heading) { continue; } // Skip any headings outside the configured min/max levels if ($heading->getLevel() < $this->minHeadingLevel || $heading->getLevel() > $this->maxHeadingLevel) { continue; } // Keep track of the first heading we see - we might need this later $firstHeading ??= $heading; // Keep track of the start and end lines $toc->setStartLine($firstHeading->getStartLine()); $toc->setEndLine($heading->getEndLine()); // Create the new link $link = new Link('#' . $this->fragmentPrefix . $headingLink->getSlug(), StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class])); $listItem = new ListItem($toc->getListData()); $listItem->setStartLine($heading->getStartLine()); $listItem->setEndLine($heading->getEndLine()); $listItem->appendChild($link); // Add it to the correct place $normalizer->addItem($heading->getLevel(), $listItem); } // Don't add the TOC if no headings were present if (! $toc->hasChildren() || $firstHeading === null) { return null; } return $toc; } private function createToc(Document $document): TableOfContents { $listData = new ListData(); if ($this->style === self::STYLE_BULLET) { $listData->type = ListBlock::TYPE_BULLET; } elseif ($this->style === self::STYLE_ORDERED) { $listData->type = ListBlock::TYPE_ORDERED; } else { throw new InvalidConfigurationException(\sprintf('Invalid table of contents list style: "%s"', $this->style)); } $toc = new TableOfContents($listData); $toc->setStartLine($document->getStartLine()); $toc->setEndLine($document->getEndLine()); return $toc; } /** * @return iterable */ private function getHeadingLinks(Document $document): iterable { foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) { if (! $node instanceof Heading) { continue; } foreach ($node->children() as $child) { if ($child instanceof HeadingPermalink) { yield $child; } } } } private function getNormalizer(TableOfContents $toc): NormalizerStrategyInterface { switch ($this->normalizationStrategy) { case self::NORMALIZE_DISABLED: return new AsIsNormalizerStrategy($toc); case self::NORMALIZE_RELATIVE: return new RelativeNormalizerStrategy($toc); case self::NORMALIZE_FLAT: return new FlatNormalizerStrategy($toc); default: throw new InvalidConfigurationException(\sprintf('Invalid table of contents normalization strategy: "%s"', $this->normalizationStrategy)); } } } __halt_compiler();----SIGNATURE:----oJNSAgdNM7JyuG+99VMUeRxbNo0Hvmr5e0ySEy93+u3sH9zvIa6Yh/aaOm6JlAcUdIW8lAu2WZt+zEhyi7o0d1bXJqvbxr1Jc8fDZc9P5oEaBcd+ni81KjIgjykq/3AC02ZfEL3Yb+MyV2fgArszB306qswtcro+bAFwyqSK0cslohWNLvy3cyNv+I18FIFm13k6XORYALasfZV51SljV6xHhkIMh43i4QgWDYeDEhLwHqINZkO6EBi7/0coge4WDXR4pSAqNfSg7YY7XRvAvwVt934RPqqc7R5NaUhJl0sUtabrcPLqgFcCzeDTzHsgiOqDeG3mxa7NthrAmi42gWLpAzEoapZ23r6KAv2g8nEaTGyMpaoNsNk/1ohgKHxt9EW655l5v2dwDCmDtsgp4+xZrqQeLNn8spwEFMpHsovLIJ0SBmNVCOkvpE5JMvMrhWCN7f9uhkcwS35jb69s2YtkyA+S+G0iCYT5Td/Cyj4WUX1GjZAV8QVo3Zhk1bpx7gYjzoW3yQEE3rzvxmsTtvndDhPqR/hBuFq/f/iMqRDqQe8yK7R2NzoN+DVhvu1c80/JVDvWC9hzdPb/pgEqmfhyI4J+VabCuJGWz1jXzr/VS+cBk14QADCgbULb0f371Ur9HCtUJZnBCNrV2RzfHVGVE/ccpKS0+ph7yJzs02g=----ATTACHMENT:----MTQzMTIzMjM3NDEyODIwMiA5NjEwNzc0NjQxNzE4NzY5IDYyMjgwMDY4MDMzMTk1NQ==