Default code formatting language varies unexpectedly

I noticed this issue at forums dot swift dot org (sorry, as a new user I’m not allowed >2 links…), where the default code formatting language is Swift. See this thread in particular.

With three-backticks style code formatting, the Swift highlighting rules do seem to be in effect:

let comparators: [(Int, Int) -> Bool] = [ (<), (<=), (>), (>=) ]

It doesn’t show up properly here; perhaps Swift highlighting was something that the forums dot swift dot org admins had to custom-add to highlight.js.

Anyway, the issue is that if you use the “Preformatted text” icon and use indentation to denote code, the code is formatted according to a different set of highlighting rules:

let comparators: [(Int, Int) -> Bool] = [ (<), (<=), (>), (>=) ]

The exact appearance on this forum is different from what you’d see on forums dot swift dot org. The first example above with red highlights on some of the commas and parens is what you get with “Preformatted text” on forums.swift.org. With ```, you get something that looks like that same line but without the red highlighting. I’m guessing that the red highlighting comes from the JavaScript highlighting grammar?

I don’t know enough about Discourse to identify the specific problem, but my guess is that “Preformatted text” handling is not properly honoring the per-forum default code language setting.

Please holler if you need any additional info.

2 Likes

Note, this happens if default code lang is set to swift

I think that if we are setting default code lang to something except for auto we should always not apply any code formatting to triple space prefixed stuff.

So:

If default_code_lang == swift

```
print("this is swift highlighted!")
```

And

   print("should not be highlighted!")

IF autohighlight all code is enabled.

THEN

   print("should be highlighted!")

@maja can you have a look at this? The issue is that the “auto” lang is not picked if autohighlight all code is enabled.

6 Likes

I tried to figure out what’s going on here and it looks like it’s already working as you described it, @sam :

I added swift to the highlighted_languages and set the default_code_lang to swift

When autohighlight_all_code is disabled:

  • with fenced code blocks: swift highlighting gets applied :white_check_mark:
  • with 4 spaces indent: no syntax highlighting :white_check_mark:

When autohighlight_all_code is enabled:

  • with fenced code blocks: swift highlighting :white_check_mark:
  • with 4 spaces indent: highlight.js tries to automatically detect the language since <pre><code> is baked without any lang class specified and it sometimes assumes a wrong language - seems like a highlight.js problem :x:
5 Likes