Rust Comment Syntax Highlighting

I’m having a bit of an issue with syntax highlighting with Rust.

I’m trying to highlight something like the following snippet:

{{< highlight rust >}}
println!("{:?}", foo);
// [0, 4, 16, 36, 64]
{{< / highlight >}}

This is being rendered like this:
27%20AM
(Note that the comments should be grey, but are not highlighted as comments)

If I add another commented line, then the first commented line is highlighted correctly as grey, but the second line is still improperly highlighted (I’d add another image, but discourse only allows me to post one)

I’ve tried this with the Chroma CLI, and it highlights this snippet fine. I’ve also tried using the “```” code fences instead of the highlight shortcode and have the same error.

Any ideas, or does this seem like a bug?

Thanks!

This is a recurring issue with Chroma; I’d suggest that you open an issue on that repo.

The fix is to basically make Chroma insert a newline after each code block for its internal parsing. I have no idea why that is not the default (like it is in Pygments).

The dev fixes this on per lexer basis by simply setting EnsureNL: true for that lexer.

See the issue that this PR fixes: https://github.com/alecthomas/chroma/pull/56.

2 Likes