Ghost Theme Customization Part 2: Adding Syntax Highlighting to Code Blocks with Prism.js

How to add code syntax highlighting to a Ghost blog theme with Prism.js — a step-by-step guide from using CDN links to downloading the files and customizing the JetBrains Mono font.

In the last post I picked Ruby from among the Ghost themes and made some simple changes to the Ruby theme. As you run a blog, you start wanting to show code nicely. Ghost's default code blocks in particular are plain, which left a lot to be desired. So this time I decided to use Prism.js to apply syntax highlighting to code.

Why Prism.js?

There are several syntax highlighting libraries, but here's why I chose Prism.js:

  • Lightweight: you can pick only the languages you need, so the size burden is small
  • Rich language support: you can use languages that Tistory or other platforms don't support
  • Flexible customization: styles can be freely modified with CSS
  • Plenty of plugins: add useful features like line numbers and a copy button

The simplest method is to add CDN links in the Ghost admin page.

Ghost Admin > Settings > Code injection:

  1. Add to Site Header
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" integrity="sha512-vswe+cgvic/XBoF1OcM/TeJ2FW0OofqAVdCZiEYkd6dwGXthvkSFWOoGGJgS2CW70VK5dQM5Oh+7ne47s74VTg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  1. Add to Site Footer
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js" integrity="sha512-9khQRAUBYEJDCDVP2yw3LRUQvjJ0Pjx0EShmaQjcHa6AXiOv6qHQu9lCAIR8O+/D8FtaCoJ2c0Tf9Xo7hYH01Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js" integrity="sha512-SkmBfuA2hqjzEVpmnMt/LINrjop3GKWqsuLSSB3e7iBmYK7JuWw4ldmmxwD9mdm2IRTTi0OxSAfEGvgEi0i2Kw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

That applies it right away! But I wanted to fine-tune the font and styles, so I decided to download the files and customize them myself.

Method 2: Download the Files and Customize

Step 1: Download Prism.js

On the Prism.js download page, select the languages and plugins you need.

💡 Download the Development version. It's unminified, so it's easier to modify.

Step 2: Prepare the Dev Environment

In the terminal, go to the theme folder and start the dev server:

cd ~/ghost-dev/content/themes/ruby
yarn dev

Step 3: Apply the CSS File

Put the downloaded prism.css file in the assets/css/misc/ folder.
Then open assets/css/screen.css and add this at the very end:

@import "misc/prism.css";

When you save, yarn dev automatically merges the CSS files into one.

Step 4: Apply the JavaScript File

Paste the contents of prism.js into assets/js/main.js.

💡 Automatic merging via the Gulp configuration is also possible, but the existing main.js was simple, so I added it manually.

Step 5: Customize the Font

Let's apply the JetBrains Mono font to code blocks.

1) Import the font

Add to assets/css/general/fonts.css:

@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&display=swap');

2) Apply the font

In assets/css/misc/prism.css, change the following:

font-family: "JetBrains Mono", Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
font-size: 0.9em;

Step 6: Build and Apply the Theme

When you're done, stop the dev server and package the theme:

# Stop the dev server
Ctrl + C

# Package the theme
yarn zip

Upload the zip file created in the dist folder in the Ghost admin page and you're done!

Checking the result

Code blocks now look much better. Syntax is highlighted in different colors per language, and readability is improved with the JetBrains Mono font.

Key Advantages of Prism.js

  1. Lightweight: optimize size by choosing only the languages you need
  2. Class-based: set a default language with a body class
  3. Broad language support: use languages other platforms don't support
  4. Extensibility: add new languages or modify existing ones with regular expressions
  5. Rich plugins: line numbers, a copy button, highlighting and more

Wrapping Up

That's the process of applying syntax highlighting to a Ghost theme. The CDN approach is simple, but downloading the files and customizing them yourself allows finer adjustments.

In the next part, I'll cover how to use Prism.js plugins to add features like line numbers and a copy button!