Autogenerated
Docusaurus can automatically create a sidebar from your filesystem structure. Each folder becomes a sidebar category, and each file becomes a document link.
type SidebarItemAutogenerated = {
type: 'autogenerated';
dirName: string; // Source folder to generate the sidebar slice from (relative to docs)
};
You can generate a full sidebar with just a single configuration:
export default {
myAutogeneratedSidebar: [
{
type: 'autogenerated',
dirName: '.', // '.' means the current docs folder
},
],
};
An autogenerated
item is converted to a sidebar slice,
which is a list of items (doc
or category
) that Docusaurus builds by scanning your folder.
You can splice multiple autogenerated
items from different directories and mix them with regular sidebar items.
Example
Consider this folder structure:
docs
βββ api
β βββ product1-api
β β βββ api.md
β βββ product2-api
β βββ basic-api.md
β βββ pro-api.md
βββ intro.md
βββ tutorials
βββ easy
β βββ easy1.md
β βββ easy2.md
βββ advanced
β βββ advanced1.md
β βββ advanced2.md
β βββ read-more
β βββ resource1.md
β βββ resource2.md
βββ tutorial-end.md
Using an autogenerated sidebar like this:
export default {
mySidebar: [
'intro',
{
type: 'category',
label: 'Tutorials',
items: [
{
type: 'autogenerated',
dirName: 'tutorials/easy',
},
{
type: 'autogenerated',
dirName: 'tutorials/advanced',
},
'tutorial-end',
],
},
{
type: 'autogenerated',
dirName: 'api',
},
],
};
Will automatically expand into nested categories and docs, matching the file/folder structureβno manual sidebar maintenance required.
Category Index Conventionβ
Docusaurus can automatically link a category to an index document if it follows one of these patterns:
index.md
(case-insensitive)README.md
(case-insensitive)- A file with the same name as the folder
For example, docs/Guides/index.md
will be linked automatically.
export default {
docs: [
{
type: 'category',
label: 'Guides',
link: { type: 'doc', id: 'Guides/index' },
items: [],
},
],
};
If a folder contains only an index file, Docusaurus converts it into a single doc link instead of a collapsible category.
Sidebar Metadataβ
When using autogenerated sidebars, Docusaurus reads sidebar metadata directly from the docβs front matter.
Doc Metadataβ
Inside your markdown files, use:
---
sidebar_position: 2
sidebar_label: Easy Tutorial
sidebar_class_name: highlight
---
sidebar_position
β controls orderingsidebar_label
β custom labelsidebar_class_name
β custom CSS class
Category Metadataβ
Create a _category_.json
or _category_.yml
in a folder:
{
"position": 2.5,
"label": "Tutorials",
"collapsible": true,
"collapsed": false,
"className": "custom-class",
"link": {
"type": "generated-index",
"title": "Tutorials Overview"
}
}
Ordering with Number Prefixesβ
A quick way to control ordering is to use number prefixes:
docs
βββ 01-Intro.md
βββ 02-Getting-Started.md
βββ 03-Advanced.md
Docusaurus removes the numbers from URLs and labels by default.
Custom Sidebar Generatorβ
For full control, you can override the sidebar generation:
export default {
plugins: [
[
'@docusaurus/plugin-content-docs',
{
async sidebarItemsGenerator({ defaultSidebarItemsGenerator, ...args }) {
const items = await defaultSidebarItemsGenerator(args);
// Example: reverse order
return items.reverse();
},
},
],
],
};