-
-
Notifications
You must be signed in to change notification settings - Fork 315
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
6 changed files
with
114 additions
and
9 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
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,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Service\CommonMark; | ||
|
||
use League\CommonMark\Environment\EnvironmentBuilderInterface; | ||
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; | ||
use League\CommonMark\Extension\ExtensionInterface; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class CodeExtension implements ExtensionInterface | ||
{ | ||
public function register(EnvironmentBuilderInterface $environment): void | ||
{ | ||
$environment->addRenderer(FencedCode::class, new CodeRenderer(), 10); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Service\CommonMark; | ||
|
||
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; | ||
use League\CommonMark\Node\Node; | ||
use League\CommonMark\Renderer\ChildNodeRendererInterface; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
use League\CommonMark\Util\HtmlElement; | ||
use League\CommonMark\Util\Xml; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class CodeRenderer implements NodeRendererInterface | ||
{ | ||
/** | ||
* @param FencedCode $node | ||
*/ | ||
public function render(Node $node, ChildNodeRendererInterface $childRenderer): string|\Stringable|null | ||
{ | ||
$codeAttr = ['data-code-highlighter-target' => 'codeBlock']; | ||
|
||
if ($lang = $node->getInfo()) { | ||
$codeAttr['class'] = 'language-'.$lang; | ||
} | ||
|
||
return new HtmlElement( | ||
'div', | ||
['class' => 'Terminal_body mb-3'], | ||
new HtmlElement( | ||
'div', | ||
['class' => 'Terminal_content'], | ||
new HtmlElement( | ||
'pre', | ||
['data-controller' => 'code-highlighter'], | ||
new HtmlElement('code', $codeAttr, Xml::escape($node->getLiteral())) | ||
) | ||
) | ||
); | ||
} | ||
} |
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