Skip to content

Commit 0b3fefc

Browse files
[TableOfContents] Add a label option
By default no label is shown, but if one is set it is added as a `<strong>` before the first item in the table of contents.
1 parent 6fbb36d commit 0b3fefc

File tree

7 files changed

+77
-1
lines changed

7 files changed

+77
-1
lines changed

src/Extension/TableOfContents/TableOfContentsBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function onDocumentParsed(DocumentParsedEvent $event): void
4343
(int) $this->config->get('table_of_contents/min_heading_level'),
4444
(int) $this->config->get('table_of_contents/max_heading_level'),
4545
(string) $this->config->get('heading_permalink/fragment_prefix'),
46+
(string) $this->config->get('table_of_contents/label'),
4647
);
4748

4849
$toc = $generator->generate($document);

src/Extension/TableOfContents/TableOfContentsExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function configureSchema(ConfigurationBuilderInterface $builder): void
3535
'max_heading_level' => Expect::int()->min(1)->max(6)->default(6),
3636
'html_class' => Expect::string()->default('table-of-contents'),
3737
'placeholder' => Expect::anyOf(Expect::string(), Expect::null())->default(null),
38+
'label' => Expect::anyOf(Expect::string(), Expect::null())->default(null),
3839
]));
3940
}
4041

src/Extension/TableOfContents/TableOfContentsGenerator.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
1919
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
2020
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
21+
use League\CommonMark\Extension\CommonMark\Node\Inline\Strong;
2122
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
2223
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
2324
use League\CommonMark\Extension\TableOfContents\Normalizer\AsIsNormalizerStrategy;
2425
use League\CommonMark\Extension\TableOfContents\Normalizer\FlatNormalizerStrategy;
2526
use League\CommonMark\Extension\TableOfContents\Normalizer\NormalizerStrategyInterface;
2627
use League\CommonMark\Extension\TableOfContents\Normalizer\RelativeNormalizerStrategy;
2728
use League\CommonMark\Node\Block\Document;
29+
use League\CommonMark\Node\Inline\Text;
2830
use League\CommonMark\Node\NodeIterator;
2931
use League\CommonMark\Node\RawMarkupContainerInterface;
3032
use League\CommonMark\Node\StringContainerHelper;
@@ -54,13 +56,17 @@ final class TableOfContentsGenerator implements TableOfContentsGeneratorInterfac
5456
/** @psalm-readonly */
5557
private string $fragmentPrefix;
5658

57-
public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel, string $fragmentPrefix)
59+
/** @psalm-readonly */
60+
private string $label;
61+
62+
public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel, string $fragmentPrefix, string $label)
5863
{
5964
$this->style = $style;
6065
$this->normalizationStrategy = $normalizationStrategy;
6166
$this->minHeadingLevel = $minHeadingLevel;
6267
$this->maxHeadingLevel = $maxHeadingLevel;
6368
$this->fragmentPrefix = $fragmentPrefix;
69+
$this->label = $label;
6470

6571
if ($fragmentPrefix !== '') {
6672
$this->fragmentPrefix .= '-';
@@ -111,6 +117,12 @@ public function generate(Document $document): ?TableOfContents
111117
return null;
112118
}
113119

120+
if ($this->label !== '') {
121+
$label = new Strong();
122+
$label->appendChild(new Text($this->label));
123+
$toc->prependChild($label);
124+
}
125+
114126
return $toc;
115127
}
116128

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<ul class="table-of-contents">
2+
<strong>Table of Contents</strong>
3+
<li><a href="#content-hello-world">Hello World!</a>
4+
<ul>
5+
<li><a href="#content-isnt-markdown-great">Isn't Markdown Great?</a></li>
6+
</ul>
7+
</li>
8+
</ul>
9+
<p>This is my document.</p>
10+
<h1><a id="content-hello-world" href="#content-hello-world" class="heading-permalink" aria-hidden="true" title="Permalink"></a>Hello World!</h1>
11+
<h2><a id="content-isnt-markdown-great" href="#content-isnt-markdown-great" class="heading-permalink" aria-hidden="true" title="Permalink"></a>Isn't Markdown Great?</h2>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
table_of_contents:
3+
label: Table of Contents
4+
---
5+
6+
This is my document.
7+
8+
# Hello World!
9+
10+
## Isn't Markdown Great?
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
table_of_contents:
3+
label: Table of Contents
4+
---
5+
6+
This is my document.
7+
8+
# Hello World!
9+
10+
## Isn't Markdown Great?
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document xmlns="http://commonmark.org/xml/1.0">
3+
<table_of_contents type="bullet" tight="false">
4+
<strong>
5+
<text>Table of Contents</text>
6+
</strong>
7+
<item>
8+
<link destination="#content-hello-world" title="">
9+
<text>Hello World!</text>
10+
</link>
11+
<list type="bullet" tight="false">
12+
<item>
13+
<link destination="#content-isnt-markdown-great" title="">
14+
<text>Isn't Markdown Great?</text>
15+
</link>
16+
</item>
17+
</list>
18+
</item>
19+
</table_of_contents>
20+
<paragraph>
21+
<text>This is my document.</text>
22+
</paragraph>
23+
<heading level="1">
24+
<heading_permalink slug="hello-world" />
25+
<text>Hello World!</text>
26+
</heading>
27+
<heading level="2">
28+
<heading_permalink slug="isnt-markdown-great" />
29+
<text>Isn't Markdown Great?</text>
30+
</heading>
31+
</document>

0 commit comments

Comments
 (0)