Bug or Feature? language-less code blocks use "default" formatting

I add this to a markdown file:

```
@all-contributors add @YOURUSERNAME for YOURCONTRIBUTIONTYPE
```

and Hugo is rendering it in the hugo-default dark theme (even with me having defined styles in css for highlighting)

I add this:

```plaintext
@all-contributors add @YOURUSERNAME for YOURCONTRIBUTIONTYPE
```

and it’s showing up in the color scheme I set.

Also… it reacts to my custom css additions that do some magic with the data properties.

This is either a bug or something expected. If my code block DOES NOT have a language type added it will default to the standard theme and does NOT add any of the options I set (like noClassesin this case).

My config in config/*/markup.toml is this:

[highlight]
anchorLineNos = true
codeFences = true
noClasses = false
[goldmark.renderer]
unsafe = true # meh, no like, but required for contributors list

and I added the styles for “my” theme to the stylesheet. “MY” theme is not a dark them (gruvbox-light I think). The theme is exported into a stylesheet via hugo gen chromastyles --style=gruvbox-light > assets/ananke/css/highlighting.css.

Is this A) a bug or B) some issue in my configuration? (repo is here)

We are on the latest Hugo version.

Yeah, this is known (at least to me) behavior, but we should document it here:
https://gohugo.io/content-management/syntax-highlighting/#lang

And perhaps some other places too (short code, highlight function). I’ll create a docs issue.

There’s a related proposal here:

I looked into this a while ago, and decided to bail but I don’t remember why.

For now, to meet your needs, I would create a codeblock render hook and set the lang to text if not specified.

And this may be useful:

https://discourse.gohugo.io/t/add-option-to-use-default-chroma-lexer-for-code-fences-with-no-language/55368/3

The render hook (layouts/_markup/render-codeblock.html) is trivial:

{{- $lang := "text" }}
{{- $options := .Options }}
{{- if transform.CanHighlight .Type }}
  {{- $lang = .Type }}
{{- else }}
  {{- $options = .Attributes }}
{{- end }}

{{- transform.Highlight .Inner $lang $options }}

Yeah, pushing it through a render hook might be a good idea.

I remember now. I decided to bail because, without a language specified in the info string, a fenced code block renders identically to an indented code block. Since you can’t specify a language with an indented block anyway, this is arguably the expected behavior.

Note that the approach I described above will not work with v0.162.0 and later.

Why?

Because we fixed the bug where syntax highlighting options were not in the Options map passed to the code block render hook when the code language (Type) was absent or unsupported. Instead, they were in the Attributes map. That is no longer the case.

What can I do instead?

You now have the ability to override the code language (Type) when calling the transform.HighlightCodeBlock function in a code block render hook. For example, to fall back to plain text when the language is not specified or not supported by the highlighter:

{{- $opts := dict }}
{{- if not (transform.CanHighlight .Type) }}
  {{- $opts = dict "type" "text" }}
{{- end }}
{{- $result := transform.HighlightCodeBlock . $opts }}
{{- $result.Wrapped }}

Let me know if you have any questions.