Usage with Hugo

You could switch completely to Highlight.js. To get the best of both worlds

here’s a minimal setup how to pass everything Hugo/Chroma cannot highlight to HighlightJS.

Identifying Chroma output

When Hugo/Chroma renders a fenced code block it will be wrapped in <pre><code> tags like this1. That’s the standard, if you configure it differently (eg custom wrappers) you have to adjust.

Configure Highlight JS

In it’s default configuration HighlightJS will pick up <pre><code> and highlight it’s content. Good for the latter two but the first would be double highlighted with strange effects.

HighlightJS provides options to tweak things like that2. Using teh standard nohighlight class to the code tag would need much more setup.

The key here is the chroma-class on the pre tag which is only added when Chroma has done highlighting.

We just use `hljs.configure to change the selector and exclude the elements highlighted by Chroma.

Play this at the end of your body:

<body>
...
  <script src="js/highlight-hugo.min.js"></script>
  <script>
     hljs.configure({ cssSelector: "pre:not(.chroma) code" });
     hljs.highlightAll();
  </script>
</body>

CSS

CSS styles can be used as usual. Simple Example for a keyword

The exact way depends on how you style these things, especially dark/light, prefers or complete color themes.


  1. There are some more variants but the general layout is like that. ↩︎

  2. Unfortunately there’s no common standard for this all over the highlighters. All Highlanders. ↩︎