HeadingPermalinkRenderer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 20
dl 0
loc 49
ccs 17
cts 18
cp 0.9444
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 26 3
A setConfiguration() 0 3 1
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\Configuration\ConfigurationAwareInterface;
17
use League\CommonMark\Configuration\ConfigurationInterface;
18
use League\CommonMark\Node\Node;
19
use League\CommonMark\Renderer\ChildNodeRendererInterface;
20
use League\CommonMark\Renderer\NodeRendererInterface;
21
use League\CommonMark\Util\HtmlElement;
22
23
/**
24
 * Renders the HeadingPermalink elements
25
 */
26
final class HeadingPermalinkRenderer implements NodeRendererInterface, ConfigurationAwareInterface
27
{
28
    public const DEFAULT_SYMBOL = '¶';
29
30
    /**
31
     * @var ConfigurationInterface
32
     *
33
     * @psalm-readonly-allow-private-mutation
34
     */
35
    private $config;
36
37 72
    public function setConfiguration(ConfigurationInterface $configuration): void
38
    {
39 72
        $this->config = $configuration;
40 72
    }
41
42
    /**
43
     * @param HeadingPermalink $node
44
     *
45
     * {@inheritdoc}
46
     *
47
     * @psalm-suppress MoreSpecificImplementedParamType
48
     */
49 66
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
50
    {
51 66
        if (! $node instanceof HeadingPermalink) {
52
            throw new \InvalidArgumentException('Incompatible node type: ' . \get_class($node));
53
        }
54
55 66
        $slug = $node->getSlug();
56
57 66
        $idPrefix = (string) $this->config->get('heading_permalink/id_prefix', 'user-content');
58 66
        if ($idPrefix !== '') {
59 63
            $idPrefix .= '-';
60
        }
61
62
        $attrs = [
63 66
            'id'          => $idPrefix . $slug,
64 66
            'href'        => '#' . $slug,
65 66
            'name'        => $slug,
66 66
            'class'       => $this->config->get('heading_permalink/html_class', 'heading-permalink'),
67 66
            'aria-hidden' => 'true',
68 66
            'title'       => $this->config->get('heading_permalink/title', 'Permalink'),
69
        ];
70
71 66
        $symbol = $this->config->get('heading_permalink/symbol', self::DEFAULT_SYMBOL);
72
        \assert(\is_string($symbol));
73
74 66
        return new HtmlElement('a', $attrs, \htmlspecialchars($symbol), false);
75
    }
76
}
77