TableOfContentsGenerator::getHeadingLinks()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 2
b 0
f 0
cc 4
nc 3
nop 1
crap 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\Extension\CommonMark\Node\Block\Heading;
17
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
18
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
19
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
20
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
21
use League\CommonMark\Extension\CommonMark\Node\Inline\HtmlInline;
22
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
23
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
24
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
25
use League\CommonMark\Extension\TableOfContents\Normalizer\AsIsNormalizerStrategy;
26
use League\CommonMark\Extension\TableOfContents\Normalizer\FlatNormalizerStrategy;
27
use League\CommonMark\Extension\TableOfContents\Normalizer\NormalizerStrategyInterface;
28
use League\CommonMark\Extension\TableOfContents\Normalizer\RelativeNormalizerStrategy;
29
use League\CommonMark\Node\Block\Document;
30
use League\CommonMark\Node\StringContainerHelper;
31
use League\Config\Exception\InvalidConfigurationException;
32
33
final class TableOfContentsGenerator implements TableOfContentsGeneratorInterface
34
{
35
    public const STYLE_BULLET  = ListBlock::TYPE_BULLET;
36
    public const STYLE_ORDERED = ListBlock::TYPE_ORDERED;
37
38
    public const NORMALIZE_DISABLED = 'as-is';
39
    public const NORMALIZE_RELATIVE = 'relative';
40
    public const NORMALIZE_FLAT     = 'flat';
41
42
    /**
43
     * @var string
44
     *
45
     * @psalm-readonly
46
     */
47
    private $style;
48
49
    /**
50
     * @var string
51
     *
52
     * @psalm-readonly
53
     */
54
    private $normalizationStrategy;
55
56
    /**
57
     * @var int
58
     *
59
     * @psalm-readonly
60
     */
61
    private $minHeadingLevel;
62
63
    /**
64
     * @var int
65
     *
66
     * @psalm-readonly
67
     */
68
    private $maxHeadingLevel;
69
70 42
    public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel)
71
    {
72 42
        $this->style                 = $style;
73 42
        $this->normalizationStrategy = $normalizationStrategy;
74 42
        $this->minHeadingLevel       = $minHeadingLevel;
75 42
        $this->maxHeadingLevel       = $maxHeadingLevel;
76 42
    }
77
78 42
    public function generate(Document $document): ?TableOfContents
79
    {
80 42
        $toc = $this->createToc($document);
81
82 42
        $normalizer = $this->getNormalizer($toc);
83
84 42
        $firstHeading = null;
85
86 42
        foreach ($this->getHeadingLinks($document) as $headingLink) {
87 39
            $heading = $headingLink->parent();
88
            // Make sure this is actually tied to a heading
89 39
            if (! $heading instanceof Heading) {
90
                continue;
91
            }
92
93
            // Skip any headings outside the configured min/max levels
94 39
            if ($heading->getLevel() < $this->minHeadingLevel || $heading->getLevel() > $this->maxHeadingLevel) {
95 3
                continue;
96
            }
97
98
            // Keep track of the first heading we see - we might need this later
99 39
            $firstHeading = $firstHeading ?? $heading;
100
101
            // Keep track of the start and end lines
102 39
            $toc->setStartLine($firstHeading->getStartLine());
103 39
            $toc->setEndLine($heading->getEndLine());
104
105
            // Create the new link
106 39
            $link = new Link('#' . $headingLink->getSlug(), StringContainerHelper::getChildText($heading, [HtmlBlock::class, HtmlInline::class]));
107
108 39
            $listItem = new ListItem($toc->getListData());
109 39
            $listItem->setStartLine($heading->getStartLine());
110 39
            $listItem->setEndLine($heading->getEndLine());
111 39
            $listItem->appendChild($link);
112
113
            // Add it to the correct place
114 39
            $normalizer->addItem($heading->getLevel(), $listItem);
115
        }
116
117
        // Don't add the TOC if no headings were present
118 42
        if (! $toc->hasChildren() || $firstHeading === null) {
119 6
            return null;
120
        }
121
122 39
        return $toc;
123
    }
124
125 42
    private function createToc(Document $document): TableOfContents
126
    {
127 42
        $listData = new ListData();
128
129 42
        if ($this->style === self::STYLE_BULLET) {
130 39
            $listData->type = ListBlock::TYPE_BULLET;
131 3
        } elseif ($this->style === self::STYLE_ORDERED) {
132 3
            $listData->type = ListBlock::TYPE_ORDERED;
133
        } else {
134
            throw new InvalidConfigurationException(\sprintf('Invalid table of contents list style: "%s"', $this->style));
135
        }
136
137 42
        $toc = new TableOfContents($listData);
138
139 42
        $toc->setStartLine($document->getStartLine());
140 42
        $toc->setEndLine($document->getEndLine());
141
142 42
        return $toc;
143
    }
144
145
    /**
146
     * @return iterable<HeadingPermalink>
147
     */
148 42
    private function getHeadingLinks(Document $document): iterable
149
    {
150 42
        $walker = $document->walker();
151 42
        while ($event = $walker->next()) {
152 42
            if ($event->isEntering() && ($node = $event->getNode()) instanceof HeadingPermalink) {
153 39
                yield $node;
154
            }
155
        }
156 42
    }
157
158 42
    private function getNormalizer(TableOfContents $toc): NormalizerStrategyInterface
159
    {
160 42
        switch ($this->normalizationStrategy) {
161 42
            case self::NORMALIZE_DISABLED:
162 3
                return new AsIsNormalizerStrategy($toc);
163 39
            case self::NORMALIZE_RELATIVE:
164 36
                return new RelativeNormalizerStrategy($toc);
165 3
            case self::NORMALIZE_FLAT:
166 3
                return new FlatNormalizerStrategy($toc);
167
            default:
168
                throw new InvalidConfigurationException(\sprintf('Invalid table of contents normalization strategy: "%s"', $this->normalizationStrategy));
169
        }
170
    }
171
}
172