# Default code formatting language varies unexpectedly

**URL:** https://meta.discourse.org/t/default-code-formatting-language-varies-unexpectedly/80418
**Category:** Bug
**Created:** [February 12, 2018, 9:59pm UTC](https://meta.discourse.org/t/default-code-formatting-language-varies-unexpectedly/80418 "2018-02-12T21:59:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![GSnyder](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gsnyder/32/120315_2.png) [@GSnyder](https://meta.discourse.org/u/GSnyder)
#### Post date: [February 12, 2018, 9:59pm UTC](https://meta.discourse.org/t/default-code-formatting-language-varies-unexpectedly/80418/1 "2018-02-12T21:59:49Z")

</div>

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](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/15) in particular.

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

```swift
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](http://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.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [February 12, 2018, 10:08pm UTC](https://meta.discourse.org/t/default-code-formatting-language-varies-unexpectedly/80418/2 "2018-02-12T22:08:04Z")

</div>

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

```plaintext
   print("should not be highlighted!")

```

IF `autohighlight all code` is enabled.

THEN

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

---

<div class="post-metadata">

### Author: ![maja](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maja/32/129533_2.png) [@maja](https://meta.discourse.org/u/maja)
#### Post date: [February 13, 2018, 6:42pm UTC](https://meta.discourse.org/t/default-code-formatting-language-varies-unexpectedly/80418/3 "2018-02-13T18:42:54Z")

</div>

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 ✅
- with 4 spaces indent: no syntax highlighting ✅

When `autohighlight_all_code` is enabled:

- with fenced code blocks: swift highlighting ✅
- 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 ❌

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [July 9, 2018, 10:32pm UTC](https://meta.discourse.org/t/default-code-formatting-language-varies-unexpectedly/80418/4 "2018-07-09T22:32:21Z")

</div>


