-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
31 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Fig\Website; | ||
|
||
use League\CommonMark\Block\Element\FencedCode; | ||
use League\CommonMark\Block\Element\IndentedCode; | ||
use League\CommonMark\Environment; | ||
use League\CommonMark\EnvironmentInterface; | ||
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension; | ||
use League\CommonMark\Extension\Table\TableExtension; | ||
use Spatie\CommonMarkHighlighter\FencedCodeRenderer; | ||
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer; | ||
|
||
class CommonMarkEnvironmentFactory | ||
{ | ||
public static function create(): EnvironmentInterface | ||
{ | ||
$supportedLanguages = [ | ||
'php', | ||
'http', # inside PSR-7 | ||
]; | ||
|
||
$config = [ | ||
'heading_permalink' => [ | ||
'id_prefix' => '', | ||
'fragment_prefix' => '', | ||
'insert' => 'after', | ||
], | ||
]; | ||
|
||
$environment = Environment::createCommonMarkEnvironment(); | ||
$environment->mergeConfig($config); | ||
$environment | ||
->addExtension(new TableExtension()) | ||
->addExtension(new HeadingPermalinkExtension()) | ||
->addBlockRenderer(FencedCode::class, new FencedCodeRenderer($supportedLanguages)) | ||
->addBlockRenderer(IndentedCode::class, new IndentedCodeRenderer($supportedLanguages)) | ||
; | ||
|
||
return $environment; | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace Fig\Website; | ||
|
||
use League\CommonMark\DocParser; | ||
use League\CommonMark\DocParserInterface; | ||
use League\CommonMark\ElementRendererInterface; | ||
use League\CommonMark\EnvironmentInterface; | ||
use League\CommonMark\Extension\TableOfContents\TableOfContentsGenerator; | ||
use League\CommonMark\HtmlRenderer; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class TwigMarkdownTOCExtension extends AbstractExtension | ||
{ | ||
private readonly DocParserInterface $docParser; | ||
private readonly ElementRendererInterface $htmlRenderer; | ||
|
||
public function __construct( | ||
EnvironmentInterface $environment, | ||
) { | ||
$this->docParser = new DocParser($environment); | ||
$this->htmlRenderer = new HtmlRenderer($environment); | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new TwigFilter('markdown_toc', function(string $markdown): string { | ||
return $this->renderTOC($markdown); | ||
}), | ||
]; | ||
} | ||
|
||
private function renderTOC(string $markdown): string | ||
{ | ||
$generator = new TableOfContentsGenerator( | ||
TableOfContentsGenerator::STYLE_ORDERED, | ||
TableOfContentsGenerator::NORMALIZE_RELATIVE, | ||
minHeadingLevel: 2, | ||
maxHeadingLevel: 3, | ||
); | ||
|
||
$toc = $generator->generate($this->docParser->parse($markdown)); | ||
|
||
return $toc === null | ||
? '' | ||
: $this->htmlRenderer->renderBlock($toc, inTightList: true); | ||
} | ||
} |
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