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 <[email protected]> |
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
|
98 |
|
public function setEnvironment(EnvironmentInterface $environment): void |
41
|
|
|
{ |
42
|
98 |
|
$this->config = $environment->getConfiguration(); |
43
|
98 |
|
$this->slugNormalizer = $environment->getSlugNormalizer(); |
44
|
|
|
} |
45
|
|
|
|
46
|
96 |
|
public function __invoke(DocumentParsedEvent $e): void |
47
|
|
|
{ |
48
|
96 |
|
$min = (int) $this->config->get('heading_permalink/min_heading_level'); |
49
|
96 |
|
$max = (int) $this->config->get('heading_permalink/max_heading_level'); |
50
|
|
|
|
51
|
96 |
|
$slugLength = (int) $this->config->get('slug_normalizer/max_length'); |
52
|
|
|
|
53
|
96 |
|
foreach ($e->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) { |
54
|
96 |
|
if ($node instanceof Heading && $node->getLevel() >= $min && $node->getLevel() <= $max) { |
55
|
88 |
|
$this->addHeadingLink($node, $slugLength); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
88 |
|
private function addHeadingLink(Heading $heading, int $slugLength): void |
61
|
|
|
{ |
62
|
88 |
|
$text = StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]); |
63
|
88 |
|
$slug = $this->slugNormalizer->normalize($text, [ |
64
|
|
|
'node' => $heading, |
65
|
|
|
'length' => $slugLength, |
66
|
|
|
]); |
67
|
|
|
|
68
|
88 |
|
$headingLinkAnchor = new HeadingPermalink($slug); |
69
|
|
|
|
70
|
88 |
|
switch ($this->config->get('heading_permalink/insert')) { |
71
|
44 |
|
case self::INSERT_BEFORE: |
72
|
82 |
|
$heading->prependChild($headingLinkAnchor); |
73
|
|
|
|
74
|
82 |
|
return; |
75
|
3 |
|
case self::INSERT_AFTER: |
76
|
6 |
|
$heading->appendChild($headingLinkAnchor); |
77
|
|
|
|
78
|
6 |
|
return; |
79
|
|
|
default: |
80
|
|
|
throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'"); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|