VitePress SideBar 插件
安装
# via npm
npm i -D vitepress-sidebar
# via yarn
yarn add -D vitepress-sidebar
# via pnpm
pnpm i -D vitepress-sidebar使用
VitePress Sidebar 可通过两个函数自动生成侧边栏:withSidebar(**推荐**) 和 generateSidebar , 这两个函数的使用时机不同
withSidebar
withSidebar 用于 defineConfig 级别。请注意,VitePress 的配置对象应放在第一个参数中,VitePress 侧边栏的选项应放在第二个参数中。
VitePress 侧边栏将覆盖 VitePress 中现有选项所需的任何附加选项。 已设置的任何 sidebar 选项都将被新选项覆盖。
// `.vitepress/config.js`
import { withSidebar } from 'vitepress-sidebar';
const vitePressOptions = {
// VitePress's options here...
title: 'VitePress Sidebar',
themeConfig: {
// ...
}
};
const vitePressSidebarOptions = {
// VitePress Sidebar's options here...
documentRootPath: '/',
collapsed: false,
capitalizeFirst: true
};
export default defineConfig(withSidebar(vitePressOptions, vitePressSidebarOptions));generateSidebar
generateSidebar 在themeConfig.sidebar级别可用。当需要对更详细的 themeConfig 设置进行代码分离时,可以使用此功能。
// `.vitepress/config.js`
import { generateSidebar } from 'vitepress-sidebar';
export default defineConfig({
themeConfig: {
sidebar: generateSidebar({
// VitePress Sidebar's options here...
})
}
});注意documentRootPath
VitePress Sidebar需要通过 documentRootPath 选项指定工作路径来了解正确的位置。默认是/,但如果VitePress项目位于一个单独的文件夹中,如docs,根据你的项目,你将需要自己指定路径。
/
├─ package.json
├─ src/
├─ docs/ <--------------- `documentRootPath` ('/docs')
│ ├─ .vitepress/
│ ├─ another-directory/
│ ├─ hello.md
│ └─ index.md
└─ ...// `.vitepress/config.js`
import { withSidebar } from 'vitepress-sidebar';
const vitePressOptions = {};
const vitePressSidebarOptions = {
documentRootPath: '/docs'
};
export default defineConfig(withSidebar(vitePressOptions, vitePressSidebarOptions));要测试侧边栏结果如何打印,请在构建 VitePress 时将 debugPrint 选项设置为 true。你应该能在控制台中看到输出结果。
配置示例
import { withSidebar } from 'vitepress-sidebar';
const vitePressConfigs = {
title: 'VitePress Sidebar',
themeConfig: {
// ...
}
};
export default defineConfig(
withSidebar(vitePressConfigs, {
/*
* 有关详细说明,请参阅下面的链接:
* https://vitepress-sidebar.cdget.com/zhHans/guide/options
*/
//
// ============ [ RESOLVING PATHS ] ============
// documentRootPath: '/',
// scanStartPath: null,
// resolvePath: null,
// basePath: null,
// followSymlinks: false,
//
// ============ [ GROUPING ] ============
// collapsed: true,
// collapseDepth: 2,
// rootGroupText: 'Contents',
// rootGroupLink: 'https://github.com/jooy2',
// rootGroupCollapsed: false,
//
// ============ [ GETTING MENU TITLE ] ============
// useTitleFromFileHeading: true,
// useTitleFromFrontmatter: true,
// useFolderLinkFromIndexFile: false,
// useFolderTitleFromIndexFile: false,
// frontmatterTitleFieldName: 'title',
//
// ============ [ GETTING MENU LINK ] ============
// useFolderLinkFromSameNameSubFile: false,
// useFolderLinkFromIndexFile: false,
// folderLinkNotIncludesFileName: false,
//
// ============ [ INCLUDE / EXCLUDE ] ============
// excludePattern: ['README.md', 'folder/'],
// excludeFilesByFrontmatterFieldName: 'exclude',
// includeDotFiles: false,
// includeEmptyFolder: false,
// includeRootIndexFile: false,
// includeFolderIndexFile: false,
//
// ============ [ STYLING MENU TITLE ] ============
// hyphenToSpace: true,
// underscoreToSpace: true,
// capitalizeFirst: false,
// capitalizeEachWords: false,
// keepMarkdownSyntaxFromTitle: false,
// removePrefixAfterOrdering: false,
// prefixSeparator: '.',
//
// ============ [ SORTING ] ============
// manualSortFileNameByPriority: ['first.md', 'second', 'third.md'],
// sortFolderTo: null,
// sortMenusByName: false,
// sortMenusByFileDatePrefix: false,
// sortMenusByFrontmatterOrder: false,
// frontmatterOrderDefaultValue: 0,
// sortMenusByFrontmatterDate: false,
// sortMenusOrderByDescending: false,
// sortMenusOrderNumericallyFromTitle: false,
// sortMenusOrderNumericallyFromLink: false,
//
// ============ [ MISC ] ============
// debugPrint: false,
})
);这里是对应的文档:
VitePress SideBar 插件
https://halo.jiangling.site/archives/vitepress-sidebar-cha-jian