2、侧边栏
侧边栏
侧边栏
创建侧边栏有助于:
- 将多个相关文档分组
- 在每个文档上显示侧边栏
- 提供分页导航,带有下一个/上一个按钮
要在 Docusaurus 网站上使用侧边栏:
- 定义一个文件,导出 侧边栏 对象 的字典。
- 直接或通过
@docusaurus/preset-classic
将此对象传递到@docusaurus/plugin-docs
插件中。
docusaurus.config.js
export default {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
sidebarPath: './sidebars.js',
},
},
],
],
};
本节概述了文档侧边栏的各种功能。在接下来的章节中,我们将更加系统地介绍以下概念:
默认侧边栏
如果未指定 sidebarPath
,Docusaurus 自动生成侧边栏 会为你提供,通过使用 docs
文件夹的文件系统结构:
sidebars.js
export default {
mySidebar: [
{
type: 'autogenerated',
dirName: '.', // generate sidebar from the docs folder (or versioned_docs/<version>)
},
],
};
你还可以明确定义侧边栏。
侧边栏对象
侧边栏的关键是类别、文档链接和其他超链接的层次结构。
type Sidebar =
// Normal syntax
| SidebarItem[]
// Shorthand syntax
| {[categoryLabel: string]: SidebarItem[]};
例如:
sidebars.js
export default {
mySidebar: [
{
type: 'category',
label: 'Getting Started',
items: [
{
type: 'doc',
id: 'doc1',
},
],
},
{
type: 'category',
label: 'Docusaurus',
items: [
{
type: 'doc',
id: 'doc2',
},
{
type: 'doc',
id: 'doc3',
},
],
},
{
type: 'link',
label: 'Learn more',
href: 'https://example.com',
},
],
};
这是一个侧边栏文件,导出一个侧边栏,名为 mySidebar
。它具有三个顶层项目:两个类别和一个外部链接。每个类别中都有一些文档链 接。
侧边栏文件可以包含 多个侧边栏对象,由其对象键标识。
type SidebarsFile = {
[sidebarID: string]: Sidebar;
};
主题配置
可隐藏侧边栏
通过启用 themeConfig.docs.sidebar.hideable
选项,你可以使整个侧边栏可隐藏,让用户更好地关注内容。当在中型屏幕(例如平板电脑)上消费内容时,这尤其有用。
docusaurus.config.js
export default {
themeConfig: {
docs: {
sidebar: {
hideable: true,
},
},
},
};
自动折叠侧边栏类别
themeConfig.docs.sidebar.autoCollapseCategories
选项将在展开一个类别时折叠所有同级类别。这可以避免用户打开太多类别,并帮助他们专注于所选部分。
docusaurus.config.js
export default {
themeConfig: {
docs: {
sidebar: {
autoCollapseCategories: true,
},
},
},
};
传递自定义属性
要将自定义属性传递到侧边栏项目,请将可选的 customProps
对象添加到任何项目。这对于通过混合 React 组件渲染侧边栏项目来应用站点自定义非常有用。
{
type: 'doc',
id: 'doc1',
customProps: {
badges: ['new', 'green'],
featured: true,
},
};
侧边栏面包屑
默认情况下,面包屑使用当前页面的 "侧边栏路径" 渲染在顶部。
可以使用插件选项禁用此行为:
docusaurus.config.js
export default {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
breadcrumbs: false,
},
},
],
],
};