Highlight code block in Hugo

Page content

Highlight codes in Hugo - It’s changed

Old Hugo

Around a year ago (2019), we need to configure pygments in order to highlight codes in code block. Here are sample lines in config.toml file.

...
pygmentsCodefences = true
pygmentsCodefencesGuessSyntax = true
pygmentsStyle = "monokai"
...

Latest Hugo (2020)

At the new version Hugo in 2020, Hugo officially supports code highlight as a default as a default.

Official syntax highlighting: https://gohugo.io/content-management/syntax-highlighting/#highlighting-in-code-fences

Release note about related function: https://gohugo.io/news/0.60.0-relnotes/

Since then, we don’t need to edit config.toml file to enable code highlight.

How to highlight

https://gohugo.io/content-management/syntax-highlighting/#highlighting-in-code-fences

Here is an example snippet for python code.

```
import numpy

print("This is test.\n")
```

The result,

import numpy

print("This is test.\n")