Files
Hugo/themes/hextra/docs/content/docs/guide/syntax-highlighting.md
T
clawandfukunee 07851043b1 chore: 初始化 Hugo 博客项目
Hextra 主题,8 篇文章,支持全文搜索/暗色模式
2026-05-13 12:48:53 +08:00

2.4 KiB

title, weight
title weight
Syntax Highlighting 3

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:

```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:

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

{{< new-feature version="v0.9.2" >}}

You can use the base_url attribute to provide a base URL that will be combined with the file name to generate a link.

The file name can include a relative path if it specifies the file's location within the base path.

```go {base_url="https://github.com/imfing/hextra/blob/main/",filename="docs/hugo.work"}
go 1.20
```
go 1.20

Line Numbers

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

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

Highlighting Lines

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

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

def main():
    say_hello()
```
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:

params:
  highlight:
    copy:
      enable: true
      # hover | always
      display: hover

Supported Languages

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