# Highlight template code in discourse forum using highlightjs-hugo

**URL:** <https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307>\
**Category:** Meta\
**Tags:** highlight, forum\
**Created:** [June 28, 2026, 7:37pm UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307 "2026-06-28T19:37:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [June 28, 2026, 7:37pm UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/1 "2026-06-28T19:37:26Z")

</div>

I developed highlight.js grammars and _Discourse_ plugins for Go and Hugo templates.

Should be easy to integrate and result in better display of _Hugo templates_ in Discourse.

Even without utilizing the advanced stylings you will get an emediate effect of proper highlighting the standard template stuff.

## Features

 ![highlightjs-hugo](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/3/f/3fd1af8cff739fcb7d4b326fc859af9c3cc5a50e.png)

### Highlight.js Grammars

- Support for:

- Hugo and Go templates

- Html and Text templates

- Full coverage of:

- Improved automatic language detection

- Advanced highlighting scopes

- Prebuilt **\*\*browser bundle\*\*** for immediate use

### Discourse Integration

- Theme components built on the provided grammars
- Enables proper syntax highlighting in Discourse environments

## Documentation and Repository

> **[Highlight 4 Hugo](https://irkode.github.io/highlightjs-hugo/)**
>
> Advanced syntax highlighting for Hugo and Go templates with Highlight.js - plus Discourse theme components and Chroma-friendly setup.

> **[GitHub - irkode/highlightjs-hugo: Advanced syntax highlighting for Hugo templates](https://github.com/irkode/highlightjs-hugo)**
>
> Advanced syntax highlighting for Hugo templates

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 21, 2026, 9:01am UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/2 "2026-07-21T09:01:29Z")

</div>

When i try to install this in the Theme components tab, I get this error:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/e/f/ef02af31aea87bc91216c34ed000825281cb7318.png)

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [July 21, 2026, 9:30am UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/3 "2026-07-21T09:30:28Z")

</div>

mmh, maybe a shortage on the install docs…

please verify you select the language-grammars zip **not** the full `highlightjs-hugo-discourse.zip`

- download the complete zip from: [https://github.com/irkode/highlightjs-hugo/releases/download/v0.3.0/highlightjs-hugo-discourse.zip](https://github.com/irkode/highlightjs-hugo/releases/download/v0.3.0/highlightjs-hugo-discourse.zip)

- extract that zip, it contains four separate zip files, one for each language+variant

- select one of these zip files in the import from your device dialog  

- don’t forget to go back to the list of components , EDIT and activate for the Theme  

just replayed that on my test instanace and it works fine.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 21, 2026, 10:11am UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/4 "2026-07-21T10:11:44Z")

</div>

Yea, right. Testing:

```auto
{{ with resources.Get "css/main.css" | css.Build }}
  {{ if hugo.IsDevelopment }}
    <link rel="stylesheet" href="{{ .RelPermalink }}">
  {{ else }}
    {{ with . | fingerprint }}
      <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
    {{ end }}
  {{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 21, 2026, 10:14am UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/5 "2026-07-21T10:14:22Z")

</div>

Maybe I’m missing something, but I have activated this on both Hugo dark and Hugo light, but I don’t see any difference when I toggle this on/off.

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [July 21, 2026, 10:37am UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/6 "2026-07-21T10:37:52Z")

</div>

I would say you used a fenced code block with **```html** resulting in a fixed class using highlight.js XML language  
`<code class="lang-html hljs language-xml"`

you want either no class for autodetection or hugo-html 🙂

- fixed `hugo-html` results in `<code class="lang-hugo-html hljs language-hugo-html">`
- autodetect results in `<class="hljs language-hugo-html">`

#### with class: ```hugo-html

```hugo-html
{{ with resources.Get "css/main.css" | css.Build }}
  {{ if hugo.IsDevelopment }}
    <link rel="stylesheet" href="{{ .RelPermalink }}">
  {{ else }}
    {{ with . | fingerprint }}
      <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
    {{ end }}
  {{ end }}
{{ end }}

```

#### autodetect (just ```)

```auto
{{ with resources.Get "css/main.css" | css.Build }}
  {{ if hugo.IsDevelopment }}
    <link rel="stylesheet" href="{{ .RelPermalink }}">
  {{ else }}
    {{ with . | fingerprint }}
      <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
    {{ end }}
  {{ end }}
{{ end }}

```

importing hugo-text would allow to use ```hugo-text which would only highlight the hugo actions leaving the html unstyled.

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [July 28, 2026, 7:59pm UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/7 "2026-07-28T19:59:49Z")

</div>

Just for Info:

while checking how it’s going with the highlight I found that sometimes blocks are not highlighted at all…and not only with my grammar thought about timing linits, … but it was a hard CAP by code block size.

> **[Automatic highlighting fails for some fenced code blocks](https://meta.discourse.org/t/automatic-highlighting-fails-for-some-fenced-code-blocks/408503)**
>
> This happens in my development setup, in a live discourse forum 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...

and here is a PR that should solve the problem

- Still a hard limit on 30000 bytes
- raise the autodetect limit to 2K
- if it’s longer, use the first 2000 bytes to try autodetect

> <https://github.com/discourse/discourse/pull/42106/>
>
> Fenced code blocks with no language are cooked as \`\<code class="lang-auto"\>\` and… highlighted via auto-detection at runtime. Blocks over 1000 characters of markup were skipped entirely, and that early-out left the invalid \`lang-auto\` class on the element, so they rendered unhighlighted and unstyled. The threshold was arbitrary and small enough to catch ordinary snippets.
> 
> Auto-detection weighs the content against every enabled language, so its cost grows with both block size and language count. Rather than cap by size, detect the language from a bounded prefix and then highlight the full block with just that language, keeping detection cost constant regardless of block size.
> 
> The \`lang-auto\` marker is now always removed, so a block that still cannot be highlighted degrades to plain text instead of carrying an invalid class.

and live: [How to capture/generate revisions of page? - #2 by Welsh](https://discourse.gohugo.io/t/how-to-capture-generate-revisions-of-page/57367/2)

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [September 11, 2026, 10:11am UTC](https://discourse.gohugo.io/t/highlight-template-code-in-discourse-forum-using-highlightjs-hugo/57307/8 "2026-09-11T10:11:15Z")

</div>

[Highlightjs-hugo v0.5.0](https://github.com/irkode/highlightjs-hugo/releases/tag/v0.5.0) is out which brings (compared to 0.3.1):

- improved whitespace between actions handling: less noisy html, faster
- fixed {{-` ` space delimiter which now is exactly ONE whitespace `\t\s\n`
- functions from Hugo v0.156 and v0.166

fetch it here: [https://github.com/irkode/highlightjs-hugo/releases/download/v0.5.0/highlightjs-hugo-discourse.zip](https://github.com/irkode/highlightjs-hugo/releases/download/v0.5.0/highlightjs-hugo-discourse.zip)

after installation and a page reload lines 1 & 3 will be highlighted as _built\_ins_

```auto
{{- css.ChromaStyles }}
{{- css.ChromaStyle }}
{{- resources.Publish }}
{{- resources.Pub }}

```

cc: @bep
