Skip to main content

Getting rid of JavaScript and giving back to the PHP community

The League of Extraordinary Packages recently released version 2.0 of their markdown parser package league/commonmark. Trying to keep my dependencies up to date, I used this opportunity to get rid of JavaScript by switching from highlight.js to highlight.php.

A bit of backstory

I had already played with the thought of removing JavaScript from my website, because the only thing I used it for was syntax highlighting via highlight.js. However, the thought of having no syntax highlighting was what stopped me from actually removing it.

Fortunately for me, Freek Van der Herten had published a blog detailing how they at Spatie deal with perfectly highlighting code snippets. That's where I learned about highlight.php.

highlight.php is a PHP implementation of highlight.js. By using highlight.php it is possible to generate code snippets with the highlight.js classes already applied when the code hits the browser, without having to rely on highlight.js / JavaScript to do the conversion.

Scratching my own itch

In Freek's post I learned that Spatie already had a package that extends league/commonmark with a highlight.php renderer.

However, after attempting to install their commonmark-highlighter package, I noticed that it was not yet compatible with league/commonmark 2.0.

So, I decided to do the upgrade myself. A few minutes later I had a fork checked out and the excellent upgrade guide open in my browser.

The upgrade itself was pretty straightforward, definitely not as hard as I expected. I managed to complete the whole upgrade in less than one hour.
With the forked package compatible, and the highlight.js markup being generated on the server side, I removed the JavaScript file from my site and decided to give back to the community.

Giving back to the community

I opened a pull request and a day later my changes got merged. To my surprise, I even got a nice shout out on Twitter.

The tweet mentioned, that Spatie's second syntax highlight package - commonmark-shiki-highlighter - would also benefit from an upgrade.
Having already figured out the upgrade path, I went to work and opened a second pull-request a day later, which also quickly got accepted.
Now everyone benefits from the upgraded packages.


My website now runs without JavaScript, syntax highlighting is handled server side, and two packages are now compatible with league/commonmark 2.0.

Giving back feels good!

Published on August 7, 2021
Last modified on August 7, 2021

Did you like what you read? Feel free to share! Make sure to follow me on Twitter to stay in the loop.