Strange code highlighting in our discourse

Out of curiosity, is there a way we can configure our discourse instance to correctly highlight code blocks? The current automatic syntax detection seems to be incredibly broken.

Here are three code blocks with minimal differences but completely different syntax highlighting. For all three I specified md in the code fence as language hint

# Test

This should be *markdown* and highlighted _accordingly_.

```fortran
module m_ifthen
  implicit none
end module m_ifthen
```
# Test

This should be *markdown* and highlighted _accordingly_.

```fortran
module m_ifthen
  implicit none
contains
end module m_ifthen
```
# Test

This should be *markdown* and highlighted __accordingly__.

```fortran
module m_ifthen
  implicit none
end module m_ifthen
```

Is there something we can do allow a meaningful syntax highlighting selection in this markdown dialect? Maybe defaulting to no highlighting instead of some random highlighting would already be a step forward.

1 Like

I believe that Discourse is open source (GitHub - discourse/discourse: A platform for community discussion. Free, open, simple.) and I assume they use some library to do syntax highlighting. If we can figure out which one they use, we can submit patches to fix the highlighting.

A quick search on the discourse meta channel reveals that this can be configured by the admins:

Could you check whether we have fortran actually in this list, please?

I didn’t find it in the settings. @milancurcic, @lkedward ?

It seems there is also a possibility to disable the automatic highlighting of code blocks

I think it is preferable to not highlight code blocks at all if the language is unknown rather than use some random highlighting.

Thanks for bringing this up @awvwgk. I’ve now explicitly added Fortran to the highlighted languages list, though I think it was detected automatically before.

The default is for automatic language detection (not random highlighting). I strongly suggest not changing this default since it will affect the formatting of existing posts. You can use nohighlight as the hint if you do not want any auto highlighting for your code block.

I’m not sure why your markdown examples above are broken - I would suggest engaging with the developers here:

1 Like

Random might not be the right word, I agree, but it felt random when I originally wrote the above post and changing a single character completely changed the highlighting. Now with the side change all examples seem to use the same highlighting.

Also, can we document this behaviour somewhere? Like having a list of supported syntax highlightings and explaining the nohighlight descriptor? This behaviour, at least for me, wasn’t really obvious.

Adding fortran to the supported languages already changed a lot of posts, to the better I guess. I checked a few code blocks I posted (with and without fortran language hint) and now they are all looking properly highlighted. Thanks a lot.

1 Like

Good point, yes this would be useful to have documented somewhere.

Ah yes true, but I’m glad to hear it’s an improvement.

1 Like

@awvwgk , @lkedward

Thanks much for your follow-up on this and bringing about the improvements to Fortran source highlighting at this site, code markup looks much better now.

Also, apologies for attempting to direct some work toward you instead of trying to look it up myself at the highlightjs site linked upthread. You may know better than I will be able to discover myself. The question is this: do you know whether the highlighting for PRINT, ALLOCATED, etc. is as intended: it appears different from other “words” in the Fortran “dictionary”.

image

highlight.js distinguishes between “keyword” and “built_in”, which are bold and blue in your example. The “built_in” set of words is mostly intrinsic procedures, like allocated. But it also includes some other words like print and deallocate that you would think would be keywords. That’s why they are highlighted differently.