Syntax highlighting code on the web happens like coloring any other text on the web happens. You wrap the bits of text you want colored uniquely in some element, probably a meaningless <span>
, then select that in CSS and apply a color. Ideally, the span-wrapping happens server-side so client-side JavaScript isn’t tied up doing something so rote, but many of the libraries that help with it are client-side.
What if… you didn’t need to do span-wrapping at all?! That’s what Heikki Lotvonen has cooked up. The colorization happens at the font level, that is, via “color fonts”, the OpenType feature, and “contextual alternates”. This is loaded with advantages. No client-side JavaScript needed, no extra DOM weight, and no exotic processing of any kind. It works just about like using any other custom font. It’s almost certainly faster than whatever you’re doing now!
Adam made a demo that showcases how CSS can override the colors used, making theming and color modes doable.
Love seeing it make it’s way into other demos.
https://codepen.io/daviddarnes/pen/poXpaLB?editors=1100