Skip to main content
Version: 3.7.0

侧边栏

创建侧边栏有助于:

  • 将多个相关文档分组
  • 在每个文档上显示侧边栏
  • 提供分页导航,带有下一页/上一页按钮

要在您的 Docusaurus 网站上使用侧边栏:

  1. 定义一个侧边栏文件,该文件导出一个侧边栏对象的字典。
  2. 将其路径直接传递给@docusaurus/plugin-docs插件或通过@docusaurus/preset-classic传递。
docusaurus.config.js
export default {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
sidebarPath: './sidebars.js',
},
},
],
],
};
Node.js runtime

侧边栏文件使用Node.js运行。你不能在其中使用或导入浏览器API、React或JSX。

本节概述了文档侧边栏的各种功能。在接下来的部分中,我们将更系统地介绍以下概念:

默认侧边栏

如果未指定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,
},
},
],
],
};

复杂侧边栏示例

来自Docusaurus网站的一个真实示例:

sidebars.js
import type {SidebarsConfig} from '@docusaurus/plugin-content-docs';
const sidebars: SidebarsConfig = {
docs: [
'introduction',
{
type: 'category',
label: 'Getting Started',
link: {
type: 'generated-index',
},
collapsed: false,
items: [
'installation',
'configuration',
'playground',
'typescript-support',
],
},
{
type: 'category',
label: 'Guides',
link: {
type: 'generated-index',
title: 'Docusaurus Guides',
description:
"Let's learn about the most important Docusaurus concepts!",
keywords: ['guides'],
image: '/img/docusaurus.png',
},
items: [
'guides/creating-pages',
{
type: 'category',
label: 'Docs',
link: {
type: 'doc',
id: 'guides/docs/introduction',
},
items: [
'guides/docs/create-doc',
{
type: 'category',
label: 'Sidebar',
link: {
type: 'doc',
id: 'guides/docs/sidebar/index',
},
items: [
'guides/docs/sidebar/items',
'guides/docs/sidebar/autogenerated',
'guides/docs/sidebar/multiple-sidebars',
],
},
'guides/docs/versioning',
'guides/docs/multi-instance',
],
},
'blog',
{
type: 'category',
label: 'Markdown Features',
link: {
type: 'doc',
id: 'guides/markdown-features/introduction',
},
items: [
'guides/markdown-features/react',
'guides/markdown-features/tabs',
'guides/markdown-features/code-blocks',
'guides/markdown-features/admonitions',
'guides/markdown-features/toc',
'guides/markdown-features/assets',
'guides/markdown-features/links',
'guides/markdown-features/plugins',
'guides/markdown-features/math-equations',
'guides/markdown-features/diagrams',
'guides/markdown-features/head-metadata',
],
},
'styling-layout',
'swizzling',
'static-assets',
'search',
'browser-support',
'seo',
'using-plugins',
'deployment',
{
type: 'category',
label: 'Internationalization',
link: {type: 'doc', id: 'i18n/introduction'},
items: [
{
type: 'doc',
id: 'i18n/tutorial',
label: 'Tutorial',
},
{
type: 'doc',
id: 'i18n/git',
label: 'Using Git',
},
{
type: 'doc',
id: 'i18n/crowdin',
label: 'Using Crowdin',
},
],
},
'guides/whats-next',
],
},
{
type: 'category',
label: 'Advanced Guides',
link: {type: 'doc', id: 'advanced/index'},
items: [
'advanced/architecture',
'advanced/plugins',
'advanced/routing',
'advanced/ssg',
'advanced/client',
],
},
{
type: 'category',
label: 'Upgrading',
link: {
type: 'doc',
id: 'migration/index',
},
items: [
'migration/v3',
{
type: 'category',
label: 'To Docusaurus v2',
items: [
'migration/v2/migration-overview',
'migration/v2/migration-automated',
'migration/v2/migration-manual',
'migration/v2/migration-versioned-sites',
'migration/v2/migration-translated-sites',
],
},
],
},
],
api: [
'cli',
'docusaurus-core',
{
type: 'autogenerated',
dirName: 'api',
},
],
};
export default sidebars;