Code highlighting happening when auto-highlighting not enabled

At our forum we recently noticed that code is being highlighted automatically, even though autohighlight all code is off, and highlighted languages is empty. We did this because we have a lot of code in languages that aren’t supported. But now they’re being detected as C#.

screenshot of settings

Example post:

2 Likes

Can we reproduce this next week @tshenry?

2 Likes

I can repro on Try. It looks like the autohighlight_all_code site setting isn’t being honored. The functionality appears to be enabled whether the setting is checked or not. The highlighted languages setting affect the language detection, but if everything is removed, it appears to default to csharp when it detects anything code-like.

The most recent core change that involved syntax highlighting appears to be @joffreyjaffeux’s refactor:

https://github.com/discourse/discourse/commit/f8062300da274cae559c17509e7c0d11458a065f

1 Like

will have a look tomorrow

3 Likes

I don’t think anything regressed here, at best it was working incorrectly before I did my various fixes few weeks ago.

What’s happening here is that by default we always add auto and nohighlight to the list of acceptable code classes. If you don’t define any language when defining your codeblock it will use the value of default_code_lang which is by default auto, if you set it to nohighlight you should get the expected result. Note that you will have to rebuild the html of the posts. You also probably don’t need to empty highlighted_languages it has no effect if you choose nohighlight.

4 Likes

This is happening for brand new posts, so something definitely changed.

(And yes, I’ve realised I don’t need an empty languages list, we do occasionally get some JS and Python, so might as well let them be highlighted if anyone adds the code.)

is giving me:

What’s not working as expected?

What’s not working is that when “autohighlight all code” isn’t selected, non-tagged code blocks still get autohighlighted.

No that’s not the case. And it’s also not the bug you reported in OP.

1 Like

Fenced code blocks (```) always get auto-highlighting, indented code blocks (    ) are what that setting affects.

2 Likes

Oohh. Well… why is there a difference? And can that be changed?

And something was changed in Discourse, because fenced code blocks didn’t used to be autohighlighted. We’ve had our Discourse forum for almost 2 years now. Until the most recent update fenced code blocks were not autohighlighted.

Can that be changed? Yes, the “autohighlight all code” setting makes the behavior consistent across both types of code blocks.

If you never want auto-highlighting except when a language is explicitly specified,

Yes maybe, but as I said, I don’t see anything not working as expected now, so my guess so far is that something was not working by error before, and you were relying on this behavior.

My guess is that it was detected as a language which was not loading correctly and was ending up in the block not being highlighted.

2 Likes

Hmm okay. Can we turn this into a request then that the description (and even name) of the “autohighlight all code” setting be made more accurate? Could it be changed to “autohighlight indented code” with description “Apply code highlighting to indented code blocks even when they didn’t explicitly specify the language.”

1 Like

Sure, feel free to make a PR.

Also i’m happy to fix an issue if you find one.

2 Likes

Looking at my console I saw something suspicious too:

This is still not new, but I think our injected value of nohighlight is wrong and should be no-highlight. This is also giving more credit to my guess, of what you saw detected as a code which couldn’t be found and was fallbacking to no-highlight. I will make the change.

3 Likes

I’ll make a PR for changing the descriptions.

One question lingers: how was csharp an option when highlighted languages was empty? Does an empty setting act as if it had the default list?

1 Like

Yes I was wondering the same.

My understanding is that at this line: https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/highlight-syntax.js#L42

When we remove this class and then ask highlightjs to highlight the codeblock, we don’t rely on markdown anymore and this is a client side change so the classes limitations don’t apply anymore.

Basically:

  • not auto → will add a class from the list in generated markup which is used by highlightjs
  • auto → will add lang-auto in the generated markup, which is then removed at runtime and lets highlightjs decide of the highlighting

Huh. That somewhat limits its usefulness. I’ll see if I can come up with a helpful clarification for the description.

As another data point, we use text as the default, which is an alias for nohighlight and have had good results with it.

Have noticed some glitches from time to time, but overall this has been the best approach for us, and looks better raw, for example:

```text
jtl_name=$(/usr/local/bin/kubectl exec -it --namespace=$namespace $master_pod -- bash -c "ls -tr /*.jtl" | awk '{sub(/\r$/, ""); print}')
```

Producing:

jtl_name=$(/usr/local/bin/kubectl exec -it --namespace=$namespace $master_pod -- bash -c "ls -tr /*.jtl" | awk '{sub(/\r$/, ""); print}')

We have a custom icon for the default text fenced code block as well in composer.

4 Likes