Hey there,
just recently I hacked together a plugin that highlights code snippets server-side.
But … why?
Server-side - that means no external dependencies (especially those written in javascript like prism.js
and highlight.js
). I love them both, but they’re not for everyone. People disabling javascript execution (and for good reasons) should still be able to enjoy some decently highlighted code IMHO.
Why not? When I found out about Geert Bergman’s highlight.php
library (a PHP port of highlight.js), I decided to go for it:
- Highlighting code for everyone - no javascript needed
- Extensive: currently 141 supported languages
- Customisable: 65 different styles included (more coming soon)
Getting started
Use one of the following methods to install & use kirby-highlight
:
Git submodule
If you know your way around Git, you can download this plugin as a submodule:
git submodule add https://github.com/S1SYPHOS/kirby-highlight.git site/plugins/kirby-highlight
Clone or download
Activate the plugin
Activate the plugin with the following line in your config.php
:
c::set('plugin.kirby-highlight', true);
Now proper classes are added to your code snippets, making them ‘themeable’. If you want to activate kirby-highlight
only on specific domains, read about multi-environment setups.
Configuration
Change kirby-highlight
options to suit your needs:
Option | Type | Default | Description |
---|---|---|---|
plugin.kirby-highlight.languages |
Array | ['html', 'php'] |
Defines languages to be auto-detected (currently 141 languages are supported). |
Styling
To apply one of the many available stylesheets, just include it in your <head>
element using the css()
helper:
<?php echo css('assets/plugins/kirby-highlight/css/zenburn.css') ?>
Note: Most of the included themes depend some way or another on the class .hljs
to be added to the code’s container!
In order to make sure the theme gets applied as planned, any of these methods will work:
- Simply use
kirby-pep
to put it there:c::set('plugin.kirby-pep.code_class', 'language-%s hljs');
- Replace
.hljs
with[class^="language-"]
in the included stylesheet - Copy it to your
assets/css
directory and modify it - Include the styles in your own workflow
Troubleshooting
Adding an unsupported language breaks kirbytext()
parsing of the code snippet in question. Always be sure to only include valid languages.
You can find the plugin on Github. It’s not much, but it’s something. I’d love to get some feedback on this, and since this is my second (public) plugin, try to be nice