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.

2 Likes

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.

1 Like

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?

1 Like

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.

Is the syntax highlighting on Discourse “broke”?

Please see this thread and the comment, “The syntax highlighting of FortranTip is amazing, much better than that of Fortran Discourse.”

But the problem I notice on a couple of web browsers is hardly any highlighting at all beyond plain text for Fortran code at this site for a while now.

See for example one of the tips by @Beliavsky on that tweeting thread which under markdown gets “highlighted” on this site as follows:

Now compare it to FortranTip:

1 Like

Added some sparkles of color, let me know what you think.

Some random code for highlighting:

/// Context manager for the library usage
typedef struct foopss_context* foopss_context;

/// Define callback function for use in custom logger
typedef void (*foopss_logger_callback)(char*, int32_t, void*);

// ...

/// Set custom logger function
void
foopss_set_context_logger(foopss_context /* ctx */,
                          foopss_logger_callback /* callback */,
                          void* /* userdata */);

// ...

/// Example for a callback function
void
example_callback(char* msg, int32_t len, void* udata) {
  /* print len chars from msg, since it need not be NUL terminated */
  printf("[callback] %.*s\n", msg, len);
}

You can browse available themes at highlight.js demo, let me know if there is something you like more (we can have different themes for light / dark mode).

1 Like

Looks better. Should there be a vote to decide which theme to use?

Yeah, it is kind of an ad hoc decision from my side after testing three themes (Atom One, GitHub, VS) which have both light and dark mode available.

I used the Atom One theme because we get some purple while the GitHub favors red and the VS one blue for keywords. We can vote but it is a long list, we should narrow it down first.