Passed
Pull Request — main (#1074)
by
unknown
03:00 queued 27s
created

HeadingPermalinkProcessor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 69
ccs 32
cts 33
cp 0.9697
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setEnvironment() 0 4 1
B addHeadingLink() 0 31 6
A __invoke() 0 16 6
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 110
    public function setEnvironment(EnvironmentInterface $environment): void
43
    {
44 110
        $this->config         = $environment->getConfiguration();
45 110
        $this->slugNormalizer = $environment->getSlugNormalizer();
46
    }
47
48 108
    public function __invoke(DocumentParsedEvent $e): void
49
    {
50 108
        $min            = (int) $this->config->get('heading_permalink/min_heading_level');
51 106
        $max            = (int) $this->config->get('heading_permalink/max_heading_level');
52 106
        $applyToHeading = (bool) $this->config->get('heading_permalink/apply_id_to_heading');
53 106
        $idPrefix       = (string) $this->config->get('heading_permalink/id_prefix');
54 106
        $slugLength     = (int) $this->config->get('slug_normalizer/max_length');
55 106
        $headingClass   = (string) $this->config->get('heading_permalink/heading_class');
56
57 106
        if ($idPrefix !== '') {
58 100
            $idPrefix .= '-';
59
        }
60
61 106
        foreach ($e->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
62 106
            if ($node instanceof Heading && $node->getLevel() >= $min && $node->getLevel() <= $max) {
63 98
                $this->addHeadingLink($node, $slugLength, $idPrefix, $applyToHeading, $headingClass);
64
            }
65
        }
66
    }
67
68 98
    private function addHeadingLink(Heading $heading, int $slugLength, string $idPrefix, bool $applyToHeading, string $headingClass): void
69
    {
70 98
        $text = StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]);
71 98
        $slug = $this->slugNormalizer->normalize($text, [
72 98
            'node' => $heading,
73 98
            'length' => $slugLength,
74 98
        ]);
75
76 98
        if ($applyToHeading) {
77 6
            $heading->data->set('attributes/id', $idPrefix . $slug);
78
        }
79
80 98
        if ($headingClass !== '') {
81 4
            $heading->data->append('attributes/class', $headingClass);
82
        }
83
84 98
        $headingLinkAnchor = new HeadingPermalink($slug);
85
86 98
        switch ($this->config->get('heading_permalink/insert')) {
87
            case self::INSERT_BEFORE:
88 90
                $heading->prependChild($headingLinkAnchor);
89
90 90
                return;
91
            case self::INSERT_AFTER:
92 6
                $heading->appendChild($headingLinkAnchor);
93
94 6
                return;
95
            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