侧边栏
创建侧边栏有助于
- 将多个相关文档分组
- 在每个文档上显示侧边栏
- 提供分页导航,带有上一页/下一页按钮
要在您的 Docusaurus 站点上使用侧边栏
- 定义一个侧边栏文件,该文件导出一个侧边栏对象字典。
- 将其路径直接或通过
@docusaurus/preset-classic
传递给@docusaurus/plugin-docs
插件。
export default {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
sidebarPath: './sidebars.js',
},
},
],
],
};
侧边栏文件在 Node.js 中运行。您不能在其中使用或导入浏览器 API、React 或 JSX。
本节概述了文档侧边栏的各种杂项功能。在以下部分中,我们将更系统地介绍以下概念:
📄️ 侧边栏项目
在上一节“自动生成”的示例中,我们介绍了三种项目类型,我们将在后面详细解释。
📄️ 自动生成
Docusaurus 可以根据您的文件系统结构自动创建侧边栏:每个文件夹创建一个侧边栏类别,每个文件创建一个文档链接。
📄️ 使用多个侧边栏
您可以为每组希望分组在一起的 Markdown 文件创建一个侧边栏。
默认侧边栏
如果未指定sidebarPath
,Docusaurus 会通过使用docs
文件夹的文件系统结构自动为您生成一个侧边栏。
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[]};
例如
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
选项,您可以使整个侧边栏可隐藏,让用户更好地专注于内容。这在内容在中等大小屏幕(例如平板电脑)上消费时特别有用。
export default {
themeConfig: {
docs: {
sidebar: {
hideable: true,
},
},
},
};
自动折叠侧边栏类别
themeConfig.docs.sidebar.autoCollapseCategories
选项会在展开一个类别时折叠所有同级类别。这可以避免用户打开过多的类别,并帮助他们专注于所选部分。
export default {
themeConfig: {
docs: {
sidebar: {
autoCollapseCategories: true,
},
},
},
};
传递自定义属性
要将自定义属性传递给侧边栏项目,请将可选的customProps
对象添加到任何项目。这对于通过自定义覆盖渲染侧边栏项目的 React 组件来应用站点自定义非常有用。
{
type: 'doc',
id: 'doc1',
customProps: {
badges: ['new', 'green'],
featured: true,
},
};
侧边栏面包屑
默认情况下,面包屑显示在顶部,使用当前页面的“侧边栏路径”。
可以通过插件选项禁用此行为
export default {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
breadcrumbs: false,
},
},
],
],
};
复杂侧边栏示例
来自 Docusaurus 站点的一个真实示例
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;