Skip to content

📑 自动侧边栏

注意

每次更改目录结构,新增/删除/改名之后需要在终端按 r 建重启服务才可生效

1.首先通过pnpm 安装

sh
pnpm add -D vitepress-sidebar

2.在 configs.mts 中添加以下代码

configs.mts
ts
// .vitepress/configs.mts
import { generateSidebar } from 'vitepress-sidebar';

const vitepressSidebarOptions = {
  /* Options... */
};

export default defineConfig({
  themeConfig: {
    sidebar: generateSidebar({
      /*
       * For detailed instructions, see the links below:
       * https://vitepress-sidebar.jooy2.com/guide/api
       */
      documentRootPath: '/docs', //文档根目录
      // scanStartPath: null,   //扫描开始路径
      // resolvePath: null,    //解析路径
      // useTitleFromFileHeading: true, //是否使用文件标题做分级标题
      // useTitleFromFrontmatter: true, //是否使用文件标题做分级标题
      // frontmatterTitleFieldName: 'title',    //frontmatter标题字段名
      // useFolderTitleFromIndexFile: false, //是否使用层级首页文件名做分级标题
      // useFolderLinkFromIndexFile: false, //是否链接至层级首页文件
      // hyphenToSpace: true,   //是否将连字符转换为空格
      // underscoreToSpace: true,   //是否将下划线转换为空格
      // capitalizeFirst: false,    //是否将标题首字母大写
      // capitalizeEachWords: false,    //是否将标题每个单词首字母大写
      collapsed: false, //折叠组关闭
      collapseDepth: 2, //折叠组2级菜单
      // sortMenusByName: false,    //是否按名称排序
      // sortMenusByFrontmatterOrder: false,    //是否按frontmatter排序
      // sortMenusByFrontmatterDate: false,    //是否按frontmatter日期排序
      // sortMenusOrderByDescending: false, //是否按名称排序
      // sortMenusOrderNumericallyFromTitle: false, //是否按名称排序
      // sortMenusOrderNumericallyFromLink: false,  //是否按名称排序
      // frontmatterOrderDefaultValue: 0,   //frontmatter排序默认值
      // manualSortFileNameByPriority: ['first.md', 'second', 'third.md'], //手动排序,文件夹不用带后缀
      removePrefixAfterOrdering: false, //删除前缀,必须与prefixSeparator一起使用
      prefixSeparator: '.', //删除前缀的符号
      // excludeFiles: ['first.md', 'secret.md'],   //排除文件
      // excludeFilesByFrontmatterFieldName: 'exclude', 
      // excludeFolders: ['secret-folder'], //排除文件夹
      // includeDotFiles: false,    //是否包含点文件
      // includeRootIndexFile: false,   //是否包含根主页
      // includeFolderIndexFile: false, //是否包含层级主页
      // includeEmptyFolder: false, //是否包含空文件夹
      // rootGroupText: 'Contents', //根分组文本
      // rootGroupLink: 'https://github.com/jooy2', //根分组链接
      // rootGroupCollapsed: false, //根分组折叠
      // convertSameNameSubFileToGroupIndexPage: false, //是否将同名子文件转换为分组索引页
      // folderLinkNotIncludesFileName: false,  //是否将文件夹链接不包括文件名
      // keepMarkdownSyntaxFromTitle: false,    //是否保留标题中的Markdown语法
      // debugPrint: false,
    }),
  },
})