侧边栏
创建侧边栏可以:
- 分组多篇相关文档
- 在每篇文档旁显示侧边栏
- 提供下一篇/上一篇按钮的分页导航
要在你的 Docusaurus 网站上使用侧边栏,只需两步:
- 定义一个导出一组侧边栏对象的文件。
- 直接将此对象传入
@docusaurus/plugin-docs
或通过@docusaurus/preset-classic
传入。
docusaurus.config.js
module.exports = {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
sidebarPath: require.resolve('./sidebars.js'),
},
},
],
],
};
这个章节是文档侧边栏功能的一个总览。 在接下来的章节中,我们会系统地介绍下列概念:
📄️ 侧边栏项目
在上一章节中,我们介绍了三种项目类别:doc、category、link。它们的用途还是很显然的。 我们会正式介绍它们的 API。 还有第四种类型:autogenerated,我们稍后再详细解释。
📄️ 自动生成侧边栏
Docusaurus 可以根据你的文件系统结构自动生成侧边栏:每个文件夹会生成一个类别,每个文件会生成一个文档链接。
📄️ 使用多个侧边栏
你可以为每组想要分类到一起的 Markdown 文件创建一个侧边栏。
默认侧边栏
If the sidebarPath
is unspecified, Docusaurus automatically generates a sidebar for you, by using the filesystem structure of the docs
folder:
sidebars.js
module.exports = {
mySidebar: [
{
type: 'autogenerated',
dirName: '.', // 为 docs 目录(或者 versioned_docs/<version>)生成侧边栏
},
],
};
你也可以显式定义你的侧边栏。
侧边栏对象
侧边栏简单来说就是由一些类别、文档链接、其他超链接组成的层级结构。
type Sidebar =
// 普通语法
| SidebarItem[]
// 简写语法
| {[categoryLabel: string]: SidebarItem[]};
举个例子:
sidebars.js
module.exports = {
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
的侧边栏的文件。 它有三个顶层项目:两个类别和一个外部链接。 每个类别内部都有几个文档链接。
A sidebars file can contain multiple sidebar objects, identified by their object keys.
type SidebarsFile = {
[sidebarID: string]: Sidebar;
};
主题配置
可隐藏侧边栏
启用 themeConfig.docs.sidebar.hideable
选项后,你可以使整个侧边栏变得可隐藏,让用户能够更好地关注内容。 这对于中等屏幕大小(如平板)的读者来说格外有用。
docusaurus.config.js
module.exports = {
themeConfig: {
docs: {
sidebar: {
hideable: true,
},
},
},
};