Improved syntax highlighting for Julia on the web

Hello everyone, I made some improvements to the Julia syntax highlighter in highlight.js. This library is used by Documenter.jl, Franklin.jl, this discourse forum and many other tools to highlight Julia code, so hopefully it will be useful. I wrote a post about it for anyone interested:

Comments and suggestions welcome!

/Fredrik

61 Likes

Great work! I would love to see these changes one day added to Chroma, which is used by Hugo. Its Julia highlighting is really underwhelming compared to this.

4 Likes

This is is a great post and a great effort, also the StarWarsArray code is hilarious!

6 Likes

If this is upstreamed, it would improve a lot of user-facing code since Stackoverflow recently moved to using highlight.js

5 Likes

Nice work! Thank you! Is there any chance these highlights can be reproduced in other environments, like julia-vim , or jlcode?

Thanks, I knew I read about some forum switching, but couldn’t find which one when trying to find it. I did include StackOverflow in the post, just didn’t know it was so recent.

Probably. In particular contextual type highlighting is something that I have wanted in vim. I opened an issue: Contextual highlighting of types · Issue #216 · JuliaEditorSupport/julia-vim · GitHub

1 Like

Is this an improvement to the support or the first time it is supported? According to this: https://github.com/highlightjs/highlight.js/blob/master/SUPPORTED_LANGUAGES.md
there is no support for Julia at all.

It would be great to get this on stackoverflow, since right now the syntax highlighting isn’t just bad – it’s worse than nothing. It’s probably using a highlighter from a completely different language, Matlab perhaps.

1 Like

It is an improvement; highlight.js it is used by many tools for julia code already, just doesn’t tag as much. I don’t know why it isn’t on that list though, probably just an oversight.

1 Like

If I understand correctly it has to be on that list to be deployed by stackoverflow

If your post doesn’t have the correct highlighting, it’s possible it’s not supported. Please look at the list of supported languages. If your language is not on the list, it needs to be created within the highlight.js project before it can be deployed by Stack Exchange.

This would be a huge improvement.

1 Like

Okay, I submitted https://github.com/highlightjs/highlight.js/pull/2797, let’s see what they say.

6 Likes

Yeah, the last I heard Stack Overflow was just using the default, language agnostic highlighter for Julia code. There were some posts on Meta about it.

EDIT: Of course that was before they switched to highlight.js.

The Julia REPL highlighting is particularly bad right now on Stack Overflow.

Great work, thanks!

In the C example, the function arguments are colored differently than the types, is it possible to also get that in Julia (with your improvements arguments and types have the same color)?

That is because the C highlighter mark int and char as keyword. If you look at the Julia example you see that the argument is tagged with params and the argument types as type. The fact that they look the same in my improved example is just that the specific color scheme assigns the same color to params and type.

2 Likes

This PR is now merged, and I asked for StackExchange to enable Julia syntax highlighting here: Julia syntax highlighting - Meta Stack Exchange

8 Likes

How can one add this to a Franklin.jl site now? Is it more realistic to just wait until highlight.js tags a new version or should I add this manually to the page (I saw that the default version pulls it in via node)

It is very easy; replace the file _libs/highlight/highlight.js with the one I provided and either make sure you use optimize(...; prerender=false) such that this new version is used when you deploy too, or you tell Franklin to prerender with the new file by setting Franklin.HIGHLIGHTJS[] = "/path/to/new/highlight.js".

3 Likes

Thank you!

Just to make this more prominent:

Please Upvote!

https://meta.stackexchange.com/questions/355928/julia-syntax-highlighting

3 Likes

https://github.com/codemirror/CodeMirror/tree/master/mode/julia

this probably needs some contrib as well.

3 Likes

Thank you for the great effort to improve highlighting!

Now, I found some lexing errors, where should I send the feedbacks?

For example:

Edit:
I appreciate your swift fix.

3 Likes