adding code highlighting to this site

I wanted to have code highlighting in an article like this. So I browsed the internet and found highlight.js that seemed to the thing. Easy enough to add. Any markup with the pre and code combo would get highlighting. In markdown this translates to blocks surrounded with ~~~. I wanted to add the js library only for posts that got code so I invented a new item key called features and if code is in the list the library was added in the head. I added this to the default layout in my nanoc setup:

- if (@item[:features] or []).include? 'code'
  == render '/highlight.*'

(I use slim for layouting and other markup stuff.) There may be no features and thus the need for “or []”. All in all pretty neat!

written by fredrik at 2023-05-01

More content on nanoc