Adding support for `julia-repl` syntax highlighting

Is it possible to add custom syntax highlighting to our Discourse instance? In the julia zulip chat (please come check it out! https://julialang.zulipchat.com/), we have an additional highlighting option called julia-repl which is designed for displaying code copy-pasted from the REPL to help visually distinguish between inputs and outputs:

Would it be feasible to have this in Discourse as well? cc @mbauman

9 Likes

We do have support turned on for julia-repl, just use that as the “type” of a code block like:

```julia-repl
```

I don’t know if its rules are meaningfully different, though… quick test:

julia:

julia> @benchmark foo(1, "bar", 'z', :foo)::Nothing
BenchmarkTools.Trial: 10000 samples with 999 evaluations.
# ...

julia-repl:

julia> @benchmark foo(1, "bar", 'z', :foo)::Nothing
BenchmarkTools.Trial: 10000 samples with 999 evaluations.
# ...
4 Likes

Oh I thought from my experiments with julia-repl here that it just didn’t do any syntax highlighting like if I did

```agbasasdasdas

but I see now that there is actually some stuff going on, sorry for the noise then! I should have checked more carefully

Yeah, it looks like it’s working well. I think it’s the themeing that could probably be improved — for example, julia> is tagged as a “prompt_” class but it’s not getting any special formatting.

5 Likes

I just found the documentation using this tag, but to no effect:

Neither Github, nor our documentation highlights anything.
And highlight.js does seem to alias julia-repl to julia, instead of considering it as its own syntax.

Nobody has implemented syntax highlighting for the Pkg REPL mode, so this is not surprising.

It is implemented by nesting regular Julia syntax within the julia> prompt. Other than that, Julia syntax in the REPL is no different from Julia syntax in files or wherever else, so I am not sure what you are missing here.

3 Likes

Maybe someone who has admin access to this forum can add a little bit of custom CSS.

span.hljs-meta.prompt_ {
  color: #17f117;
}

3 Likes

Awesome, thanks! My above post is now much more snazzy! How’s the contrast of #17f117 on light mode?

Edit: to answer my own question, it looks a little bright… suggestions on another color?

image

I just realized there was a theme switcher on the bottom-left corner, and there are a lot of light colored themes. I had only ever seen the dark theme here until today. I picked a new darker green that looks OK to me in both dark and light themes. (I picked green, because the Julia REPL gives me a green julia> prompt by default.)

#1d9a1d

1 Like

But why did someone then use the tag julia-repl? I anticipation for an implementation?

Äähm. No matter how small the difference, there is one. And aliasing one to the other, gives no other result. So, calling julia-repl will always call the normal highlighting, as is.

Because even highlighting with the wrong highlighter can sometimes give some color/flavor to the text, but not in this example.

What is the difference?

1 Like

And how does this make any sense? Using a mode, that’s not working? I assume it worked at some point, or the committer thought, it might get supported at some point.

Among other things, it breaks on the prompt.

If I change the highlighting to julia this is the result:

Screenshot_20231004_085741

And I don’t know if that is on purpose:

Screenshot_20231004_085553

But that is not the point. GitHub, the Documentation, and probably other sources are not applying julia-repl, so this is an issue. It seems to work on Discourse.

And aliasing two different modes is just wrong. It defeats the entire point of having distinct modes in the first place.

Feel free to contribute by removing the language tags.

What do you mean? As demonstrated above it doesn’t break on the prompt, on the contrary, the prompt is detected and classified as a prompt.

GitHub uses a different highlighter, and the documents are not written for GitHub.

Again, they are not just aliased. julia-repl does prompt detection for julia> and then the inner content is highlighted with the regular julia highlighter, as it should. Do you have an example where this doesn’t work?

2 Likes

I would rather care, that julia-repl works. And I was not sure, where this is implemented, so that I could link to it in an issue that I would raise. So I thought I ask here, first. :slightly_smiling_face:

Again, it does on highlight.js:

Screenshot_20231004_093552
Screenshot_20231004_093618

Thats just very confusing documentation, but what they mean is that for the language Julia there are two “flavors” (julia and julia-repl). The implementation of julia-repl is here: https://github.com/highlightjs/highlight.js/blob/58dc5961f6f2bb8bc8bb1e7ce39f268a2fdd874f/src/languages/julia-repl.js

4 Likes

Feels like the Julia highlighting mode could ideally detect REPL syntax and change its rules. But not sure how feasible that would be.

4 Likes