TableOfContentsExtension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 25
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureSchema() 0 10 1
A register() 0 10 2
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\Environment\EnvironmentBuilderInterface;
17
use League\CommonMark\Event\DocumentParsedEvent;
18
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
19
use League\CommonMark\Extension\CommonMark\Renderer\Block\ListBlockRenderer;
20
use League\CommonMark\Extension\ConfigurableExtensionInterface;
21
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
22
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
23
use League\Config\ConfigurationBuilderInterface;
24
use Nette\Schema\Expect;
25
26
final class TableOfContentsExtension implements ConfigurableExtensionInterface
27
{
28
    public function configureSchema(ConfigurationBuilderInterface $builder): void
29
    {
30
        $builder->addSchema('table_of_contents', Expect::structure([
31
            'position' => Expect::anyOf(TableOfContentsBuilder::POSITION_BEFORE_HEADINGS, TableOfContentsBuilder::POSITION_PLACEHOLDER, TableOfContentsBuilder::POSITION_TOP)->default(TableOfContentsBuilder::POSITION_TOP),
32
            'style' => Expect::anyOf(ListBlock::TYPE_BULLET, ListBlock::TYPE_ORDERED)->default(ListBlock::TYPE_BULLET),
33
            'normalize' => Expect::anyOf(TableOfContentsGenerator::NORMALIZE_RELATIVE, TableOfContentsGenerator::NORMALIZE_FLAT, TableOfContentsGenerator::NORMALIZE_DISABLED)->default(TableOfContentsGenerator::NORMALIZE_RELATIVE),
34
            'min_heading_level' => Expect::int()->min(1)->max(6)->default(1),
35
            'max_heading_level' => Expect::int()->min(1)->max(6)->default(6),
36
            'html_class' => Expect::string()->default('table-of-contents'),
37
            'placeholder' => Expect::anyOf(Expect::string(), Expect::null())->default(null),
38
        ]));
39
    }
40
41
    public function register(EnvironmentBuilderInterface $environment): void
42
    {
43
        $environment->addRenderer(TableOfContents::class, new TableOfContentsRenderer(new ListBlockRenderer()));
44
        $environment->addEventListener(DocumentParsedEvent::class, [new TableOfContentsBuilder(), 'onDocumentParsed'], -150);
45
46
        // phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
47
        if ($environment->getConfiguration()->get('table_of_contents/position') === TableOfContentsBuilder::POSITION_PLACEHOLDER) {
48
            $environment->addBlockStartParser(TableOfContentsPlaceholderParser::blockStartParser(), 200);
49
            // If a placeholder cannot be replaced with a TOC element this renderer will ensure the parser won't error out
50
            $environment->addRenderer(TableOfContentsPlaceholder::class, new TableOfContentsPlaceholderRenderer());
51
        }
52
    }
53
}
54