blob: aaf135542f72af2510645ee61755077c06cda1cc (
plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
<?php
/*
* 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\Block\Element\Heading;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Exception\InvalidOptionException;
use League\CommonMark\Extension\HeadingPermalink\Slug\SlugGeneratorInterface as DeprecatedSlugGeneratorInterface;
use League\CommonMark\Inline\Element\Code;
use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Node\Node;
use League\CommonMark\Normalizer\SlugNormalizer;
use League\CommonMark\Normalizer\TextNormalizerInterface;
use League\CommonMark\Util\ConfigurationAwareInterface;
use League\CommonMark\Util\ConfigurationInterface;
/**
* Searches the Document for Heading elements and adds HeadingPermalinks to each one
*/
final class HeadingPermalinkProcessor implements ConfigurationAwareInterface
{
const INSERT_BEFORE = 'before';
const INSERT_AFTER = 'after';
/** @var TextNormalizerInterface|DeprecatedSlugGeneratorInterface */
private $slugNormalizer;
/** @var ConfigurationInterface */
private $config;
/**
* @param TextNormalizerInterface|DeprecatedSlugGeneratorInterface|null $slugNormalizer
*/
public function __construct($slugNormalizer = null)
{
if ($slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
@trigger_error(sprintf('Passing a %s into the %s constructor is deprecated; use a %s instead', DeprecatedSlugGeneratorInterface::class, self::class, TextNormalizerInterface::class), E_USER_DEPRECATED);
}
$this->slugNormalizer = $slugNormalizer ?? new SlugNormalizer();
}
public function setConfiguration(ConfigurationInterface $configuration)
{
$this->config = $configuration;
}
public function __invoke(DocumentParsedEvent $e): void
{
$this->useNormalizerFromConfigurationIfProvided();
$walker = $e->getDocument()->walker();
while ($event = $walker->next()) {
$node = $event->getNode();
if ($node instanceof Heading && $event->isEntering()) {
$this->addHeadingLink($node);
}
}
}
private function useNormalizerFromConfigurationIfProvided(): void
{
$generator = $this->config->get('heading_permalink/slug_normalizer');
if ($generator === null) {
return;
}
if (!($generator instanceof DeprecatedSlugGeneratorInterface || $generator instanceof TextNormalizerInterface)) {
throw new InvalidOptionException('The heading_permalink/slug_normalizer option must be an instance of ' . TextNormalizerInterface::class);
}
$this->slugNormalizer = $generator;
}
private function addHeadingLink(Heading $heading): void
{
$text = $this->getChildText($heading);
if ($this->slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
$slug = $this->slugNormalizer->createSlug($text);
} else {
$slug = $this->slugNormalizer->normalize($text, $heading);
}
$headingLinkAnchor = new HeadingPermalink($slug);
switch ($this->config->get('heading_permalink/insert', 'before')) {
case self::INSERT_BEFORE:
$heading->prependChild($headingLinkAnchor);
return;
case self::INSERT_AFTER:
$heading->appendChild($headingLinkAnchor);
return;
default:
throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
}
}
/**
* @deprecated Not needed in 2.0
*/
private function getChildText(Node $node): string
{
$text = '';
$walker = $node->walker();
while ($event = $walker->next()) {
if ($event->isEntering() && (($child = $event->getNode()) instanceof Text || $child instanceof Code)) {
$text .= $child->getContent();
}
}
return $text;
}
}
|