侧边栏
创建侧边栏可以:
- Group multiple related documents
- Display a sidebar on each of those documents
- Provide paginated navigation, with next/previous button
要在你的 Docusaurus 网站上使用侧边栏,只需两步:
- Define a file that exports a dictionary of sidebar objects.
- Pass this object into the
@docusaurus/plugin-docs
plugin directly or via@docusaurus/preset-classic
.
export default {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
sidebarPath: './sidebars.js',
},
},
],
],
};
这个章节是文档侧边栏功能的一个总览。 在接下来的章节中,我们会系统地介绍下列概念:
📄️ 侧边栏项目
We have introduced three types of item types in the example in the previous section autogenerated, which we will explain in detail later.
📄️ 自动生成侧边栏
Docusaurus can create a sidebar automatically from your filesystem structure: each folder creates a sidebar category, and each file creates a doc link.
📄️ 使用多个侧边栏
You can create a sidebar for each set of Markdown files that you want to group together.
Default sidebar
If the sidebarPath
is unspecified, Docusaurus automatically generates a sidebar for you, by using the filesystem structure of the docs
folder:
export default {
mySidebar: [
{
type: 'autogenerated',
dirName: '.', // generate sidebar from the docs folder (or versioned_docs/<version>)
},
],
};
你也可以显式定义你的侧边栏。
Sidebar object
侧边栏 简单来说就是由一些类别、文档链接、其他超链接组成的层级结构。
type Sidebar =
// 普通语法
| SidebarItem[]
// 简写语法
| {[categoryLabel: string]: SidebarItem[]};
举个例子:
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',
},
],
};
This is a sidebars file that exports one sidebar, called mySidebar
. 它有三个顶层项目:两个类别和一个外部链接。 每个类别内部都有几个文档链接。
A sidebars file can contain multiple sidebar objects, identified by their object keys.
type SidebarsFile = {
[sidebarID: string]: Sidebar;
};
Theme configuration
Hideable sidebar
By enabling the themeConfig.docs.sidebar.hideable
option, you can make the entire sidebar hideable, allowing users to better focus on the content. 这对于中等屏幕大小(如平板)的读者来说格外有用。
export default {
themeConfig: {
docs: {
sidebar: {
hideable: true,
},
},
},
};