How to annotate code block as rust in markdown output of `rustc --explain`?

The output of rustc --explain is in markdown and wraps code blocks in backticks:

```
\\ example rust code
```

Is it possible to annotate the language of the code block as rust?

```rust
\\ example rust code
```

The reason is to make it possible to pipe the output to a markdown renderer such as glow which has language-specific syntax highlighting, e.g., rustc --explain Exxxx | glow.

1 Like

This works for me:

$ rustc --explain Exxxx | sed '/^```/{s//&rust/;:a;n;//!ba}' | glow

from:

2 Likes

And it looks like there's not a built-in way. If I'm reading that right. (It strips any annotations from the markdown, which they do have (albeit not rust).)

1 Like

Well, making this unconditionally add rust would be dead simple.

text.push_str(&line[..(indent_level + 3)]);
if is_in_code_block {
    text.push_str("rust");
}

Not sure if this stuff is intended to be stable. It would probably be more useful to add this to glow instead.

Sure. (But I don't think that's what the OP meant.)

Using glow was just an example. I'm viewing the output of rustc --explain using a plugin inside neovim and want the markdown syntax to apply there as well. See spaces added to popup message breaks markdown formatting · Issue #2 · yining/vim-rustcexplain · GitHub.

1 Like

Thanks. It would be nicer if it was built in, but I've used this as a workaround.

1 Like