Jamdesk Documentation logo

代码块

添加带语法高亮、行号、文件名和行高亮的代码块,并使用 CodeGroup 展示多语言示例。

使用代码块展示命令、配置文件和带语法高亮的代码片段。

基本语法高亮

const greeting = "Hello, Jamdesk!";
console.log(greeting);

添加文件名

使用 title 属性显示文件路径:

src/config.ts
export const config = {
  name: "My Docs",
  theme: "jam"
};

行高亮

使用 {1,3-5} 语法突出显示特定行:

// This line is highlighted
const config = {
  apiKey: process.env.API_KEY,
  baseUrl: 'https://api.example.com', // highlighted
  timeout: 5000,                       // highlighted
  retries: 3,                          // highlighted
};

行号

使用 showLineNumbers 添加行号:

def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

自定义起始行

从指定行开始编号:

func processRequest(ctx context.Context) error {
    // This is line 42
    return nil
}

组合功能

同时使用多个功能:

api/client.ts showLineNumbers {3-5}
import axios from 'axios';

const client = axios.create({
  baseURL: process.env.API_URL,
  timeout: 10000,
});

export default client;

隐藏复制按钮

添加 nocopy 即可移除单个代码块的复制按钮:

$ jamdesk build
Building 128 pages...
Done in 4.2s

只要复制这个代码块会让读者拿到无法直接使用的内容,就可以用它:比如提示符和输出混在一起的终端记录、经过脱敏处理的示例,或者“不要这样做”的片段。

nocopy 可以与其他所有选项组合使用:

broken.js
const config = loadConfig();
config.apiKey = "sk-live-REDACTED";

代码组

以多种语言展示相同的代码:

curl -X POST https://api.example.com/posts \
  -H "Authorization: Bearer $API_KEY"

可用主题

在 docs.json 中配置语法高亮主题:

docs.json
{
  "styling": {
    "codeblocks": {
      "theme": {
        "light": "github-light",
        "dark": "github-dark"
      }
    }
  }
}

热门主题包括:

  • GitHub:github-dark、github-light、github-dark-default、github-light-default
  • 深色主题:tokyo-night、one-dark-pro、dracula、nord、vitesse-dark
  • 浅色主题:vitesse-light、min-light
  • Catppuccin:catppuccin-mocha、catppuccin-latte

CSS 变量主题

如需完全控制语法高亮颜色,请使用 CSS 变量主题:

docs.json
{
  "styling": {
    "codeblocks": {
      "theme": "css-variables"
    }
  }
}

然后在 style.css 中自定义颜色:

style.css
:root {
  --jd-token-keyword: #ff7b72;
  --jd-token-string: #a5d6ff;
  --jd-token-comment: #8b949e;
  --jd-token-function: #d2a8ff;
  --jd-token-constant: #79c0ff;
  --jd-token-parameter: #ffa657;
}
变量说明
--jd-color-text默认文本颜色
--jd-color-background代码块背景
--jd-token-keyword关键字(if、const、function)
--jd-token-string字符串字面量
--jd-token-comment注释
--jd-token-function函数名称
--jd-token-constant常量
--jd-token-parameter函数参数
--jd-token-punctuation括号、逗号
--jd-token-linkURL 和链接

接下来做什么?

SEO 优化

提升内容发现率并优化元数据

Frontmatter

定义标题、描述和 SEO 字段