Lee Kelleher

Code Syntax Highlighting in Wyam

Posted on . Estimated read time: 2 minutes (283 words)

I'd been going back and forth on how I should handle the syntax highlighting of my blog's code snippets. A popular choice would be to use a JavaScript library, (such as Prettify or Prism). I have nothing against those libraries, but in the pursuit of making my website fast and lightweight, I wanted a non-JavaScript option. My idea was to use a .NET library to convert my source-code snippets into colourised markup.

The only library that I knew of (in .NET land) was ColorCode. CodePlex use for their source-code highlighting, so it should be stable, but it hasn't been updated in a few years, so fingers crossed!

My next step was to figure out how to make this happen in Wyam. Now, Wyam is a very versatile generator, there are many ways this problem could be tackled... it should be a simple search & replace, right? Alas, after a couple of hours hacking around, I wasn't getting anywhere, I was stumpted!

Heading over to the Wyam chat room on Gitter, I wanted to see if anyone had any ideas for this. There was a suggestion of treating my code snippets as assets, cross-referencing and pulling them in at build-time. But that would require making structural changes to my blog, which I'd rather not at this juncture.

Then Dave Glick had an idea... he claims to have been Nerd Snippered ... and he was away, updating code, released a new version of Wyam, and wrote a blog post. I was presented with a solution! Thanks again Dave!

If you want to know more about it, read about all the ways you can do Syntax Highlighting in Wyam.