# Automatic highlighting fails for some fenced code blocks

**URL:** https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503
**Category:** Bug
**Tags:** fixed, bug
**Created:** [July 25, 2026, 8:16am UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503 "2026-07-25T08:16:46Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![irkode](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/irkode/32/571817_2.png) [@irkode](https://meta.discourse.org/u/irkode)
#### Post date: [July 25, 2026, 8:16am UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/1 "2026-07-25T08:16:46Z")

</div>

This happens in my development setup, in a live discourse forum [https://discourse.gohugo.io/](https://discourse.gohugo.io/) and the discourse playground.

In general when a fenced code block has no language set we get autodetection. This succeeds in a normal Highlight.JS usage (node or browser) all of the time by selecting something.

Discurses seems to save such blocks as `<code class="lang-auto">`. which is no valid language 😉

I talked with the [AI-Bot ask.discourse.cm…30386](https://ask.discourse.com/t/fixing-discourse-auto-code-highlighting-failures/30386):  
it told that you do some magic ON RUNTIME in removing the the “lang-auto” and then let _highlight.js_ redo the auto work. It looks like that update is not all the time happening BEFORE highlight.JS touches the code. So that looks like a quirk in the highlight.js integration and auto-detect implementation within discourse.

unsetting a default language will also not work, cause then the “remove lang-auto” does not work and looks like the discourse way does not redo the autodetect.

- setting the language manually works all of the time – for built-in and a custom (hugo-templates) language added via the PluginAPI (registerHLJSLanguage) as a theme component.
- without a language it fails depending of the content() race conditions, timings

Setting default language to “text” won’t be a way cause we want that highlighted and we want autodetection for usability (and it works…)

Here’s a Markdown example, where the second code block fails (ofc only if the default language is set to “auto”. Easily reproducable with the below markdown on [https://try.discourse.org/](https://try.discourse.org/)

and within 24h : [https://try.discourse.org/t/failed-discourse-autodetection-breaks-rendering-of-fenced-code-blocks/1504](https://try.discourse.org/t/failed-discourse-autodetection-breaks-rendering-of-fenced-code-blocks/1504)

````markdown
# This works

```
{{ $currentPagemd5 := md5 $currentPage.File.Path }}
    {{ $currentPagemd5ext := (print $currentPagemd5 ".json")}}
```

# This failed and assigned "lang-auto"

```
{{ $currentPage := . }}
{{ $jsonFile := "" }}
{{ with $currentPage.File }}
    {{ $currentPagemd5 := md5 $currentPage.File.Path }}
    {{ $currentPagemd5ext := (print $currentPagemd5 ".json")}}
    {{ $files := readDir "data/commits/" }}
    {{if gt (len $files ) 0}}
        <div class="bord-bas"></div>
        <div>
        <details>
            <summary>Changements de la page</summary>
            <div>
            {{ range $files }}
                {{ if eq .Name $currentPagemd5ext }}
                    {{ $jsonFile := $currentPagemd5 }}
                    {{ $data := index $.Site.Data.commits $jsonFile }}
                    <ul>
                    {{ range $data }}
                        <li>
                            {{ dateFormat "02/01/2006" .commit_date }} : {{ trim .message "\n" }} ({{ .author }})
                        </li>
                    {{ end }}
                    </ul>
                {{ end }}
            {{ end }}
            </div>
        </details>
        </div>
    {{ end }}
{{ end }}
```

````

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [July 25, 2026, 8:20am UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/2 "2026-07-25T08:20:07Z")

</div>

Perhaps it is simply that Discourse and HighlightJS cannot recognize the language, hence making it as such.

---

<div class="post-metadata">

### Author: ![irkode](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/irkode/32/571817_2.png) [@irkode](https://meta.discourse.org/u/irkode)
#### Post date: [July 25, 2026, 8:42am UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/3 "2026-07-25T08:42:24Z")

</div>

The emphasis here is on Discourse **and** Highlight.JS

- Highlight.JS alone always detects the language and selects it

- In Discourse, however, the language is not always detected at runtime without the lang attribute.

#### Hugo Forum: 57307 Post 6:

Here is a post from the forum: [Proposal: Add highlighting support for Go/Hugo templates to Discourse - #6 by irkode - Meta - HUGO](https://discourse.gohugo.io/t/proposal-add-highlighting-support-for-go-hugo-templates-to-discourse/57307/6?u=irkode)

- The first code block is introduced with ` ```hugo-html`.  
This results in \<code="lang-hugo-html\>
- The second block with `````  
This results in \<code=“lang-auto”\>

Both cases are highlighted correctly.

#### Hugo Forum: 57367 Post 2:

And here is another one where it doesn’t work: [How to capture/generate revisions of page? - #2 by Welsh - support - HUGO](https://discourse.gohugo.io/t/how-to-capture-generate-revisions-of-page/57367/2?u=irkode)

Here too, “lang-auto” is assigned, but the automatic detection at runtime does not work.

It somehow depends on the Discourse implementation - possibly depending on the content due to timeouts, promises …

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [July 25, 2026, 8:56am UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/4 "2026-07-25T08:56:01Z")

</div>

Maybe I’m misunderstanding, but this is what I’m seeing in the 1st link you posted:

 ![image](https://global.discourse-cdn.com/meta/original/4X/9/2/f/92fa511cb907ec8c903560f5fbfb409fbbe12d23.png)

Neither shows `lang-auto`, and both are highlighted in the same way, although you did not specify the codeblock language for the second codeblock.

---

<div class="post-metadata">

### Author: ![irkode](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/irkode/32/571817_2.png) [@irkode](https://meta.discourse.org/u/irkode)
#### Post date: [July 25, 2026, 8:59am UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/5 "2026-07-25T08:59:22Z")

</div>

Thats the Developer Console - it shows the code **after** highlight.js has successfully autodetected.

You will have to look at the page source (Ctrl+u) where you will see the lang-auto

which then will trigger discourse way of auto-highlighting by removin the lang-auto and try again check the second post i linked, same situation but there detection fails.

and yes - without the discourse framework around it works

---

<div class="post-metadata">

### Author: ![irkode](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/irkode/32/571817_2.png) [@irkode](https://meta.discourse.org/u/irkode)
#### Post date: [July 25, 2026, 9:06am UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/6 "2026-07-25T09:06:51Z")

</div>

When moving to bare Highlight.JS:

- a code wrapped in `<code class="lang-auto">` won’t get highlighted unless we have a grammar for “auto” 😉

- we could restart the autodetection for those using a plugin:

I could not get that working in Discourse due to the way Plugins, Grammars are integrated with the API and due to the DOM capsulation of the highlight.js engine and methods, async with promise, … and even if we might get race conditions with the default discourse live rendering

---

<div class="post-metadata">

### Author: ![renato](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/renato/32/383632_2.png) [@renato](https://meta.discourse.org/u/renato)
#### Post date: [July 28, 2026, 5:49pm UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/8 "2026-07-28T17:49:27Z")

</div>

The code is not highlighted because of a condition that skips auto-detection for any code longer than 1k characters.

[This PR](https://github.com/discourse/discourse/pull/42106) changes the logic to use a bounded 2k prefix to detect the language.

---

<div class="post-metadata">

### Author: ![irkode](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/irkode/32/571817_2.png) [@irkode](https://meta.discourse.org/u/irkode)
#### Post date: [July 28, 2026, 8:41pm UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/9 "2026-07-28T20:41:38Z")

</div>

thx for the investigation and the fix - heading forward to see it in action.

love it already

especially the “use first 2000” will bring a big benefit.

---

<div class="post-metadata">

### Author: ![irkode](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/irkode/32/571817_2.png) [@irkode](https://meta.discourse.org/u/irkode)
#### Post date: [July 29, 2026, 3:52pm UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/10 "2026-07-29T15:52:21Z")

</div>

looks like this is live

> **[How to capture/generate revisions of page?](https://discourse.gohugo.io/t/how-to-capture-generate-revisions-of-page/57367/2?u=irkode)**
>
> Hi I had the similar need. Here is the method I use : I commit with a message in my CI i call a schell script : make a directory data/commits find all the markdown files for each create a json file with all the commits of this file in my...

thx

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [August 12, 2026, 9:09pm UTC](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503/11 "2026-08-12T21:09:56Z")

</div>


