Skip to content
Bright Themes

How to add syntax highlighting in Ghost

Color the code blocks on your Ghost site with Prism.js, added through code injection in minutes or built into your theme.

Norbert1 min read

Ghost’s editor highlights code while you write, but the published post shows code blocks in one color. To color them on your site, add a highlighting library. This tutorial uses Prism.js (opens in a new tab), which is small, well documented and has plugins for line numbers, a copy button and more. highlight.js (opens in a new tab) works in much the same way.

Tip

Some themes already include Prism. Several of ours load it on posts with the internal tag #syntax-highlight; see for example the Maali docs.

What you need

  • Access to Settings › Advanced › Code injection for the quick setup.
  • Or a copy of your theme you can edit, to build it in.

1. Set the language on your code blocks

Prism colors a code block when it knows its language. Ghost marks the language for it in two ways:

  • In a code card (more in how editor cards work in Ghost), type the language in the card’s language field, for example css.
  • In a Markdown card, add the language after the opening backticks: ```css.

Either way, Ghost outputs <pre><code class="language-css">, which is the class Prism looks for. Blocks without a language stay plain.

2. Add Prism with code injection

The quickest way needs no theme edit. In Settings › Advanced › Code injection (see how code injection works in Ghost), add the stylesheet to the Site header:

HTML
<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/themes/prism.min.css"
/>

And the script to the Site footer:

HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/plugins/autoloader/prism-autoloader.min.js"></script>

The core file highlights HTML, CSS, JavaScript and C-like languages. The autoloader plugin loads any other language the first time a page needs it, so language-python or language-sass just works, without a script per language.

If you’d rather list the languages yourself, leave out the autoloader and add one script per language instead, for example:

HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/components/prism-sass.min.js"></script>

For another color scheme, replace prism.min.css with one of Prism’s other themes, such as prism-tomorrow.min.css for a dark one.

3. Or build Prism into your theme

Code injection loads Prism on every page from a CDN. To serve it from your own site, with only the languages and plugins you need:

  1. On Prism’s download page (opens in a new tab), pick a theme, your languages and plugins, and download prism.js and prism.css.
  2. Copy them to assets/js/ and assets/css/ in your theme.
  3. In default.hbs (the base template; see Ghost theme structure, explained), add the stylesheet inside <head>:
Handlebars
<link rel="stylesheet" href="{{asset "css/prism.css"}}" />
  1. Add the script before </body>:
Handlebars
<script src="{{asset "js/prism.js"}}"></script>
  1. Zip the theme and upload it in Settings › Site › Theme › Change theme › Upload theme.

The {{asset}} helper builds the file’s URL and adds a version to it, so browsers fetch the new file after a theme update.

To load Prism only on the posts that need it, as our themes do, wrap each of the two lines in a check for an internal tag. The stylesheet, in <head>:

Handlebars
{{#is "post"}}
  {{#post}}
    {{#has tag="#syntax-highlight"}}
      <link rel="stylesheet" href="{{asset "css/prism.css"}}" />
    {{/has}}
  {{/post}}
{{/is}}

And the script, before </body>:

Handlebars
{{#is "post"}}
  {{#post}}
    {{#has tag="#syntax-highlight"}}
      <script src="{{asset "js/prism.js"}}"></script>
    {{/has}}
  {{/post}}
{{/is}}

Then add the internal tag #syntax-highlight to each post with code, in the post’s Tags field. Internal tags start with # and don’t show on the site.

The result

Your code blocks are colored by language, on every post or only the ones you tag. A block like this one:

CSS
.blog-header {
  padding: 1em;
  background-color: #eee;
}

shows its selectors, properties and values in different colors, in the Prism theme you chose.

Related posts

More on design

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.