侧边栏项目
We have introduced three types of item types in the example in the previous section: doc
, category
, and link
, whose usages are fairly intuitive. 我们会正式介绍它们的 API。 There's also a fourth type: autogenerated
, which we will explain in detail later.
- Doc: link to a doc page, associating it with the sidebar
- Link: link to any internal or external page
- Category: creates a dropdown of sidebar items
- Autogenerated: generate a sidebar slice automatically
- HTML: renders pure HTML in the item's position
- *Ref: link to a doc page, without making the item take part in navigation generation
Doc: link to a doc
Use the doc
type to link to a doc page and assign that doc to a sidebar:
type SidebarItemDoc =
// Normal syntax
| {
type: 'doc';
id: string;
label: string; // Sidebar label text
className?: string; // Class name for sidebar label
customProps?: Record<string, unknown>; // Custom props
}
// Shorthand syntax
| string; // docId shortcut
示例:
module.exports = {
mySidebar: [
// 普通语法:
{
type: 'doc',
id: 'doc1', // 文档 ID
label: 'Getting started', // 侧边栏标签
},
// 简写语法:
'doc2', // 文档 ID
],
};
If you use the doc shorthand or autogenerated sidebar, you would lose the ability to customize the sidebar label through item definition. You can, however, use the sidebar_label
Markdown front matter within that doc, which has higher precedence over the label
key in the sidebar item. Similarly, you can use sidebar_custom_props
to declare custom metadata for a doc page.
A doc
item sets an implicit sidebar association. Don't assign the same doc to multiple sidebars: change the type to ref
instead.
如果想要向客户端传送任意文档元数据,侧边栏自定义属性是个不错的方式,这样你就可以在使用钩子函数时,在返回的文档对象上获得额外的信息了。
Link: link to any page
Use the link
type to link to any page (internal or external) that is not a doc.
type SidebarItemLink = {
type: 'link';
label: string;
href: string;
className?: string;
description?: string;
};
示例:
module.exports = {
myLinksSidebar: [
// 外部链接
{
type: 'link',
label: 'Facebook', // 链接标签
href: 'https://facebook.com', // 外部 URL
},
// 内部链接
{
type: 'link',
label: 'Home', // 链接标签
href: '/', // 内部路径
},
],
};
HTML: render custom markup
Use the html
type to render custom HTML within the item's <li>
tag.
适用于插入自定义项目,比如分割线、节标题、广告、图片。
type SidebarItemHtml = {
type: 'html';
value: string;
defaultStyle?: boolean; // 使用默认的菜单项目样式
className?: string;
};
示例:
module.exports = {
myHtmlSidebar: [
{
type: 'html',
value: '<img src="sponsor.png" alt="Sponsor" />', // The HTML to be rendered
defaultStyle: true, // Use the default menu item styling
},
],
};
The menu item is already wrapped in an <li>
tag, so if your custom item is simple, such as a title, just supply a string as the value and use the className
property to style it:
module.exports = {
myHtmlSidebar: [
{
type: 'html',
value: '核心概念',
className: 'sidebar-title',
},
],
};
Category: create a hierarchy
Use the category
type to create a hierarchy of sidebar items.
type SidebarItemCategory = {
type: 'category';
label: string; // Sidebar label text.
items: SidebarItem[]; // Array of sidebar items.
className?: string;
description?: string;
// Category options:
collapsible: boolean; // Set the category to be collapsible
collapsed: boolean; // Set the category to be initially collapsed or open by default
link: SidebarItemCategoryLinkDoc | SidebarItemCategoryLinkGeneratedIndex;
};
示例:
module.exports = {
docs: [
{
type: 'category',
label: 'Guides',
collapsible: true,
collapsed: false,
items: [
'creating-pages',
{
type: 'category',
label: 'Docs',
items: ['introduction', 'sidebar', 'markdown-features', 'versioning'],
},
],
},
],
};
Use the shorthand syntax when you don't need customizations:
module.exports = {
docs: {
Guides: [
'creating-pages',
{
Docs: ['introduction', 'sidebar', 'markdown-features', 'versioning'],
},
],
},
};
Category links
使用类别链接,让点击类别标签可以跳转到另一个页面。
用类别链接来做一组文档的引言。
Autogenerated categories can use the _category_.yml
file to declare the link.
Generated index page
你可以自动生成一个索引页,显示此类别的所有直接子项。 The slug
allows you to customize the generated page's route, which defaults to /category/[categoryName]
.
module.exports = {
docs: [
{
type: 'category',
label: '教程',
link: {
type: 'generated-index',
title: 'Docusaurus 教程',
description: '学习最重要的 Docusaurus 概念!',
slug: '/category/docusaurus-guides',
keywords: ['guides'],
image: '/img/docusaurus.png',
},
items: ['pages', 'docs', 'blog', 'search'],
},
],
};
See it in action on the Docusaurus Guides page.
Use generated-index
links as a quick way to get an introductory document.
Doc link
类别链接可以指向现有的文档。
module.exports = {
docs: [
{
type: 'category',
label: '教程',
link: {type: 'doc', id: 'introduction'},
items: ['pages', 'docs', 'blog', 'search'],
},
],
};
See it in action on the i18n introduction page.