Skip to content

Commit 7d371a1

Browse files
committed
add commonmark code extension to highlight code blocks with highlight.js
1 parent 4588d2a commit 7d371a1

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

ux.symfony.com/config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ services:
2424
# add more service definitions when explicit configuration is needed
2525
# please note that last definitions always *replace* previous ones
2626

27+
App\Service\CommonMark\CodeExtension:
28+
tags: ['twig.markdown.league_extension']
29+
2730
League\CommonMark\Extension\ExternalLink\ExternalLinkExtension:
2831
tags: ['twig.markdown.league_extension']
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\Service\CommonMark;
13+
14+
use League\CommonMark\Environment\EnvironmentBuilderInterface;
15+
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
16+
use League\CommonMark\Extension\ExtensionInterface;
17+
18+
/**
19+
* @author Kevin Bond <[email protected]>
20+
*/
21+
final class CodeExtension implements ExtensionInterface
22+
{
23+
public function register(EnvironmentBuilderInterface $environment): void
24+
{
25+
$environment->addRenderer(FencedCode::class, new CodeRenderer(), 10);
26+
}
27+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\Service\CommonMark;
13+
14+
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
15+
use League\CommonMark\Node\Node;
16+
use League\CommonMark\Renderer\ChildNodeRendererInterface;
17+
use League\CommonMark\Renderer\NodeRendererInterface;
18+
use League\CommonMark\Util\HtmlElement;
19+
use League\CommonMark\Util\Xml;
20+
21+
/**
22+
* @author Kevin Bond <[email protected]>
23+
*/
24+
final class CodeRenderer implements NodeRendererInterface
25+
{
26+
/**
27+
* @param FencedCode $node
28+
*/
29+
public function render(Node $node, ChildNodeRendererInterface $childRenderer): string|\Stringable|null
30+
{
31+
$codeAttr = ['data-code-highlighter-target' => 'codeBlock'];
32+
33+
if ($lang = $node->getInfo()) {
34+
$codeAttr['class'] = 'language-'.$lang;
35+
}
36+
37+
return new HtmlElement(
38+
'div',
39+
['class' => 'Terminal_body mb-3'],
40+
new HtmlElement(
41+
'div',
42+
['class' => 'Terminal_content'],
43+
new HtmlElement(
44+
'pre',
45+
['data-controller' => 'code-highlighter'],
46+
new HtmlElement('code', $codeAttr, Xml::escape($node->getLiteral()))
47+
)
48+
)
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)