Highlight your markdown code blocks with league/commonmark
The output html is compatible with highlight.js themes, which you can explore on highlightjs.org.
What are the benefits of using this package over highlight.js?
- No more flash of unstyled code blocks
This project was inspired by sixlive/parsedown-highlight.
You can install the package via composer:
composer require spatie/commonmark-highlighter
Create a custom CommonMark environment, and register the
IndentedCodeRender as described in the league/commonmark documentation.
use League\CommonMark\Block\Element\FencedCode; use League\CommonMark\Block\Element\IndentedCode; use League\CommonMark\CommonMarkConverter; use League\CommonMark\Environment; use Spatie\CommonMarkHighlighter\FencedCodeRenderer; use Spatie\CommonMarkHighlighter\IndentedCodeRenderer; $environment = Environment::createCommonMarkEnvironment(); $environment->addBlockRenderer(FencedCode::class, new FencedCodeRenderer()); $environment->addBlockRenderer(IndentedCode::class, new IndentedCodeRenderer()); $commonMarkConverter = new CommonMarkConverter(, $environment); echo $commonMarkConverter->convertToHtml($markdown);
The underlying highlight library recommends specifying a subset of languages for the auto-detection. You can pass an array of languages to any of the renderers.
new FencedCodeRenderer(['html', 'php', 'js']); new IndentedCodeCodeRenderer(['html', 'php', 'js']);
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email firstname.lastname@example.org instead of using the issue tracker.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
We publish all received postcards on our company website.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
The MIT License (MIT). Please see License File for more information.