Skip to content

侧边栏插件配置

本模板使用了 vitepress-sidebar 插件来自动生成侧边栏,避免了手动维护 config.mts 中庞大的侧边栏数组。

核心配置

你可以在 docs/.vitepress/config.mts 中找到 sidebarOptions 配置项:

typescript
const sidebarOptions = {
  // 文档根目录(对应 docs/.vitepress 所在路径)
  documentRootPath: '/docs',
  // 侧边栏分组可折叠
  collapsed: false,
  // 二级及更深层级默认折叠
  collapseDepth: 2,
  // 优先读取 frontmatter 的 title
  useTitleFromFrontmatter: true,
  // frontmatter 没有 title 时,取一级标题
  useTitleFromFileHeading: true,
  // 文件夹标题从 index.md 读取
  useFolderTitleFromIndexFile: true,
  // 文件夹链接指向 index.md
  useFolderLinkFromIndexFile: true,
  // 根目录的 index.md 不显示在侧边栏
  includeRootIndexFile: false,
  // 按链接中的数字顺序排序(适合 01-xxx 之类命名)
  sortMenusOrderNumericallyFromLink: true,
  // 排除系统文件
  excludeByGlobPattern: ['**/.DS_Store'],
};

常见操作

1. 调整排序

插件默认按照文件系统顺序排序。如果你想控制顺序,可以:

  • 使用数字前缀,如 01-introduction.md, 02-setup.md
  • 插件配置了 sortMenusOrderNumericallyFromLink: true,会自动识别并隐藏这些数字前缀。

2. 修改菜单名称

  • 推荐在 Markdown 文件的 frontmatter 中设置 title:
    markdown
    ---
    title: 我的自定义标题
    ---
  • 插件会优先读取这个 title 作为侧边栏显示的名称。

3. 分组显示

侧边栏会根据文件夹结构自动生成分组。例如 docs/guide/ 目录下的文件会被自动归类到“指南”分组中(如果 guide/index.md 存在,则分组名取自 index.md 的标题)。

更多参考

了解更多高级配置,请访问 vitepress-sidebar 官方文档