Skip to content
Highlight code blocks with league/commonmark
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Use the correct language... Jun 14, 2019
.editorconfig First commit Nov 28, 2018
.gitattributes First commit Nov 28, 2018
.travis.yml wip Feb 8, 2019 Update Jun 16, 2019 First commit Nov 28, 2018 Update Feb 8, 2019

Highlight your markdown code blocks with league/commonmark

Latest Version on Packagist Build Status Quality Score [StyleCI] Total Downloads

A block renderer for league/commonmark to highlight code blocks using scrivo/highlight.php.

highlight.php is a server side code highlighter written in PHP that currently supports 185 languages. It's a port of highlight.js by Ivan Sagalaev that makes full use of the language and style definitions of the original JavaScript project.

The output html is compatible with highlight.js themes, which you can explore on

What are the benefits of using this package over highlight.js?

  • Less JavaScript, which means faster page loads
  • 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 FencedCodeRenderer and 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']);


composer test


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 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.


Support us

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.

You can’t perform that action at this time.