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