Syntax Highlighting

Syntax Highlighting

Hugo uses Chroma, a general purpose syntax highlighter in pure Go, for syntax highlighting. It is recommended to use backticks for code blocks in Markdown content. For example:

Markdown
```python
def say_hello():
    print("Hello!")
```

will be rendered as:

def say_hello():
    print("Hello!")

Features

Filename

To add a filename or title to the code block, set attribute filename:

Markdown
```python {filename="hello.py"}
def say_hello():
    print("Hello!")
```
hello.py
def say_hello():
    print("Hello!")

Line Numbers

To set line numbers, set attribute linenos to table and optionally set linenostart to the starting line number:

Markdown
```python {linenos=table,linenostart=42}
def say_hello():
    print("Hello!")
```
42
43
def say_hello():
    print("Hello!")

Highlighting Lines

To highlight lines, set attribute hl_lines to a list of line numbers:

Markdown
```python {linenos=table,hl_lines=[2,4],linenostart=1,filename="hello.py"}
def say_hello():
    print("Hello!")

def main():
    say_hello()
```
hello.py
1
2
3
4
5
def say_hello():
    print("Hello!")

def main():
    say_hello()

Copy Button

By default, copy button is enabled for code blocks. Its behavior can changed by modifying the site configuration file:

hugo.yaml
42
43
44
45
46
47
params:
  highlight:
    copy:
      enable: true
      # hover | always
      display: hover

Supported Languages

For a list of supported languages, please see the Chroma documentation.

Last updated on