-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Table of Contents to be placed manually
- Loading branch information
1 parent
d96e776
commit fe22268
Showing
15 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Extension/TableOfContents/Node/TableOfContentsPlaceholder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the league/commonmark package. | ||
* | ||
* (c) Colin O'Dell <colinodell@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\CommonMark\Extension\TableOfContents\Node; | ||
|
||
use League\CommonMark\Block\Element\AbstractBlock; | ||
use League\CommonMark\Cursor; | ||
|
||
final class TableOfContentsPlaceholder extends AbstractBlock | ||
{ | ||
public function canContain(AbstractBlock $block): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isCode(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function matchesNextLine(Cursor $cursor): bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Extension/TableOfContents/TableOfContentsPlaceholderParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the league/commonmark package. | ||
* | ||
* (c) Colin O'Dell <colinodell@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\CommonMark\Extension\TableOfContents; | ||
|
||
use League\CommonMark\Block\Parser\BlockParserInterface; | ||
use League\CommonMark\ContextInterface; | ||
use League\CommonMark\Cursor; | ||
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder; | ||
use League\CommonMark\Util\ConfigurationAwareInterface; | ||
use League\CommonMark\Util\ConfigurationInterface; | ||
|
||
final class TableOfContentsPlaceholderParser implements BlockParserInterface, ConfigurationAwareInterface | ||
{ | ||
/** @var ConfigurationInterface */ | ||
private $config; | ||
|
||
public function parse(ContextInterface $context, Cursor $cursor): bool | ||
{ | ||
$placeholder = $this->config->get('table_of_contents/placeholder'); | ||
if ($placeholder === null) { | ||
return false; | ||
} | ||
|
||
// The placeholder must be the only thing on the line | ||
if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) { | ||
return false; | ||
} | ||
|
||
$context->addBlock(new TableOfContentsPlaceholder()); | ||
|
||
return true; | ||
} | ||
|
||
public function setConfiguration(ConfigurationInterface $configuration) | ||
{ | ||
$this->config = $configuration; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Extension/TableOfContents/TableOfContentsPlaceholderRenderer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the league/commonmark package. | ||
* | ||
* (c) Colin O'Dell <colinodell@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\CommonMark\Extension\TableOfContents; | ||
|
||
use League\CommonMark\Block\Element\AbstractBlock; | ||
use League\CommonMark\Block\Renderer\BlockRendererInterface; | ||
use League\CommonMark\ElementRendererInterface; | ||
|
||
final class TableOfContentsPlaceholderRenderer implements BlockRendererInterface | ||
{ | ||
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false) | ||
{ | ||
return '<!-- table of contents -->'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/functional/Extension/TableOfContents/data/position-placeholder-missing.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<p>This is my document.</p> | ||
<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink"><svg class="heading-permalink-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Hello World!</h1> | ||
<h2><a id="user-content-isnt-markdown-great" href="#isnt-markdown-great" name="isnt-markdown-great" class="heading-permalink" aria-hidden="true" title="Permalink"><svg class="heading-permalink-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Isn't Markdown Great?</h2> |
5 changes: 5 additions & 0 deletions
5
tests/functional/Extension/TableOfContents/data/position-placeholder-missing.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This is my document. | ||
|
||
# Hello World! | ||
|
||
## Isn't Markdown Great? |
32 changes: 32 additions & 0 deletions
32
tests/functional/Extension/TableOfContents/data/position-placeholder-multiple.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<p>This is my document.</p> | ||
<ul class="table-of-contents"> | ||
<li> | ||
<p><a href="#another-copy-of-my-toc-is-here">Another copy of my TOC is here</a></p> | ||
<ul> | ||
<li> | ||
<p><a href="#this-contains-something-that-looks-like-a-placeholder-but-actually-isnt">This contains something that looks like a placeholder but actually isn't</a></p> | ||
</li> | ||
<li> | ||
<p><a href="#just-a-link-reference-down-here">Just a link reference down here</a></p> | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> | ||
<h1><a id="user-content-another-copy-of-my-toc-is-here" href="#another-copy-of-my-toc-is-here" name="another-copy-of-my-toc-is-here" class="heading-permalink" aria-hidden="true" title="Permalink"><svg class="heading-permalink-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Another copy of my TOC is here</h1> | ||
<ul class="table-of-contents"> | ||
<li> | ||
<p><a href="#another-copy-of-my-toc-is-here">Another copy of my TOC is here</a></p> | ||
<ul> | ||
<li> | ||
<p><a href="#this-contains-something-that-looks-like-a-placeholder-but-actually-isnt">This contains something that looks like a placeholder but actually isn't</a></p> | ||
</li> | ||
<li> | ||
<p><a href="#just-a-link-reference-down-here">Just a link reference down here</a></p> | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> | ||
<h2><a id="user-content-this-contains-something-that-looks-like-a-placeholder-but-actually-isnt" href="#this-contains-something-that-looks-like-a-placeholder-but-actually-isnt" name="this-contains-something-that-looks-like-a-placeholder-but-actually-isnt" class="heading-permalink" aria-hidden="true" title="Permalink"><svg class="heading-permalink-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>This contains something that looks like a placeholder but actually isn't</h2> | ||
<p>This is not a placeholder, but a link reference: <a href="https://www.example.com" title="Link Reference">TOC</a></p> | ||
<p><a href="https://www.example.com" title="Link Reference">TOC</a> This should also be handled as a link reference</p> | ||
<h2><a id="user-content-just-a-link-reference-down-here" href="#just-a-link-reference-down-here" name="just-a-link-reference-down-here" class="heading-permalink" aria-hidden="true" title="Permalink"><svg class="heading-permalink-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Just a link reference down here</h2> |
17 changes: 17 additions & 0 deletions
17
tests/functional/Extension/TableOfContents/data/position-placeholder-multiple.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
This is my document. | ||
|
||
[TOC] | ||
|
||
# Another copy of my TOC is here | ||
|
||
[TOC] | ||
|
||
## This contains something that looks like a placeholder but actually isn't | ||
|
||
This is not a placeholder, but a link reference: [TOC] | ||
|
||
[TOC] This should also be handled as a link reference | ||
|
||
## Just a link reference down here | ||
|
||
[TOC]: https://www.example.com "Link Reference" |
3 changes: 3 additions & 0 deletions
3
tests/functional/Extension/TableOfContents/data/position-placeholder-with-no-toc.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<p>Here is my placeholder:</p> | ||
<!-- table of contents --> | ||
<p>But I don't actually have any heading elements here!</p> |
5 changes: 5 additions & 0 deletions
5
...s/functional/Extension/TableOfContents/data/position-placeholder-with-no-toc.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Here is my placeholder: | ||
|
||
[TOC] | ||
|
||
But I don't actually have any heading elements here! |