跳转至

代码块

DocsForge 支持丰富的代码块,包括语法高亮、标题、行号等功能。

基础代码块

使用带有语言标识符的三个反引号:

``` python
def hello(name):
    print(f"Hello, {name}!")
```
def hello(name):
    print(f"Hello, {name}!")

支持的语言

DocsForge 通过 Pygments 支持 300 多种语言,包括:

  • python, py
  • javascript, js
  • typescript, ts
  • bash, sh, shell
  • yaml, yml
  • json
  • html
  • css
  • markdown, md
  • dockerfile
  • sql
  • rust
  • go
  • java
  • c, cpp
  • csharp, cs
  • ruby, rb
  • php
  • lua
  • r
  • julia
  • kotlin
  • swift
  • dart

带标题的代码块

``` python title="hello.py"
def hello(name):
    print(f"Hello, {name}!")
```
hello.py
def hello(name):
    print(f"Hello, {name}!")

行号

``` python linenums="1"
def hello(name):
    print(f"Hello, {name}!")
    return True
```
1
2
3
def hello(name):
    print(f"Hello, {name}!")
    return True

高亮行

``` python hl_lines="2 3"
def hello(name):
    message = f"Hello, {name}!"
    print(message)
    return message
```
def hello(name):
    message = f"Hello, {name}!"
    print(message)
    return message

Diff 代码块

显示新增和删除:

``` diff
  def hello(name):
-     print("Hello!")
+     print(f"Hello, {name}!")
      return True
```
  def hello(name):
-     print("Hello!")
+     print(f"Hello, {name}!")
      return True

控制台代码块

显示命令输出:

``` console
$ docsforge build
INFO    -  Cleaning site directory
INFO    -  Building documentation to directory: site
INFO    -  Documentation built in 2.34 seconds
```
$ docsforge build
INFO    -  Cleaning site directory
INFO    -  Building documentation to directory: site
INFO    -  Documentation built in 2.34 seconds

无复制按钮

禁用特定代码块的复制按钮:

``` python
```

或在配置中全局禁用:

theme:
  features:
    # 不要包含 - content.code.copy

下一步