PHPackages                             spatie/commonmark-highlighter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/commonmark-highlighter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/commonmark-highlighter
=============================

Highlight your markdown code blocks with league/commonmark

3.0.0(4y ago)138400.5k—3.8%1018MITPHPPHP ^7.4|^8.0CI passing

Since Nov 28Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/spatie/commonmark-highlighter)[ Packagist](https://packagist.org/packages/spatie/commonmark-highlighter)[ Docs](https://github.com/spatie/commonmark-highlighter)[ RSS](/packages/spatie-commonmark-highlighter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (12)Used By (18)

Highlight your markdown code blocks with league/commonmark
==========================================================

[](#highlight-your-markdown-code-blocks-with-leaguecommonmark)

[![Latest Version](https://camo.githubusercontent.com/9fc7df1ce11fd45079935195a512b5c5306900d6c8038f8739e755c3bbc95368/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7370617469652f636f6d6d6f6e6d61726b2d686967686c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://github.com/spatie/commonmark-highlighter/releases)[![Build Status](https://camo.githubusercontent.com/048fe992f224fae573f1e59473faab53557ad6ea89c5b1b6687bb661dfbfa887/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f636f6d6d6f6e6d61726b2d686967686c6967687465722f54657374732e7376673f7374796c653d666c61742d737175617265)](https://github.com/spatie/commonmark-highlighter/actions)[![Quality Score](https://camo.githubusercontent.com/e060b5d79c400fa573f4f31f50c40f618f978d593bff7e5c8c0568bbf32cb4d2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f636f6d6d6f6e6d61726b2d686967686c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/commonmark-highlighter)[![StyleCI](https://camo.githubusercontent.com/7b009cb8b2015ede08d0d4971475c2166cfc23d3f453da9c3bdb04cf43a3c6ed/68747470733a2f2f7374796c6563692e696f2f7265706f732f3135393531333331302f736869656c64)](https://styleci.io/repos/159513310)[![Total Downloads](https://camo.githubusercontent.com/d7df5975a0c5a0100caf191af69ab2d1b38ef9fefc665ad0f637cbbfcba458d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f636f6d6d6f6e6d61726b2d686967686c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/commonmark-highlighter)

A block renderer for [league/commonmark](https://github.com/thephpleague/commonmark) to highlight code blocks using [scrivo/highlight.php](https://github.com/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 [highlightjs.org](https://highlightjs.org/static/demo/).

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](https://github.com/sixlive/parsedown-highlight).

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/7f7e575ac936287d45ac1cbcb9140f04534b94a3cdced90ab1bee13345d10369/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f636f6d6d6f6e6d61726b2d686967686c6967687465722e6a70673f743d31)](https://spatie.be/github-ad-click/commonmark-highlighter)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/commonmark-highlighter
```

Usage
-----

[](#usage)

Create a custom CommonMark environment, and register the `FencedCodeRenderer` and `IndentedCodeRender` as described in the [league/commonmark documentation](https://commonmark.thephpleague.com/customization/block-rendering/).

```
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode;
use League\CommonMark\MarkdownConverter;
use Spatie\CommonMarkHighlighter\FencedCodeRenderer;
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer;

$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addRenderer(FencedCode::class, new FencedCodeRenderer());
$environment->addRenderer(IndentedCode::class, new IndentedCodeRenderer());

$markdownConverter = new MarkdownConverter($environment);

echo $markdownConverter->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 IndentedCodeRenderer(['html', 'php', 'js']);
```

### Highlighting specific lines

[](#highlighting-specific-lines)

Line numbers start at 1.

```php - Don't highlight any lines
```php{4} - Highlight just line 4
```php{4-6} - Highlight the range of lines from 4 to 6 (inclusive)
```php{1,5} - Highlight just lines 1 and 5 on their own
```php{1-3,5} - Highlight 1 through 3 and then 5 on its own
```php{5,7,2-3} - The order of lines don't matter
However, specifying 3-2 will not work.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

### Security

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Postcardware
------------

[](#postcardware)

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, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Sebastian De Deyne](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance54

Moderate activity, may be stable

Popularity51

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~98 days

Recently: every ~195 days

Total

11

Last Release

1748d ago

Major Versions

0.1.0 → 1.0.02018-11-30

1.1.0 → 2.0.02019-07-05

2.1.1 → 3.0.02021-08-04

PHP version history (4 changes)0.1.0PHP ^7.1

1.0.3PHP ^7.2

2.1.1PHP ^7.2|^8.0

3.0.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (31 commits)")[![owenvoke](https://avatars.githubusercontent.com/u/1899334?v=4)](https://github.com/owenvoke "owenvoke (11 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (9 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (8 commits)")[![alexanderglueck](https://avatars.githubusercontent.com/u/6459474?v=4)](https://github.com/alexanderglueck "alexanderglueck (4 commits)")[![allejo](https://avatars.githubusercontent.com/u/1246453?v=4)](https://github.com/allejo "allejo (4 commits)")[![imliam](https://avatars.githubusercontent.com/u/4326337?v=4)](https://github.com/imliam "imliam (3 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (2 commits)")[![jonnybarnes](https://avatars.githubusercontent.com/u/108303?v=4)](https://github.com/jonnybarnes "jonnybarnes (1 commits)")[![jdlien](https://avatars.githubusercontent.com/u/13335535?v=4)](https://github.com/jdlien "jdlien (1 commits)")[![boast](https://avatars.githubusercontent.com/u/51127?v=4)](https://github.com/boast "boast (1 commits)")

---

Tags

commonmarkhighlightspatiecommonmark-highlighter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-commonmark-highlighter/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-commonmark-highlighter/health.svg)](https://phpackages.com/packages/spatie-commonmark-highlighter)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[spatie/laravel-collection-macros

A set of useful Laravel collection macros

1.9k5.7M29](/packages/spatie-laravel-collection-macros)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
