Highlighting Code Blocks

By Cory LaViska on November 20, 2026

Code blocks in Surreal CMS are plain HTML. If you want syntax highlighting, you need a script. Prism has been the go-to for years, but there's a newer option that's a better fit for a CMS.

The editor writes code blocks in this format. The language-* class tells a highlighter which language the code is in.

<pre class="cms-code"><code class="language-javascript">
  ...
</code></pre>

Both of the options below read that class, so you don't have to change anything in your content.


MicroLighter

MicroLighter is a tiny syntax highlighter by Dave Rupert. He wrote about why he built it in Introducing Microlighter.

Most highlighters wrap every token in a <span>. MicroLighter uses the CSS Custom Highlight API instead, so it colors the code without changing your markup. That matters in a CMS, because there are no extra tags that could end up in your published HTML. It's about 2 KB, has no dependencies, and loads the grammar for each of its 37 languages only when a page needs it. It's what we use on this website.

Add a theme inside the <head> section of your page.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/microlighter@2/dist/themes/github.css">

Then tell the page which theme to use with a data-syntax-theme attribute on the <body>.

<body data-syntax-theme="github">

There are 11 themes to choose from, including dracula, night-owl, and solarized-light. Themes are a handful of CSS custom properties, so it's easy to make your own that matches your website.

Finally, add this script just before the closing </body> tag.

<script type="module">
  import { highlightAll } from 'https://cdn.jsdelivr.net/npm/microlighter@2/dist/index.js';

  if (!window.isCMS) {
    highlightAll();
  }
</script>

The window.isCMS check is one way to detect the CMS. Since MicroLighter doesn't change your markup, running it in the editor wouldn't affect what gets published, but the colors won't keep up while someone edits a code block. It's simpler to highlight on your live website only.

The CSS Custom Highlight API works in current versions of Chrome, Edge, Firefox, and Safari. Older browsers show the code without colors.


Prism

Prism is a lightweight, extensible syntax highlighter used by thousands of websites. It supports more languages than MicroLighter and has a lot of plugins.

Add a theme inside the <head> section of your page. If you want a different one, replace the href with any other theme.

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

Add these scripts just before the closing </body> tag.

<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/prism.min.js" data-manual></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/components/prism-markup.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/components/prism-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.30.0/components/prism-javascript.min.js"></script>
<script>
  if (!window.isCMS) {
    Prism.highlightAll();
  }
</script>

The first script is Prism itself. The next three add HTML, CSS, and JavaScript. Remove the ones you don't need, or add other languages. Each one is another request, so only include the languages you use.

Prism highlights code by wrapping each token in a <span>. If it ran in the editor, those tags would be published along with your content. The data-manual attribute stops Prism from running on its own, and the last script runs it on your live website only.


Which one should you use?

Start with MicroLighter. It's smaller, it's easier to set up, and it never touches your markup. Use Prism if you need a language MicroLighter doesn't support or one of Prism's plugins.

MicroLighter also has a <micro-lighter> element that adds line numbers and a copy button. Don't use it for code blocks in content regions. Stick with the imperative highlightAll() method instead.


If you're having trouble getting either one to work, feel free to contact us for help.

So simple, it’s Surreal

Free for personal, educational, and non-profit websites.

By signing up, you agree to the terms of service.