Ghost currently doesn’t ship with Code Highlighting, you can render code but to get it to highlight correctly depending on the language, that’s something you need to do yourself.
There’s numerous code highlighters you could use for this, but the one I’ve always gravitated towards, and the one that seems to be recommended by a lot of folks is Prism.
Prism makes it really easy to properly highlight your code blocks, depending on the language. When I launched this site, I didn’t want to have to dig around and edit bits of code to get proper highlighting, thankfully though I can utilise the Ghost Code Injection feature.
Code Injection allows you to, well, inject bits of code into the header and footer of the website, so by injecting Prism into the header and footer, I can get instant code highlighting without doing anything else. In my head, I have:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/themes/prism-tomorrow.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/plugins/line-numbers/prism-line-numbers.min.css" />
content for Prism
The first stylesheet is the theme I’m using. I played around with a bunch of different ones but I liked the look of the Tomorrow theme the most. The second stylesheet is a plugin for Prism which generates snippets with page numbers. I wanted code snippets on this website to resemble what you’d see in an IDE a bit closer, so line numbers were a must!
In the footer is where I inject all of the Prism scripts to get the thing to actually work:
<script>
window.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre[class*=language-]').forEach(function (node) {
node.classList.add('line-numbers');
});
Prism.highlightAll();
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-markup-templating.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-php.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-js.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-json.min.js"></script>
