Commaaa2
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,11 +13,8 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents\Node;
|
||||
|
||||
use League\CommonMark\Block\Element\ListBlock;
|
||||
use League\CommonMark\Extension\TableOfContents\TableOfContents as DeprecatedTableOfContents;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
|
||||
|
||||
final class TableOfContents extends ListBlock
|
||||
{
|
||||
}
|
||||
|
||||
\class_exists(DeprecatedTableOfContents::class);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,23 +13,8 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents\Node;
|
||||
|
||||
use League\CommonMark\Block\Element\AbstractBlock;
|
||||
use League\CommonMark\Cursor;
|
||||
use League\CommonMark\Node\Block\AbstractBlock;
|
||||
|
||||
final class TableOfContentsPlaceholder extends AbstractBlock
|
||||
{
|
||||
public function canContain(AbstractBlock $block): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isCode(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function matchesNextLine(Cursor $cursor): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,18 +13,20 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
|
||||
|
||||
use League\CommonMark\Block\Element\ListBlock;
|
||||
use League\CommonMark\Block\Element\ListItem;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
|
||||
|
||||
final class AsIsNormalizerStrategy implements NormalizerStrategyInterface
|
||||
{
|
||||
/** @var ListBlock */
|
||||
private $parentListBlock;
|
||||
/** @var int */
|
||||
private $parentLevel = 1;
|
||||
/** @var ListItem|null */
|
||||
private $lastListItem;
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private ListBlock $parentListBlock;
|
||||
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private int $parentLevel = 1;
|
||||
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private ?ListItem $lastListItem = null;
|
||||
|
||||
public function __construct(TableOfContents $toc)
|
||||
{
|
||||
@@ -43,16 +47,17 @@ final class AsIsNormalizerStrategy implements NormalizerStrategyInterface
|
||||
$newListBlock->setEndLine($listItemToAdd->getEndLine());
|
||||
$this->lastListItem->appendChild($newListBlock);
|
||||
$this->parentListBlock = $newListBlock;
|
||||
$this->lastListItem = null;
|
||||
$this->lastListItem = null;
|
||||
|
||||
$this->parentLevel++;
|
||||
}
|
||||
|
||||
while ($level < $this->parentLevel) {
|
||||
// Search upwards for the previous parent list block
|
||||
while (true) {
|
||||
$this->parentListBlock = $this->parentListBlock->parent();
|
||||
if ($this->parentListBlock instanceof ListBlock) {
|
||||
$search = $this->parentListBlock;
|
||||
while ($search = $search->parent()) {
|
||||
if ($search instanceof ListBlock) {
|
||||
$this->parentListBlock = $search;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -65,6 +70,3 @@ final class AsIsNormalizerStrategy implements NormalizerStrategyInterface
|
||||
$this->lastListItem = $listItemToAdd;
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger autoload without causing a deprecated error
|
||||
\class_exists(TableOfContents::class);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,13 +13,13 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
|
||||
|
||||
use League\CommonMark\Block\Element\ListItem;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
|
||||
|
||||
final class FlatNormalizerStrategy implements NormalizerStrategyInterface
|
||||
{
|
||||
/** @var TableOfContents */
|
||||
private $toc;
|
||||
/** @psalm-readonly */
|
||||
private TableOfContents $toc;
|
||||
|
||||
public function __construct(TableOfContents $toc)
|
||||
{
|
||||
@@ -29,6 +31,3 @@ final class FlatNormalizerStrategy implements NormalizerStrategyInterface
|
||||
$this->toc->appendChild($listItemToAdd);
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger autoload without causing a deprecated error
|
||||
\class_exists(TableOfContents::class);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,7 +13,7 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
|
||||
|
||||
use League\CommonMark\Block\Element\ListItem;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
||||
|
||||
interface NormalizerStrategyInterface
|
||||
{
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,17 +13,21 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
|
||||
|
||||
use League\CommonMark\Block\Element\ListBlock;
|
||||
use League\CommonMark\Block\Element\ListItem;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
|
||||
|
||||
final class RelativeNormalizerStrategy implements NormalizerStrategyInterface
|
||||
{
|
||||
/** @var TableOfContents */
|
||||
private $toc;
|
||||
/** @psalm-readonly */
|
||||
private TableOfContents $toc;
|
||||
|
||||
/** @var array<int, ListItem> */
|
||||
private $listItemStack = [];
|
||||
/**
|
||||
* @var array<int, ListItem>
|
||||
*
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
private array $listItemStack = [];
|
||||
|
||||
public function __construct(TableOfContents $toc)
|
||||
{
|
||||
@@ -30,18 +36,15 @@ final class RelativeNormalizerStrategy implements NormalizerStrategyInterface
|
||||
|
||||
public function addItem(int $level, ListItem $listItemToAdd): void
|
||||
{
|
||||
\end($this->listItemStack);
|
||||
$previousLevel = \key($this->listItemStack);
|
||||
$previousLevel = \array_key_last($this->listItemStack);
|
||||
|
||||
// Pop the stack if we're too deep
|
||||
while ($previousLevel !== null && $level < $previousLevel) {
|
||||
array_pop($this->listItemStack);
|
||||
\end($this->listItemStack);
|
||||
$previousLevel = \key($this->listItemStack);
|
||||
\array_pop($this->listItemStack);
|
||||
$previousLevel = \array_key_last($this->listItemStack);
|
||||
}
|
||||
|
||||
/** @var ListItem|false $lastListItem */
|
||||
$lastListItem = \current($this->listItemStack);
|
||||
$lastListItem = \end($this->listItemStack);
|
||||
|
||||
// Need to go one level deeper? Add that level
|
||||
if ($lastListItem !== false && $level > $previousLevel) {
|
||||
@@ -62,6 +65,3 @@ final class RelativeNormalizerStrategy implements NormalizerStrategyInterface
|
||||
$this->listItemStack[$level] = $listItemToAdd;
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger autoload without causing a deprecated error
|
||||
\class_exists(TableOfContents::class);
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents;
|
||||
|
||||
use League\CommonMark\Block\Element\ListBlock;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents as NewTableOfContents;
|
||||
|
||||
if (!class_exists(NewTableOfContents::class)) {
|
||||
@trigger_error(sprintf('TableOfContents has moved to a new namespace; use %s instead', NewTableOfContents::class), \E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
\class_alias(NewTableOfContents::class, TableOfContents::class);
|
||||
|
||||
if (false) {
|
||||
/**
|
||||
* @deprecated This class has moved to the Node sub-namespace; use that instead
|
||||
*/
|
||||
final class TableOfContents extends ListBlock
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,59 +13,36 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents;
|
||||
|
||||
use League\CommonMark\Block\Element\Document;
|
||||
use League\CommonMark\Block\Element\Heading;
|
||||
use League\CommonMark\Event\DocumentParsedEvent;
|
||||
use League\CommonMark\Exception\InvalidOptionException;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
|
||||
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
|
||||
use League\CommonMark\Util\ConfigurationAwareInterface;
|
||||
use League\CommonMark\Util\ConfigurationInterface;
|
||||
use League\CommonMark\Node\Block\Document;
|
||||
use League\CommonMark\Node\NodeIterator;
|
||||
use League\Config\ConfigurationAwareInterface;
|
||||
use League\Config\ConfigurationInterface;
|
||||
use League\Config\Exception\InvalidConfigurationException;
|
||||
|
||||
final class TableOfContentsBuilder implements ConfigurationAwareInterface
|
||||
{
|
||||
/**
|
||||
* @deprecated Use TableOfContentsGenerator::STYLE_BULLET instead
|
||||
*/
|
||||
public const STYLE_BULLET = TableOfContentsGenerator::STYLE_BULLET;
|
||||
|
||||
/**
|
||||
* @deprecated Use TableOfContentsGenerator::STYLE_ORDERED instead
|
||||
*/
|
||||
public const STYLE_ORDERED = TableOfContentsGenerator::STYLE_ORDERED;
|
||||
|
||||
/**
|
||||
* @deprecated Use TableOfContentsGenerator::NORMALIZE_DISABLED instead
|
||||
*/
|
||||
public const NORMALIZE_DISABLED = TableOfContentsGenerator::NORMALIZE_DISABLED;
|
||||
|
||||
/**
|
||||
* @deprecated Use TableOfContentsGenerator::NORMALIZE_RELATIVE instead
|
||||
*/
|
||||
public const NORMALIZE_RELATIVE = TableOfContentsGenerator::NORMALIZE_RELATIVE;
|
||||
|
||||
/**
|
||||
* @deprecated Use TableOfContentsGenerator::NORMALIZE_FLAT instead
|
||||
*/
|
||||
public const NORMALIZE_FLAT = TableOfContentsGenerator::NORMALIZE_FLAT;
|
||||
|
||||
public const POSITION_TOP = 'top';
|
||||
public const POSITION_TOP = 'top';
|
||||
public const POSITION_BEFORE_HEADINGS = 'before-headings';
|
||||
public const POSITION_PLACEHOLDER = 'placeholder';
|
||||
public const POSITION_PLACEHOLDER = 'placeholder';
|
||||
|
||||
/** @var ConfigurationInterface */
|
||||
private $config;
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private ConfigurationInterface $config;
|
||||
|
||||
public function onDocumentParsed(DocumentParsedEvent $event): void
|
||||
{
|
||||
$document = $event->getDocument();
|
||||
|
||||
$generator = new TableOfContentsGenerator(
|
||||
$this->config->get('table_of_contents/style', TableOfContentsGenerator::STYLE_BULLET),
|
||||
$this->config->get('table_of_contents/normalize', TableOfContentsGenerator::NORMALIZE_RELATIVE),
|
||||
(int) $this->config->get('table_of_contents/min_heading_level', 1),
|
||||
(int) $this->config->get('table_of_contents/max_heading_level', 6)
|
||||
(string) $this->config->get('table_of_contents/style'),
|
||||
(string) $this->config->get('table_of_contents/normalize'),
|
||||
(int) $this->config->get('table_of_contents/min_heading_level'),
|
||||
(int) $this->config->get('table_of_contents/max_heading_level'),
|
||||
(string) $this->config->get('heading_permalink/fragment_prefix'),
|
||||
);
|
||||
|
||||
$toc = $generator->generate($document);
|
||||
@@ -73,13 +52,13 @@ final class TableOfContentsBuilder implements ConfigurationAwareInterface
|
||||
}
|
||||
|
||||
// Add custom CSS class(es), if defined
|
||||
$class = $this->config->get('table_of_contents/html_class', 'table-of-contents');
|
||||
if (!empty($class)) {
|
||||
$toc->data['attributes']['class'] = $class;
|
||||
$class = $this->config->get('table_of_contents/html_class');
|
||||
if ($class !== null) {
|
||||
$toc->data->append('attributes/class', $class);
|
||||
}
|
||||
|
||||
// Add the TOC to the Document
|
||||
$position = $this->config->get('table_of_contents/position', self::POSITION_TOP);
|
||||
$position = $this->config->get('table_of_contents/position');
|
||||
if ($position === self::POSITION_TOP) {
|
||||
$document->prependChild($toc);
|
||||
} elseif ($position === self::POSITION_BEFORE_HEADINGS) {
|
||||
@@ -87,41 +66,41 @@ final class TableOfContentsBuilder implements ConfigurationAwareInterface
|
||||
} elseif ($position === self::POSITION_PLACEHOLDER) {
|
||||
$this->replacePlaceholders($document, $toc);
|
||||
} else {
|
||||
throw new InvalidOptionException(\sprintf('Invalid config option "%s" for "table_of_contents/position"', $position));
|
||||
throw InvalidConfigurationException::forConfigOption('table_of_contents/position', $position);
|
||||
}
|
||||
}
|
||||
|
||||
private function insertBeforeFirstLinkedHeading(Document $document, TableOfContents $toc): void
|
||||
{
|
||||
$walker = $document->walker();
|
||||
while ($event = $walker->next()) {
|
||||
if ($event->isEntering() && ($node = $event->getNode()) instanceof HeadingPermalink && ($parent = $node->parent()) instanceof Heading) {
|
||||
$parent->insertBefore($toc);
|
||||
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
|
||||
if (! $node instanceof Heading) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return;
|
||||
foreach ($node->children() as $child) {
|
||||
if ($child instanceof HeadingPermalink) {
|
||||
$node->insertBefore($toc);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function replacePlaceholders(Document $document, TableOfContents $toc): void
|
||||
{
|
||||
$walker = $document->walker();
|
||||
while ($event = $walker->next()) {
|
||||
// Add the block once we find a placeholder (and we're about to leave it)
|
||||
if (!$event->getNode() instanceof TableOfContentsPlaceholder) {
|
||||
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
|
||||
// Add the block once we find a placeholder
|
||||
if (! $node instanceof TableOfContentsPlaceholder) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($event->isEntering()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event->getNode()->replaceWith(clone $toc);
|
||||
$node->replaceWith(clone $toc);
|
||||
}
|
||||
}
|
||||
|
||||
public function setConfiguration(ConfigurationInterface $config)
|
||||
public function setConfiguration(ConfigurationInterface $configuration): void
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->config = $configuration;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,21 +13,41 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents;
|
||||
|
||||
use League\CommonMark\ConfigurableEnvironmentInterface;
|
||||
use League\CommonMark\Environment\EnvironmentBuilderInterface;
|
||||
use League\CommonMark\Event\DocumentParsedEvent;
|
||||
use League\CommonMark\Extension\ExtensionInterface;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
|
||||
use League\CommonMark\Extension\CommonMark\Renderer\Block\ListBlockRenderer;
|
||||
use League\CommonMark\Extension\ConfigurableExtensionInterface;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
|
||||
use League\Config\ConfigurationBuilderInterface;
|
||||
use Nette\Schema\Expect;
|
||||
|
||||
final class TableOfContentsExtension implements ExtensionInterface
|
||||
final class TableOfContentsExtension implements ConfigurableExtensionInterface
|
||||
{
|
||||
public function register(ConfigurableEnvironmentInterface $environment): void
|
||||
public function configureSchema(ConfigurationBuilderInterface $builder): void
|
||||
{
|
||||
$builder->addSchema('table_of_contents', Expect::structure([
|
||||
'position' => Expect::anyOf(TableOfContentsBuilder::POSITION_BEFORE_HEADINGS, TableOfContentsBuilder::POSITION_PLACEHOLDER, TableOfContentsBuilder::POSITION_TOP)->default(TableOfContentsBuilder::POSITION_TOP),
|
||||
'style' => Expect::anyOf(ListBlock::TYPE_BULLET, ListBlock::TYPE_ORDERED)->default(ListBlock::TYPE_BULLET),
|
||||
'normalize' => Expect::anyOf(TableOfContentsGenerator::NORMALIZE_RELATIVE, TableOfContentsGenerator::NORMALIZE_FLAT, TableOfContentsGenerator::NORMALIZE_DISABLED)->default(TableOfContentsGenerator::NORMALIZE_RELATIVE),
|
||||
'min_heading_level' => Expect::int()->min(1)->max(6)->default(1),
|
||||
'max_heading_level' => Expect::int()->min(1)->max(6)->default(6),
|
||||
'html_class' => Expect::string()->default('table-of-contents'),
|
||||
'placeholder' => Expect::anyOf(Expect::string(), Expect::null())->default(null),
|
||||
]));
|
||||
}
|
||||
|
||||
public function register(EnvironmentBuilderInterface $environment): void
|
||||
{
|
||||
$environment->addRenderer(TableOfContents::class, new TableOfContentsRenderer(new ListBlockRenderer()));
|
||||
$environment->addEventListener(DocumentParsedEvent::class, [new TableOfContentsBuilder(), 'onDocumentParsed'], -150);
|
||||
|
||||
if ($environment->getConfig('table_of_contents/position') === TableOfContentsBuilder::POSITION_PLACEHOLDER) {
|
||||
$environment->addBlockParser(new TableOfContentsPlaceholderParser(), 200);
|
||||
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
|
||||
if ($environment->getConfiguration()->get('table_of_contents/position') === TableOfContentsBuilder::POSITION_PLACEHOLDER) {
|
||||
$environment->addBlockStartParser(TableOfContentsPlaceholderParser::blockStartParser(), 200);
|
||||
// If a placeholder cannot be replaced with a TOC element this renderer will ensure the parser won't error out
|
||||
$environment->addBlockRenderer(TableOfContentsPlaceholder::class, new TableOfContentsPlaceholderRenderer());
|
||||
$environment->addRenderer(TableOfContentsPlaceholder::class, new TableOfContentsPlaceholderRenderer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,46 +13,58 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents;
|
||||
|
||||
use League\CommonMark\Block\Element\Document;
|
||||
use League\CommonMark\Block\Element\Heading;
|
||||
use League\CommonMark\Block\Element\ListBlock;
|
||||
use League\CommonMark\Block\Element\ListData;
|
||||
use League\CommonMark\Block\Element\ListItem;
|
||||
use League\CommonMark\Block\Element\Paragraph;
|
||||
use League\CommonMark\Exception\InvalidOptionException;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
|
||||
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
|
||||
use League\CommonMark\Extension\TableOfContents\Normalizer\AsIsNormalizerStrategy;
|
||||
use League\CommonMark\Extension\TableOfContents\Normalizer\FlatNormalizerStrategy;
|
||||
use League\CommonMark\Extension\TableOfContents\Normalizer\NormalizerStrategyInterface;
|
||||
use League\CommonMark\Extension\TableOfContents\Normalizer\RelativeNormalizerStrategy;
|
||||
use League\CommonMark\Inline\Element\AbstractStringContainer;
|
||||
use League\CommonMark\Inline\Element\Link;
|
||||
use League\CommonMark\Node\Block\Document;
|
||||
use League\CommonMark\Node\NodeIterator;
|
||||
use League\CommonMark\Node\RawMarkupContainerInterface;
|
||||
use League\CommonMark\Node\StringContainerHelper;
|
||||
use League\Config\Exception\InvalidConfigurationException;
|
||||
|
||||
final class TableOfContentsGenerator implements TableOfContentsGeneratorInterface
|
||||
{
|
||||
public const STYLE_BULLET = ListBlock::TYPE_BULLET;
|
||||
public const STYLE_BULLET = ListBlock::TYPE_BULLET;
|
||||
public const STYLE_ORDERED = ListBlock::TYPE_ORDERED;
|
||||
|
||||
public const NORMALIZE_DISABLED = 'as-is';
|
||||
public const NORMALIZE_RELATIVE = 'relative';
|
||||
public const NORMALIZE_FLAT = 'flat';
|
||||
public const NORMALIZE_FLAT = 'flat';
|
||||
|
||||
/** @var string */
|
||||
private $style;
|
||||
/** @var string */
|
||||
private $normalizationStrategy;
|
||||
/** @var int */
|
||||
private $minHeadingLevel;
|
||||
/** @var int */
|
||||
private $maxHeadingLevel;
|
||||
/** @psalm-readonly */
|
||||
private string $style;
|
||||
|
||||
public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel)
|
||||
/** @psalm-readonly */
|
||||
private string $normalizationStrategy;
|
||||
|
||||
/** @psalm-readonly */
|
||||
private int $minHeadingLevel;
|
||||
|
||||
/** @psalm-readonly */
|
||||
private int $maxHeadingLevel;
|
||||
|
||||
/** @psalm-readonly */
|
||||
private string $fragmentPrefix;
|
||||
|
||||
public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel, string $fragmentPrefix)
|
||||
{
|
||||
$this->style = $style;
|
||||
$this->style = $style;
|
||||
$this->normalizationStrategy = $normalizationStrategy;
|
||||
$this->minHeadingLevel = $minHeadingLevel;
|
||||
$this->maxHeadingLevel = $maxHeadingLevel;
|
||||
$this->minHeadingLevel = $minHeadingLevel;
|
||||
$this->maxHeadingLevel = $maxHeadingLevel;
|
||||
$this->fragmentPrefix = $fragmentPrefix;
|
||||
|
||||
if ($fragmentPrefix !== '') {
|
||||
$this->fragmentPrefix .= '-';
|
||||
}
|
||||
}
|
||||
|
||||
public function generate(Document $document): ?TableOfContents
|
||||
@@ -64,7 +78,7 @@ final class TableOfContentsGenerator implements TableOfContentsGeneratorInterfac
|
||||
foreach ($this->getHeadingLinks($document) as $headingLink) {
|
||||
$heading = $headingLink->parent();
|
||||
// Make sure this is actually tied to a heading
|
||||
if (!$heading instanceof Heading) {
|
||||
if (! $heading instanceof Heading) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -74,30 +88,26 @@ final class TableOfContentsGenerator implements TableOfContentsGeneratorInterfac
|
||||
}
|
||||
|
||||
// Keep track of the first heading we see - we might need this later
|
||||
$firstHeading = $firstHeading ?? $heading;
|
||||
$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('#' . $headingLink->getSlug(), self::getHeadingText($heading));
|
||||
$paragraph = new Paragraph();
|
||||
$paragraph->setStartLine($heading->getStartLine());
|
||||
$paragraph->setEndLine($heading->getEndLine());
|
||||
$paragraph->appendChild($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($paragraph);
|
||||
$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) {
|
||||
if (! $toc->hasChildren() || $firstHeading === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -113,7 +123,7 @@ final class TableOfContentsGenerator implements TableOfContentsGeneratorInterfac
|
||||
} elseif ($this->style === self::STYLE_ORDERED) {
|
||||
$listData->type = ListBlock::TYPE_ORDERED;
|
||||
} else {
|
||||
throw new InvalidOptionException(\sprintf('Invalid table of contents list style "%s"', $this->style));
|
||||
throw new InvalidConfigurationException(\sprintf('Invalid table of contents list style: "%s"', $this->style));
|
||||
}
|
||||
|
||||
$toc = new TableOfContents($listData);
|
||||
@@ -125,16 +135,19 @@ final class TableOfContentsGenerator implements TableOfContentsGeneratorInterfac
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Document $document
|
||||
*
|
||||
* @return iterable<HeadingPermalink>
|
||||
*/
|
||||
private function getHeadingLinks(Document $document)
|
||||
private function getHeadingLinks(Document $document): iterable
|
||||
{
|
||||
$walker = $document->walker();
|
||||
while ($event = $walker->next()) {
|
||||
if ($event->isEntering() && ($node = $event->getNode()) instanceof HeadingPermalink) {
|
||||
yield $node;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,24 +162,7 @@ final class TableOfContentsGenerator implements TableOfContentsGeneratorInterfac
|
||||
case self::NORMALIZE_FLAT:
|
||||
return new FlatNormalizerStrategy($toc);
|
||||
default:
|
||||
throw new InvalidOptionException(\sprintf('Invalid table of contents normalization strategy "%s"', $this->normalizationStrategy));
|
||||
throw new InvalidConfigurationException(\sprintf('Invalid table of contents normalization strategy: "%s"', $this->normalizationStrategy));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private static function getHeadingText(Heading $heading)
|
||||
{
|
||||
$text = '';
|
||||
|
||||
$walker = $heading->walker();
|
||||
while ($event = $walker->next()) {
|
||||
if ($event->isEntering() && ($child = $event->getNode()) instanceof AbstractStringContainer) {
|
||||
$text .= $child->getContent();
|
||||
}
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,13 +13,10 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents;
|
||||
|
||||
use League\CommonMark\Block\Element\Document;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
|
||||
use League\CommonMark\Node\Block\Document;
|
||||
|
||||
interface TableOfContentsGeneratorInterface
|
||||
{
|
||||
public function generate(Document $document): ?TableOfContents;
|
||||
}
|
||||
|
||||
// Trigger autoload without causing a deprecated error
|
||||
\class_exists(TableOfContents::class);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,37 +13,62 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents;
|
||||
|
||||
use League\CommonMark\Block\Parser\BlockParserInterface;
|
||||
use League\CommonMark\ContextInterface;
|
||||
use League\CommonMark\Cursor;
|
||||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
|
||||
use League\CommonMark\Util\ConfigurationAwareInterface;
|
||||
use League\CommonMark\Util\ConfigurationInterface;
|
||||
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
|
||||
use League\CommonMark\Parser\Block\BlockContinue;
|
||||
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
|
||||
use League\CommonMark\Parser\Block\BlockStart;
|
||||
use League\CommonMark\Parser\Block\BlockStartParserInterface;
|
||||
use League\CommonMark\Parser\Cursor;
|
||||
use League\CommonMark\Parser\MarkdownParserStateInterface;
|
||||
use League\Config\ConfigurationAwareInterface;
|
||||
use League\Config\ConfigurationInterface;
|
||||
|
||||
final class TableOfContentsPlaceholderParser implements BlockParserInterface, ConfigurationAwareInterface
|
||||
final class TableOfContentsPlaceholderParser extends AbstractBlockContinueParser
|
||||
{
|
||||
/** @var ConfigurationInterface */
|
||||
private $config;
|
||||
/** @psalm-readonly */
|
||||
private TableOfContentsPlaceholder $block;
|
||||
|
||||
public function parse(ContextInterface $context, Cursor $cursor): bool
|
||||
public function __construct()
|
||||
{
|
||||
$placeholder = $this->config->get('table_of_contents/placeholder');
|
||||
if ($placeholder === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The placeholder must be the only thing on the line
|
||||
if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$context->addBlock(new TableOfContentsPlaceholder());
|
||||
|
||||
return true;
|
||||
$this->block = new TableOfContentsPlaceholder();
|
||||
}
|
||||
|
||||
public function setConfiguration(ConfigurationInterface $configuration)
|
||||
public function getBlock(): TableOfContentsPlaceholder
|
||||
{
|
||||
$this->config = $configuration;
|
||||
return $this->block;
|
||||
}
|
||||
|
||||
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
|
||||
{
|
||||
return BlockContinue::none();
|
||||
}
|
||||
|
||||
public static function blockStartParser(): BlockStartParserInterface
|
||||
{
|
||||
return new class () implements BlockStartParserInterface, ConfigurationAwareInterface {
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private ConfigurationInterface $config;
|
||||
|
||||
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
|
||||
{
|
||||
$placeholder = $this->config->get('table_of_contents/placeholder');
|
||||
if ($placeholder === null) {
|
||||
return BlockStart::none();
|
||||
}
|
||||
|
||||
// The placeholder must be the only thing on the line
|
||||
if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) {
|
||||
return BlockStart::none();
|
||||
}
|
||||
|
||||
return BlockStart::of(new TableOfContentsPlaceholderParser())->at($cursor);
|
||||
}
|
||||
|
||||
public function setConfiguration(ConfigurationInterface $configuration): void
|
||||
{
|
||||
$this->config = $configuration;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -11,14 +13,28 @@
|
||||
|
||||
namespace League\CommonMark\Extension\TableOfContents;
|
||||
|
||||
use League\CommonMark\Block\Element\AbstractBlock;
|
||||
use League\CommonMark\Block\Renderer\BlockRendererInterface;
|
||||
use League\CommonMark\ElementRendererInterface;
|
||||
use League\CommonMark\Node\Node;
|
||||
use League\CommonMark\Renderer\ChildNodeRendererInterface;
|
||||
use League\CommonMark\Renderer\NodeRendererInterface;
|
||||
use League\CommonMark\Xml\XmlNodeRendererInterface;
|
||||
|
||||
final class TableOfContentsPlaceholderRenderer implements BlockRendererInterface
|
||||
final class TableOfContentsPlaceholderRenderer implements NodeRendererInterface, XmlNodeRendererInterface
|
||||
{
|
||||
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
|
||||
public function render(Node $node, ChildNodeRendererInterface $childRenderer): string
|
||||
{
|
||||
return '<!-- table of contents -->';
|
||||
}
|
||||
|
||||
public function getXmlTagName(Node $node): string
|
||||
{
|
||||
return 'table_of_contents_placeholder';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, scalar>
|
||||
*/
|
||||
public function getXmlAttributes(Node $node): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user