Adding a custom lexer to Chroma

I recently switched from using pygments to chroma for syntax highlighting. My site compiles a lot faster, which is great, but chroma doesn’t have all of the lexers I need. I work with a couple of niche languages and have to write my own lexers.

Converting my pygments lexer to chroma wasn’t too difficult, but now I’m hitting a wall with getting Hugo to use it. As I understand it, Hugo vendors chroma and chroma doesn’t have extension points. It seems to me that if I want to use my custom lexers, I’ll have to do the following:

  1. Remove the prebuilt hugo binary
  2. go get hugo to grab the source code
  3. create a vendor folder with the chroma source code
  4. manually add my lexer file to chroma
  5. go install hugo, building all of the packages too.

It feels like I’m doing this the wrong way- especially since I’d have to repeat the process of I ever want to tweak my lexer or update Hugo! Is this what I’ll need to do, or is there a better way? Thank you!

Hi there, :wave:

Another option is to contribute your new or adjusted lexers to Chroma. Alec, the maintainer of Chroma, is very open to pull request. I’ve added a couple of lexers and fixes this way.

It does take a bit longer for you can use the lexers in Hugo, since Chroma first needs a new release and then we’ll require a new Hugo release to include the new Chroma release.

1 Like