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\TableOfContents\Node\TableOfContentsPlaceholder; |
17
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
18
|
|
|
use League\CommonMark\Parser\Block\AbstractBlockContinueParser; |
19
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
20
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
21
|
|
|
use League\CommonMark\Parser\Block\BlockStart; |
22
|
|
|
use League\CommonMark\Parser\Block\BlockStartParserInterface; |
23
|
|
|
use League\CommonMark\Parser\Cursor; |
24
|
|
|
use League\CommonMark\Parser\MarkdownParserStateInterface; |
25
|
|
|
use League\Config\ConfigurationAwareInterface; |
26
|
|
|
use League\Config\ConfigurationInterface; |
27
|
|
|
|
28
|
|
|
final class TableOfContentsPlaceholderParser extends AbstractBlockContinueParser |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var TableOfContentsPlaceholder |
32
|
|
|
* |
33
|
|
|
* @psalm-readonly |
34
|
|
|
*/ |
35
|
|
|
private $block; |
36
|
|
|
|
37
|
3 |
|
public function __construct() |
38
|
|
|
{ |
39
|
3 |
|
$this->block = new TableOfContentsPlaceholder(); |
40
|
3 |
|
} |
41
|
|
|
|
42
|
3 |
|
public function getBlock(): AbstractBlock |
43
|
|
|
{ |
44
|
3 |
|
return $this->block; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
48
|
|
|
{ |
49
|
3 |
|
return BlockContinue::none(); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public static function blockStartParser(): BlockStartParserInterface |
53
|
|
|
{ |
54
|
2 |
|
return new class () implements BlockStartParserInterface, ConfigurationAwareInterface { |
55
|
|
|
/** |
56
|
|
|
* @var ConfigurationInterface |
57
|
|
|
* |
58
|
|
|
* @psalm-readonly-allow-private-mutation |
59
|
|
|
*/ |
60
|
|
|
private $config; |
61
|
|
|
|
62
|
1 |
|
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart |
63
|
|
|
{ |
64
|
3 |
|
$placeholder = $this->config->get('table_of_contents/placeholder'); |
65
|
3 |
|
if ($placeholder === null) { |
66
|
|
|
return BlockStart::none(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// The placeholder must be the only thing on the line |
70
|
3 |
|
if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) { |
71
|
3 |
|
return BlockStart::none(); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return BlockStart::of(new TableOfContentsPlaceholderParser())->at($cursor); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function setConfiguration(ConfigurationInterface $configuration): void |
78
|
|
|
{ |
79
|
3 |
|
$this->config = $configuration; |
80
|
3 |
|
} |
81
|
|
|
}; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|