From b21323e9e24c8dfbd32c7f7e2f9031a390fac771 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Fri, 15 Mar 2024 11:15:36 +0100 Subject: [PATCH] Commonmark --- HighlightCodeBlockRendererTest.php | 31 +++++++++++++++ README.md | 28 ++++++++++++- composer.json | 4 +- src/CommonMark/HighlightCodeBlockRenderer.php | 39 +++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 HighlightCodeBlockRendererTest.php create mode 100644 src/CommonMark/HighlightCodeBlockRenderer.php diff --git a/HighlightCodeBlockRendererTest.php b/HighlightCodeBlockRendererTest.php new file mode 100644 index 0000000..fd66501 --- /dev/null +++ b/HighlightCodeBlockRendererTest.php @@ -0,0 +1,31 @@ +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()); + } +} diff --git a/README.md b/README.md index a1f849e..8a079ca 100644 --- a/README.md +++ b/README.md @@ -50,4 +50,30 @@ Or you can build your own with just a couple of classes: } ``` -You should style your any pre tags yourself. \ No newline at end of file +You should style your any pre tags yourself. + +## CommonMark integration + +If you're using `league/commonmark`, you can add highlight support to codeblocks like so: + +```php +use League\CommonMark\Environment\Environment; +use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; +use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; +use League\CommonMark\MarkdownConverter; +use Tempest\Highlight\CommonMark\HighlightCodeBlockRenderer; + +$environment = new Environment(); + +$environment + ->addExtension(new CommonMarkCoreExtension()) + ->addRenderer(FencedCode::class, new HighlightCodeBlockRenderer()); + +$markdown = new MarkdownConverter($environment); +``` + +Keep in mind that you need to manually install `league/commonmark`: + +```php +composer require league/commonmark; +``` \ No newline at end of file diff --git a/composer.json b/composer.json index 71ec92e..8945841 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,9 @@ "require-dev": { "phpunit/phpunit": "^11.0", "phpstan/phpstan": "^1.10.0", - "friendsofphp/php-cs-fixer": "^3.21" + "friendsofphp/php-cs-fixer": "^3.21", + "league/commonmark": "^2.4", + "larapack/dd": "^1.1" }, "scripts": { "phpunit": "vendor/bin/phpunit --display-warnings --display-skipped --display-deprecations --display-errors --display-notices", diff --git a/src/CommonMark/HighlightCodeBlockRenderer.php b/src/CommonMark/HighlightCodeBlockRenderer.php new file mode 100644 index 0000000..4f11e64 --- /dev/null +++ b/src/CommonMark/HighlightCodeBlockRenderer.php @@ -0,0 +1,39 @@ +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; + } +}