Correct highlighting for compiler output

I wonder if anyone has brought up that hundreds of examples posted here use wrong highlighting.

While code gets formatted properly in this forum:

fn main() {
    panic!("Let's try to highlight this.")
}

(Playground)

The error output doesn't get formatted properly:

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.84s
     Running `target/debug/playground`
thread 'main' panicked at 'Let's try to highlight this.', src/main.rs:2:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Here, 0.1 appears as cyan (while the v0. part is black). target is red, 0.84 cyan again. Also Let's gets purple, and try becomes bold.

I wonder if it's difficult to fix this. I could use a text marker after the three backticks, but then error messages become less readible. Compare:

fn foo() -> &i32 {
    &1
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0106]: missing lifetime specifier
 --> src/lib.rs:1:13
  |
1 | fn foo() -> &i32 {
  |             ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
  |
1 | fn foo() -> &'static i32 {
  |              +++++++

For more information about this error, try `rustc --explain E0106`.
error: could not compile `playground` due to previous error

Here, some of the highlighting is useful, such as the 'static or the bold fn. But certainly making 's bold is a mistake, same as making return type bold, or contains red.

Here is how it looks like with the text tag:

   Compiling playground v0.0.1 (/playground)
error[E0106]: missing lifetime specifier
 --> src/lib.rs:1:13
  |
1 | fn foo() -> &i32 {
  |             ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
  |
1 | fn foo() -> &'static i32 {
  |              +++++++

For more information about this error, try `rustc --explain E0106`.
error: could not compile `playground` due to previous error

Less readible (which is why I guess that tag isn't inserted by default when being on Playground and opening an URLO post).

1 Like

I guess you'd first have to create a "Rust error message language" for highlight.js which is used by Discourse for syntax highlighting, and then you'd add that to the URLO Discourse instance. Or maybe you could make a modified version of the Rust language definition that tries to detect if it's a regular Rust code block or a compiler error. Seems doable, assuming it's possible to install custom highlight.js language definitions on Discourse.

edit: I found that someone has apparently done this for Jai: GitHub - Jai-Community/discourse-highlightjs-jai: Discourse theme component to highlight Jai syntax