November 29, 2026
Over-engineering code blocks
While I’m a fan of Hugo, the static site generator used for this website, I never liked the built-in code blocks. The syntax highlighting was poor, styling them appropriately was painful, and features like line numbers or copy buttons seemed to require more toil than should be necessary.
For a while, I lived with it. But it grated on me over time, and yak shaving is truly the most fun activity for an engineer. So, I present my shiny custom code blocks:
fn foo () -> Result < Bar , ()> { Err (()) } These are generated with a custom tool called arbgen. This leverages the fantastic Arborium, which does much of the heavy lifting. Arborium uses tree-sitter for parsing and syntax highlighting code blocks.
How does it work?
At its core, arbgen is an ahead-of-time generator. It takes a .snippet file, which contains the code block with some frontmatter, and outputs a corresponding .html file that can be included in any Hugo template or Markdown document.
For example, the code block above lives as example-codeblock.snippet:
+++lang = "rust"+++fn foo() -> Result<Bar, ()> { Err(())}arbgen will see this file and transform it into an example-codeblock.gen.html file, which can be included with a tiny Hugo shortcode:
{{ $file := .Get 0 }}{{ (printf "%s%s" .Page.File.Dir $file) | readFile | safeHTML }}The generator also outputs a single stylesheet for the syntax highlighting, which is bundled into the website.
✨ Features
As arbgen is entirely custom, I can add whatever features I like. At the time of writing, it supports:
- language-specific icons in the code block header
- line numbers (always on)
- a snazzy copy button in the top right
- a watch mode for a local dev workflow
Was it worth it?
Ultimately, this was an excuse to try some new libraries and build something fun. I like it, and that’s enough for me.
The source is available if you’d like to take a look, fork, or do whatever you please.
