Markdown: Language-identifying attribute for code blocks?

I’m struggling to find precedents besides RedCarpet’s usage in Jekyll, but several tools I’ve used have produced an attribute that identifies the language of the code block being rendered (i.e., data-lang="<LANGUAGE>", class="language-<LANGUAGE>"). This can offer some neat styling tweaks with CSS! Here’s an example of how that might look:

From this:

```rust
fn main() {
    println!("Hello, world!");
}
```

…to this:

<pre>
  <code data-lang="rust"> <!-- >
fn main() {
    println!("Hello, world!");
}
  </code>
</pre>
1 Like

Is it only for the cases where syntax highlighting is disabled?

In Syntect, you don’t actually have a <code> tag, only <pre>.
The way to implement it would be to do a basic replace on the result of https://github.com/getzola/zola/blob/master/components/rendering/src/markdown.rs#L189 (which is returning https://github.com/trishume/syntect/blob/master/src/html.rs#L308-L314)

If it is only for codeblocks without syntax highlighting, then it’s even easier, just a string to modify.

I’d prefer to have it be available in all cases – I specifically was hoping for language-specific decorations around rendered code blocks – but I understand that I’m not necessarily representative of others’ use cases. :slight_smile:

I just created a branch with that feature:

Unless there are huge objections I would open a PR for it.

PR created: https://github.com/getzola/zola/pull/1204

1 Like