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;

代码组

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

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"
      }
    }
  }
}

热门主题包括:

  • GitHubgithub-darkgithub-lightgithub-dark-defaultgithub-light-default
  • 深色主题tokyo-nightone-dark-prodraculanordvitesse-dark
  • 浅色主题vitesse-lightmin-light
  • Catppuccincatppuccin-mochacatppuccin-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 字段