Passed
Push — dependabot/github_actions/ride... ( bf7823 )
by
unknown
02:50
created

TableOfContentsBuilder::replacePlaceholders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
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\CommonMark\Node\NodeIterator;
23
use League\Config\ConfigurationAwareInterface;
24
use League\Config\ConfigurationInterface;
25
use League\Config\Exception\InvalidConfigurationException;
26
27
final class TableOfContentsBuilder implements ConfigurationAwareInterface
28
{
29
    public const POSITION_TOP             = 'top';
30
    public const POSITION_BEFORE_HEADINGS = 'before-headings';
31
    public const POSITION_PLACEHOLDER     = 'placeholder';
32
33
    /** @psalm-readonly-allow-private-mutation */
34
    private ConfigurationInterface $config;
35
36 66
    public function onDocumentParsed(DocumentParsedEvent $event): void
37
    {
38 66
        $document = $event->getDocument();
39
40 66
        $generator = new TableOfContentsGenerator(
41 66
            (string) $this->config->get('table_of_contents/style'),
42 66
            (string) $this->config->get('table_of_contents/normalize'),
43 66
            (int) $this->config->get('table_of_contents/min_heading_level'),
44 66
            (int) $this->config->get('table_of_contents/max_heading_level'),
45 66
            (string) $this->config->get('heading_permalink/fragment_prefix'),
46
        );
47
48 66
        $toc = $generator->generate($document);
49 66
        if ($toc === null) {
50
            // No linkable headers exist, so no TOC could be generated
51 8
            return;
52
        }
53
54
        // Add custom CSS class(es), if defined
55 58
        $class = $this->config->get('table_of_contents/html_class');
56 58
        if ($class !== null) {
57 58
            $toc->data->append('attributes/class', $class);
58
        }
59
60
        // Add the TOC to the Document
61 58
        $position = $this->config->get('table_of_contents/position');
62 58
        if ($position === self::POSITION_TOP) {
63 46
            $document->prependChild($toc);
64 12
        } elseif ($position === self::POSITION_BEFORE_HEADINGS) {
65 4
            $this->insertBeforeFirstLinkedHeading($document, $toc);
66 8
        } elseif ($position === self::POSITION_PLACEHOLDER) {
67 8
            $this->replacePlaceholders($document, $toc);
68
        } else {
69
            throw InvalidConfigurationException::forConfigOption('table_of_contents/position', $position);
70
        }
71
    }
72
73 4
    private function insertBeforeFirstLinkedHeading(Document $document, TableOfContents $toc): void
74
    {
75 4
        foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
76 4
            if (! $node instanceof Heading) {
77 4
                continue;
78
            }
79
80 4
            foreach ($node->children() as $child) {
81 4
                if ($child instanceof HeadingPermalink) {
82 4
                    $node->insertBefore($toc);
83
84 4
                    return;
85
                }
86
            }
87
        }
88
    }
89
90 8
    private function replacePlaceholders(Document $document, TableOfContents $toc): void
91
    {
92 8
        foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
93
            // Add the block once we find a placeholder
94 8
            if (! $node instanceof TableOfContentsPlaceholder) {
95 8
                continue;
96
            }
97
98 4
            $node->replaceWith(clone $toc);
99
        }
100
    }
101
102 66
    public function setConfiguration(ConfigurationInterface $configuration): void
103
    {
104 66
        $this->config = $configuration;
105
    }
106
}
107