TableOfContentsBuilder   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
eloc 39
dl 0
loc 81
ccs 38
cts 40
cp 0.95
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfiguration() 0 3 1
B onDocumentParsed() 0 33 6
A insertBeforeFirstLinkedHeading() 0 8 5
A replacePlaceholders() 0 14 4
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\TableOfContents;
15
16
use League\CommonMark\Event\DocumentParsedEvent;
17
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
18
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
19
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
20
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
21
use League\CommonMark\Node\Block\Document;
22
use League\Config\ConfigurationAwareInterface;
23
use League\Config\ConfigurationInterface;
24
use League\Config\Exception\InvalidConfigurationException;
25
26
final class TableOfContentsBuilder implements ConfigurationAwareInterface
27
{
28
    public const POSITION_TOP             = 'top';
29
    public const POSITION_BEFORE_HEADINGS = 'before-headings';
30
    public const POSITION_PLACEHOLDER     = 'placeholder';
31
32
    /**
33
     * @var ConfigurationInterface
34
     *
35
     * @psalm-readonly-allow-private-mutation
36
     */
37
    private $config;
38
39 42
    public function onDocumentParsed(DocumentParsedEvent $event): void
40
    {
41 42
        $document = $event->getDocument();
42
43 42
        $generator = new TableOfContentsGenerator(
44 42
            (string) $this->config->get('table_of_contents/style'),
45 42
            (string) $this->config->get('table_of_contents/normalize'),
46 42
            (int) $this->config->get('table_of_contents/min_heading_level'),
47 42
            (int) $this->config->get('table_of_contents/max_heading_level')
48
        );
49
50 42
        $toc = $generator->generate($document);
51 42
        if ($toc === null) {
52
            // No linkable headers exist, so no TOC could be generated
53 6
            return;
54
        }
55
56
        // Add custom CSS class(es), if defined
57 39
        $class = $this->config->get('table_of_contents/html_class');
58 39
        if ($class !== null) {
59 39
            $toc->data->append('attributes/class', $class);
60
        }
61
62
        // Add the TOC to the Document
63 39
        $position = $this->config->get('table_of_contents/position');
64 39
        if ($position === self::POSITION_TOP) {
65 33
            $document->prependChild($toc);
66 6
        } elseif ($position === self::POSITION_BEFORE_HEADINGS) {
67 3
            $this->insertBeforeFirstLinkedHeading($document, $toc);
68 3
        } elseif ($position === self::POSITION_PLACEHOLDER) {
69 3
            $this->replacePlaceholders($document, $toc);
70
        } else {
71
            throw InvalidConfigurationException::forConfigOption('table_of_contents/position', $position);
72
        }
73 39
    }
74
75 3
    private function insertBeforeFirstLinkedHeading(Document $document, TableOfContents $toc): void
76
    {
77 3
        $walker = $document->walker();
78 3
        while ($event = $walker->next()) {
79 3
            if ($event->isEntering() && ($node = $event->getNode()) instanceof HeadingPermalink && ($parent = $node->parent()) instanceof Heading) {
80 3
                $parent->insertBefore($toc);
81
82 3
                return;
83
            }
84
        }
85
    }
86
87 3
    private function replacePlaceholders(Document $document, TableOfContents $toc): void
88
    {
89 3
        $walker = $document->walker();
90 3
        while ($event = $walker->next()) {
91
            // Add the block once we find a placeholder (and we're about to leave it)
92 3
            if (! $event->getNode() instanceof TableOfContentsPlaceholder) {
93 3
                continue;
94
            }
95
96 3
            if ($event->isEntering()) {
97 3
                continue;
98
            }
99
100 3
            $event->getNode()->replaceWith(clone $toc);
101
        }
102 3
    }
103
104 42
    public function setConfiguration(ConfigurationInterface $configuration): void
105
    {
106 42
        $this->config = $configuration;
107 42
    }
108
}
109