-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
4 changed files
with
100 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Tempest\Highlight\CommonMark; | ||
|
||
use League\CommonMark\Environment\Environment; | ||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; | ||
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; | ||
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension; | ||
use League\CommonMark\MarkdownConverter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HighlightCodeBlockRendererTest extends TestCase | ||
{ | ||
public function test_commonmark(): void | ||
{ | ||
$environment = new Environment(); | ||
|
||
$environment | ||
->addExtension(new CommonMarkCoreExtension()) | ||
->addExtension(new FrontMatterExtension()) | ||
->addRenderer(FencedCode::class, new \Tempest\Highlight\CommonMark\HighlightCodeBlockRenderer()); | ||
|
||
$markdown = new MarkdownConverter($environment); | ||
|
||
$parsed = $markdown->convert("```php | ||
class Foo {} | ||
```"); | ||
|
||
$this->assertStringContainsString('hl-keyword', $parsed->getContent()); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Tempest\Highlight\CommonMark; | ||
|
||
use InvalidArgumentException; | ||
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; | ||
use League\CommonMark\Extension\CommonMark\Renderer\Block\FencedCodeRenderer as BaseFencedCodeRenderer; | ||
use League\CommonMark\Node\Node; | ||
use League\CommonMark\Renderer\ChildNodeRendererInterface; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
use Tempest\Highlight\Highlighter; | ||
|
||
class HighlightCodeBlockRenderer implements NodeRendererInterface | ||
{ | ||
public function render(Node $node, ChildNodeRendererInterface $childRenderer) | ||
{ | ||
if (! $node instanceof FencedCode) { | ||
throw new InvalidArgumentException('Block must be instance of ' . FencedCode::class); | ||
} | ||
|
||
$renderer = new BaseFencedCodeRenderer(); | ||
|
||
$language = $node->getInfoWords()[0] ?? 'txt'; | ||
|
||
$highlight = new Highlighter(); | ||
|
||
/** @var \League\CommonMark\Util\HtmlElement $codeBlock */ | ||
$codeBlock = $renderer->render($node, $childRenderer); | ||
|
||
/** @var string $codeText */ | ||
$codeText = $codeBlock->getContents(false)->getContents(); | ||
|
||
$codeBlock->setContents($highlight->parse($codeText, $language)); | ||
|
||
$codeBlock->setContents($codeBlock->getContents()); | ||
|
||
return $codeBlock; | ||
} | ||
} |