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#.
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:
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.
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.)
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.
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.
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.”
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.
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