For 2, syntect directly added support for highlighting so it should be straightforward.
The main blocker is how to let a user turn on those options.
See https://gohugo.io/content-management/syntax-highlighting/ for how Hugo does it but they use a shortcode instead of the classic Markdown codeblock.
This is a possibility as I don’t have a good idea on how to implement that with triple backticks since a syntax name is optional.
The typical approach I’ve seen is to add it to the opening code fence, treating it as comma-separated. This is how options are done in rustdoc (e.g. ```rust,ignore,compile_fail,no_run,should_panic, to use a bunch that don’t actually fit well together!).
Something like this could do it:
```rust, linenos, hl_lines=3-8 10-20
But I must remark with this that I consider line highlighting to be insufficient: I require character highlighting. My FizzBuzz article is an example; I achieved that in my current Hyde-powered site by extending its syntax highlighting with an extra step that roughly turns « into <mark> and » into </mark>. (It doesn’t work perfectly with all languages, as the « may mess up the syntax highlighter, it still being there during syntax highlighting—but it worked for Rust, and that’s all I needed at the time. For best results, this would need to be integrated into the highlighter more perfectly.)
I’m slowly working on migrating to Zola, and I expect I’ll indefinitely maintain a fork for this and other reasons. (Another sample reason: inline syntax highlighting, which pure shortcodes are insufficient for.)
It’s moderately irrelevant, but I also find myself today wanting to combine languages, like diff+javascript to highlight JavaScript in a diff. I wonder whether I’ll need to write a syntax file with it, or if there’s some other way to handle it…
That was my idea as well but markdown doesn’t require a language to be set. We could special case some keywords like linenos, hl_lines etc to avoid that issue though.
That approach definitely needs to be part of the syntax highlighter, to use the theme highlight colour. Granted you could also change the mark css manually. I had never though of character level highlighting though, that is interesting.
Sublime Text doesn’t do that so it is unlikely to happen automatically
Not really, you basically need to first come up with a nice HTML structure that works and then overwrites what syntect is producing to fit in it.
It would be helpful to see how some nice JS libraries are doing it and essentially try to copy the structure, ditching the basic syntect output.
For line numbers, what about CSS counters like this post: https://www.sylvaindurand.org/using-css-to-add-line-numbering/
Doing it this way could even let you start the line numbers for a snippet on a particular number (maybe linenos=27?), which I imagine could be useful if you had code blocks that were snippets from a larger codeblock.
To implement it, would only require a tag wrapped around every line and a counter-reset style (with the initial line number) on the pre tag.
Zola themes would need to add some CSS to make this work, and a name would need to be chosen for what the line number counter should be called.
I would prefer something that doesn’t require CSS if possible.
It could be a table like Syntax Highlighting | Hugo which would make highlighting easier as well I think? It would still require some CSS though
I think I prefer the Hugo approach (although I need to check how others JS highlighters or even Github handle it) since selecting line numbers is reaaaaaally annoying when trying to make copy/pastable snippets.