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