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