# Strange code highlighting in our discourse

**URL:** <https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469>\
**Category:** Meta\
**Created:** [June 27, 2021, 7:47pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469 "2021-06-27T19:47:27Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [June 27, 2021, 7:47pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/1 "2021-06-27T19:47:27Z")

</div>

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

````auto
# Test

This should be *markdown* and highlighted _accordingly_.

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

````

````auto
# Test

This should be *markdown* and highlighted _accordingly_.

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

````

````auto
# 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.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [June 27, 2021, 7:56pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/2 "2021-06-27T19:56:12Z")

</div>

I believe that Discourse is open source ([GitHub - discourse/discourse: A platform for community discussion. Free, open, simple.](https://github.com/discourse/discourse)) 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.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [June 27, 2021, 8:07pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/3 "2021-06-27T20:07:05Z")

</div>

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

> **[How do I select a language in code blocks?](https://meta.discourse.org/t/how-do-i-select-a-language-in-code-blocks/19247)**
>
> Is there a way to suggest a highlighting language in a Discourse post? SE uses a magic HTML comment; that mechanism doesn’t appear to work in Discourse. Summary: When using triple backticks to enclose code blocks, you can type a language name...

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

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [June 27, 2021, 8:15pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/4 "2021-06-27T20:15:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [June 27, 2021, 8:17pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/5 "2021-06-27T20:17:12Z")

</div>

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

> **[Support auto-highlighting for preformatted code](https://meta.discourse.org/t/support-auto-highlighting-for-preformatted-code/14234)**
>
> Highlightjs offers “auto highlighting” out of the box. It’d be nice if this functionality could be exposed to Discourse through an admin pref (eg. “Auto highlight preformatted code”). I understand this won’t be relevant for all forums but for some...

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

---

<div class="post-metadata">

**Author:** ![lkedward](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lkedward/32/72_2.png) [@lkedward](https://fortran-lang.discourse.group/u/lkedward)\
**Post date:** [June 28, 2021, 7:46am UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/6 "2021-06-28T07:46:06Z")

</div>

> [@certik](#):
>
> I didn’t find it in the settings.

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.

> [@awvwgk](#):
>
> I think it is preferable to not highlight code blocks at all if the language is unknown rather than use some random highlighting.

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.

> [@awvwgk](#):
>
> The current automatic syntax detection seems to be incredibly broken.

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

> **[GitHub - highlightjs/highlight.js: JavaScript syntax highlighter with...](https://github.com/highlightjs/highlight.js)**
>
> JavaScript syntax highlighter with language auto-detection and zero dependencies. - GitHub - highlightjs/highlight.js: JavaScript syntax highlighter with language auto-detection and zero dependencies.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [June 28, 2021, 8:16am UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/7 "2021-06-28T08:16:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [June 28, 2021, 8:32am UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/8 "2021-06-28T08:32:26Z")

</div>

> [@lkedward](#):
>
> I strongly suggest not changing this default since it will affect the formatting of existing posts.

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.

---

<div class="post-metadata">

**Author:** ![lkedward](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lkedward/32/72_2.png) [@lkedward](https://fortran-lang.discourse.group/u/lkedward)\
**Post date:** [June 28, 2021, 10:09am UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/9 "2021-06-28T10:09:23Z")

</div>

> [@awvwgk](#):
>
> 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.

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

> [@awvwgk](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [June 28, 2021, 3:24pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/10 "2021-06-28T15:24:42Z")

</div>

@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](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/13b27bb6d1d5037e1c575b6710bcdf2f63c3b927.png)

---

<div class="post-metadata">

**Author:** ![ashe](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@ashe](https://fortran-lang.discourse.group/u/ashe)\
**Post date:** [June 28, 2021, 4:18pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/11 "2021-06-28T16:18:20Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 13, 2022, 10:21pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/12 "2022-02-13T22:21:34Z")

</div>

Is the syntax highlighting on Discourse “broke”?

Please see this [**thread**](https://fortran-lang.discourse.group/t/tweeting-fortran/2425/19) 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](https://twitter.com/fortrantip/status/1475453317526528003) by @Beliavsky on that tweeting thread which under markdown gets “highlighted” on this site as follows:

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/0/06ef5c5e2d9cd9e32005d41a21e7683d4b7c1ef6.png)

Now compare it to FortranTip:

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/9/95c625ddf9bce2b5583525bccbf4a9df192b2456.jpeg)

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [February 14, 2022, 10:26pm UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/13 "2022-02-14T22:26:47Z")

</div>

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

_Some random code for highlighting:_

```auto
/// 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](https://highlightjs.org/static/demo/), let me know if there is something you like more (we can have different themes for light / dark mode).

---

<div class="post-metadata">

**Author:** ![fortran4r](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fortran4r](https://fortran-lang.discourse.group/u/fortran4r)\
**Post date:** [February 15, 2022, 1:04am UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/14 "2022-02-15T01:04:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [February 15, 2022, 11:14am UTC](https://fortran-lang.discourse.group/t/strange-code-highlighting-in-our-discourse/1469/15 "2022-02-15T11:14:01Z")

</div>

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.
