2023-08-31 04:21:31 +08:00
|
|
|
|
---
|
2024-12-31 00:34:20 +00:00
|
|
|
|
title: "语法高亮"
|
2023-08-31 04:21:31 +08:00
|
|
|
|
weight: 3
|
|
|
|
|
---
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
Hugo 使用纯 Go 编写的通用语法高亮工具 [Chroma](https://github.com/alecthomas/chroma) 来实现代码高亮。建议在 Markdown 内容中使用反引号标记代码块,例如:
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
|
|
|
|
<!--more-->
|
|
|
|
|
|
|
|
|
|
````markdown {filename="Markdown"}
|
|
|
|
|
```python
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
将渲染为:
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
```
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
## 功能特性
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
### 文件名标注
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
通过设置 `filename` 属性可为代码块添加文件名或标题:
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
|
|
|
|
````markdown {filename="Markdown"}
|
|
|
|
|
```python {filename="hello.py"}
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
```python {filename="hello.py"}
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
```
|
|
|
|
|
|
2024-12-31 00:34:20 +00:00
|
|
|
|
### 文件链接
|
|
|
|
|
|
|
|
|
|
{{< new-feature version="v0.9.2" >}}
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
通过 `base_url` 属性可设置基础 URL,该 URL 会与文件名组合生成可点击的链接。文件名可包含相对路径以指定文件在基础路径中的位置。
|
2024-12-31 00:34:20 +00:00
|
|
|
|
|
|
|
|
|
````markdown {filename="Markdown"}
|
|
|
|
|
```go {base_url="https://github.com/imfing/hextra/blob/main/",filename="exampleSite/hugo.work"}
|
|
|
|
|
go 1.20
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
```go {base_url="https://github.com/imfing/hextra/blob/main/",filename="exampleSite/hugo.work"}
|
|
|
|
|
go 1.20
|
|
|
|
|
```
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
### 行号显示
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
设置 `linenos=table` 可启用行号,并通过 `linenostart` 指定起始行号:
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
|
|
|
|
````markdown {filename="Markdown"}
|
|
|
|
|
```python {linenos=table,linenostart=42}
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
```python {linenos=table,linenostart=42}
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
```
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
### 行高亮
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
通过 `hl_lines` 属性可高亮指定行号(支持数组格式):
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
|
|
|
|
````markdown {filename="Markdown"}
|
|
|
|
|
```python {linenos=table,hl_lines=[2,4],linenostart=1,filename="hello.py"}
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
say_hello()
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
```python {linenos=table,hl_lines=[2,4],linenostart=1,filename="hello.py"}
|
|
|
|
|
def say_hello():
|
|
|
|
|
print("Hello!")
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
say_hello()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 复制按钮
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
代码块默认启用复制功能,可通过站点配置文件修改其行为:
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
2024-12-31 00:34:20 +00:00
|
|
|
|
```yaml {linenos=table,linenostart=42,filename="hugo.yaml"}
|
|
|
|
|
params:
|
|
|
|
|
highlight:
|
|
|
|
|
copy:
|
|
|
|
|
enable: true
|
|
|
|
|
# hover | always
|
|
|
|
|
display: hover
|
|
|
|
|
```
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
## 支持语言
|
2023-08-31 04:21:31 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
完整支持的语言列表请参阅 [Chroma 文档](https://github.com/alecthomas/chroma#supported-languages)。
|