I’ve created a Hugo theme and have written a Markdown render hook for codeblocks. I’d like to have line highlighting (e.g. {hl_lines="3 6"}) even for languages that are not supported by Chroma.
In my render hook I’m using transform.HighlightCodeBlock like so:
You’re right, there’s currently no way to do this, but I agree that it would be useful.
The simplest way would probably to allow overriding the type in the opts passed in. If you could create a GitHub issue for it, I’m sure someone will pick it up.
What you could try is to set the option guessSyntax which will give you highlighting even for non-supported types; the current “guessing” is pretty basic (I think you get a text lexer for all), but you should at least get line highlighting.
The .Options map passed into the code block render hook is empty when transform.CanHighlight is false. However, in this case, the highlight options are available in the .Attributes map.
So we can cheat.
If you’re OK using the default wrapper, set the default language to “text” (or whatever), and use the transform.Highlight function.
The default wrapper generated by transform.Highlight (as shown above) is a simpler approach if you plan to mix line number options (none, inline, or table) throughout the site.
This is exactly what I’ve done. One thing I noticed though was that the casing in .Options and .Attributes differ: especially for the lineNos parameter - which is lineNos in .Options (upper-case N) but linenos in .Attributes (lower-case N).
To check for this parameter I ended up with this:
index $originalOptions "lineNos" | default (index $originalOptions "linenos")
Unsure whether this should be considered a bug but it makes things harder than the should be (IMHO).