| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
/*
|
|
|
6 |
* This file is part of the league/commonmark package.
|
|
|
7 |
*
|
|
|
8 |
* (c) Colin O'Dell <colinodell@gmail.com>
|
|
|
9 |
*
|
|
|
10 |
* For the full copyright and license information, please view the LICENSE
|
|
|
11 |
* file that was distributed with this source code.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
namespace League\CommonMark\Extension\HeadingPermalink;
|
|
|
15 |
|
|
|
16 |
use League\CommonMark\Environment\EnvironmentAwareInterface;
|
|
|
17 |
use League\CommonMark\Environment\EnvironmentInterface;
|
|
|
18 |
use League\CommonMark\Event\DocumentParsedEvent;
|
|
|
19 |
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
|
|
|
20 |
use League\CommonMark\Node\NodeIterator;
|
|
|
21 |
use League\CommonMark\Node\RawMarkupContainerInterface;
|
|
|
22 |
use League\CommonMark\Node\StringContainerHelper;
|
|
|
23 |
use League\CommonMark\Normalizer\TextNormalizerInterface;
|
|
|
24 |
use League\Config\ConfigurationInterface;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Searches the Document for Heading elements and adds HeadingPermalinks to each one
|
|
|
28 |
*/
|
|
|
29 |
final class HeadingPermalinkProcessor implements EnvironmentAwareInterface
|
|
|
30 |
{
|
|
|
31 |
public const INSERT_BEFORE = 'before';
|
|
|
32 |
public const INSERT_AFTER = 'after';
|
|
|
33 |
|
|
|
34 |
/** @psalm-readonly-allow-private-mutation */
|
|
|
35 |
private TextNormalizerInterface $slugNormalizer;
|
|
|
36 |
|
|
|
37 |
/** @psalm-readonly-allow-private-mutation */
|
|
|
38 |
private ConfigurationInterface $config;
|
|
|
39 |
|
|
|
40 |
public function setEnvironment(EnvironmentInterface $environment): void
|
|
|
41 |
{
|
|
|
42 |
$this->config = $environment->getConfiguration();
|
|
|
43 |
$this->slugNormalizer = $environment->getSlugNormalizer();
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function __invoke(DocumentParsedEvent $e): void
|
|
|
47 |
{
|
|
|
48 |
$min = (int) $this->config->get('heading_permalink/min_heading_level');
|
|
|
49 |
$max = (int) $this->config->get('heading_permalink/max_heading_level');
|
|
|
50 |
|
|
|
51 |
$slugLength = (int) $this->config->get('slug_normalizer/max_length');
|
|
|
52 |
|
|
|
53 |
foreach ($e->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
|
|
|
54 |
if ($node instanceof Heading && $node->getLevel() >= $min && $node->getLevel() <= $max) {
|
|
|
55 |
$this->addHeadingLink($node, $slugLength);
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
private function addHeadingLink(Heading $heading, int $slugLength): void
|
|
|
61 |
{
|
|
|
62 |
$text = StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]);
|
|
|
63 |
$slug = $this->slugNormalizer->normalize($text, [
|
|
|
64 |
'node' => $heading,
|
|
|
65 |
'length' => $slugLength,
|
|
|
66 |
]);
|
|
|
67 |
|
|
|
68 |
$headingLinkAnchor = new HeadingPermalink($slug);
|
|
|
69 |
|
|
|
70 |
switch ($this->config->get('heading_permalink/insert')) {
|
|
|
71 |
case self::INSERT_BEFORE:
|
|
|
72 |
$heading->prependChild($headingLinkAnchor);
|
|
|
73 |
|
|
|
74 |
return;
|
|
|
75 |
case self::INSERT_AFTER:
|
|
|
76 |
$heading->appendChild($headingLinkAnchor);
|
|
|
77 |
|
|
|
78 |
return;
|
|
|
79 |
default:
|
|
|
80 |
throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
}
|