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