A few weeks ago, TJ Miller introduced me to highlight.php by writing a Parsedown extension for it. Highlight.php does the exact same thing as highlight.js: they both add tags and classes to your
code blocks, which enables them to be highlighted with CSS. The difference is, highlight.php does this on the server.
There are two benefits of highlighting code blocks on the server:
- The highlight.js script weighs about 19KB min+gzip. This number fluctuates if you want to support more or less languages. Either way, it's significant enough to affect your website's load time.
Whenever I need to convert markdown to HTML in PHP, I pull in league/commonmark. It parses markdown to an AST before rendering it to HTML which makes it easy to extend, and I did exactly that.
I created a spatie/commonmark-highlighter package that supports higlighting with CommonMark. After you register two custom renderers, all code blocks will receive a set of tags and classes, so they're already prepped to be highlighted by CSS when your content arrives in the browser.
<!DOCTYPE html> <title>I'm highlighted!</title>
View the above snippet's source in your browser, and you'll see the highlighting has already been done!