Adding Code Syntax Highlighting to a Ghost Blog with Prism.js
Ghost is an awesome and user-friendly Open-Source platform for blogging. But by default, it does not support syntax highlighting, which may not be very practical if you happen to add code snippets to your posts.
Thankfully, Ghost is very easy to customize in different ways, and one way we are going to explore here is via Code Injection, as in use throughout my own blog.
Ghost’s Code Injection feature allows to add custom CSS styles and JS code to the header and footer of all or individual articles.
There are several code syntax highlighting libraries out there, such as:
They essentially almost have the same features. So no matter what you chose, the steps depicted below will be pretty much the same.
Prism.js Code Injection
Go to Settings > Code Injection from the Admin menu.
Then add the CSS in the Blog Header section, and the JS files (core + all languages you wish to use) in the Blog Footer section.
Here you have to first include the core library (prism.min.js), and all JS files for all individual languages you wish to have highlighting support for.
You can link to a CDN like https://cdnjs.com/libraries/prism to select exactly the languages libraries you wish to have included.
Rather than manually include each individual language library, if you are able to auto-host and serve your own files, you can also head to the Prism.js download page, where you can pick whatever language and download tailored JS and CSS files, which you can then include as above.
At this point, Prism.js will be automatically activated as soon as a valid Markdown code syntax is encountered with the language specified.
Remember to use
language** before the code snippet and ** after to have your code block highlighted.
Example with a Dart code snippet which has to be written like this:
This is highlighted as follows:
Thanks for reading.
As always, your feedback and comments are more than welcome!