HeadingPermalinkProcessor.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
/*
* 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\HeadingPermalink;
use League\CommonMark\Environment\EnvironmentAwareInterface;
use League\CommonMark\Environment\EnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Node\NodeIterator;
use League\CommonMark\Node\RawMarkupContainerInterface;
use League\CommonMark\Node\StringContainerHelper;
use League\CommonMark\Normalizer\TextNormalizerInterface;
use League\Config\ConfigurationInterface;
use League\Config\Exception\InvalidConfigurationException;
/**
* Searches the Document for Heading elements and adds HeadingPermalinks to each one
*/
final class HeadingPermalinkProcessor implements EnvironmentAwareInterface
{
public const INSERT_BEFORE = 'before';
public const INSERT_AFTER = 'after';
public const INSERT_NONE = 'none';
/** @psalm-readonly-allow-private-mutation */
private TextNormalizerInterface $slugNormalizer;
/** @psalm-readonly-allow-private-mutation */
private ConfigurationInterface $config;
public function setEnvironment(EnvironmentInterface $environment): void
{
$this->config = $environment->getConfiguration();
$this->slugNormalizer = $environment->getSlugNormalizer();
}
public function __invoke(DocumentParsedEvent $e): void
{
$min = (int) $this->config->get('heading_permalink/min_heading_level');
$max = (int) $this->config->get('heading_permalink/max_heading_level');
$applyToHeading = (bool) $this->config->get('heading_permalink/apply_id_to_heading');
$idPrefix = (string) $this->config->get('heading_permalink/id_prefix');
$slugLength = (int) $this->config->get('slug_normalizer/max_length');
$headingClass = (string) $this->config->get('heading_permalink/heading_class');
if ($idPrefix !== '') {
$idPrefix .= '-';
}
foreach ($e->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
if ($node instanceof Heading && $node->getLevel() >= $min && $node->getLevel() <= $max) {
$this->addHeadingLink($node, $slugLength, $idPrefix, $applyToHeading, $headingClass);
}
}
}
private function addHeadingLink(Heading $heading, int $slugLength, string $idPrefix, bool $applyToHeading, string $headingClass): void
{
$text = StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]);
$slug = $this->slugNormalizer->normalize($text, [
'node' => $heading,
'length' => $slugLength,
]);
if ($applyToHeading) {
$heading->data->set('attributes/id', $idPrefix . $slug);
}
if ($headingClass !== '') {
$heading->data->append('attributes/class', $headingClass);
}
$headingLinkAnchor = new HeadingPermalink($slug);
switch ($this->config->get('heading_permalink/insert')) {
case self::INSERT_BEFORE:
$heading->prependChild($headingLinkAnchor);
return;
case self::INSERT_AFTER:
$heading->appendChild($headingLinkAnchor);
return;
case self::INSERT_NONE:
return;
default:
throw new InvalidConfigurationException("Invalid configuration value for heading_permalink/insert; expected 'before', 'after', or 'none'");
}
}
}