Autogenerated
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';Docusaurus can create a sidebar automatically from your filesystem structure: each folder creates a sidebar category, and each file creates a doc link.
type SidebarItemAutogenerated = {
type: 'autogenerated';
dirName: string; // Source folder to generate the sidebar slice from (relative to docs)
};Docusaurus can generate a full sidebar from your docs folder:
export default { myAutogeneratedSidebar: [ { type: 'autogenerated', dirName: '.', // '.' means the current docs folder }, ],};An autogenerated item is converted by Docusaurus to a sidebar slice (also discussed in category shorthands): a list of items of type doc or category, so you can splice multiple autogenerated items from multiple directories, interleaving them with regular sidebar items, in one sidebar level.
A real-world example
Consider this file structure:
docs
βββ api
β βββ product1-api
β β βββ api.md
β βββ product2-api
β βββ basic-api.md
β βββ pro-api.md
βββ intro.md
βββ tutorials
βββ advanced
β βββ advanced1.md
β βββ advanced2.md
β βββ read-more
β βββ resource1.md
β βββ resource2.md
βββ easy
β βββ easy1.md
β βββ easy2.md
βββ tutorial-end.md
βββ tutorial-intro.md
βββ tutorial-medium.mdAnd assume every doc's ID is just its file name. If you define an autogenerated sidebar like this:
export default { mySidebar: [ 'intro', { type: 'category', label: 'Tutorials', items: [ 'tutorial-intro', { type: 'autogenerated', dirName: 'tutorials/easy', // Generate sidebar slice from docs/tutorials/easy }, 'tutorial-medium', { type: 'autogenerated', dirName: 'tutorials/advanced', // Generate sidebar slice from docs/tutorials/advanced }, 'tutorial-end', ], }, { type: 'autogenerated', dirName: 'api', // Generate sidebar slice from docs/api }, { type: 'category', label: 'Community', items: ['team', 'chat'], }, ],};It would be resolved as:
export default { mySidebar: [ 'intro', { type: 'category', label: 'Tutorials', items: [ 'tutorial-intro', // Two files in docs/tutorials/easy 'easy1', 'easy2', 'tutorial-medium', // Two files and a folder in docs/tutorials/advanced 'advanced1', 'advanced2', { type: 'category', label: 'read-more', items: ['resource1', 'resource2'], }, 'tutorial-end', ], }, // Two folders in docs/api { type: 'category', label: 'product1-api', items: ['api'], }, { type: 'category', label: 'product2-api', items: ['basic-api', 'pro-api'], }, { type: 'category', label: 'Community', items: ['team', 'chat'], }, ],};Note how the autogenerate source directories themselves don't become categories: only the items they contain do. This is what we mean by "sidebar slice".
Category index convention
Section titled βCategory index conventionβDocusaurus can automatically link a category to its index document.
A category index document is a document following one of those filename conventions:
- Named as
index(case-insensitive):docs/Guides/index.md - Named as
README(case-insensitive):docs/Guides/README.mdx - Same name as parent folder:
docs/Guides/Guides.md
This is equivalent to using a category with a doc link:
export default { docs: [ { type: 'category', label: 'Guides', link: {type: 'doc', id: 'Guides/index'}, items: [], }, ],};Customizing category index matching
It is possible to opt out any of the category index conventions, or define even more conventions. You can inject your own isCategoryIndex matcher through the sidebarItemsGenerator callback. For example, you can also pick intro as another file name eligible for automatically becoming the category index.
export default { plugins: [ [ '@docusaurus/plugin-content-docs', { async sidebarItemsGenerator({ ...args, isCategoryIndex: defaultCategoryIndexMatcher, // The default matcher implementation, given below defaultSidebarItemsGenerator, }) { return defaultSidebarItemsGenerator({ ...args, isCategoryIndex(doc) { return ( // Also pick intro.md in addition to the default ones doc.fileName.toLowerCase() === 'intro' || defaultCategoryIndexMatcher(doc) ); }, }); }, }, ], ],};Or choose to not have any category index convention.
export default { plugins: [ [ '@docusaurus/plugin-content-docs', { async sidebarItemsGenerator({ ...args, isCategoryIndex: defaultCategoryIndexMatcher, // The default matcher implementation, given below defaultSidebarItemsGenerator, }) { return defaultSidebarItemsGenerator({ ...args, isCategoryIndex() { // No doc will be automatically picked as category index return false; }, }); }, }, ], ],};The isCategoryIndex matcher will be provided with three fields:
fileName, the file's name without extension and with casing preserveddirectories, the list of directory names from the lowest level to the highest level, relative to the docs root directoryextension, the file's extension, with a leading dot.
For example, for a doc file at guides/sidebar/autogenerated.md, the props the matcher receives are
const props = {
fileName: 'autogenerated',
directories: ['sidebar', 'guides'],
extension: '.md',
};The default implementation is:
function isCategoryIndex({fileName, directories}) {
const eligibleDocIndexNames = [
'index',
'readme',
directories[0].toLowerCase(),
];
return eligibleDocIndexNames.includes(fileName.toLowerCase());
}Autogenerated sidebar metadata
Section titled βAutogenerated sidebar metadataβFor handwritten sidebar definitions, you would provide metadata to sidebar items through sidebars.js; for autogenerated, Docusaurus would read them from the item's respective file. In addition, you may want to adjust the relative position of each item because, by default, items within a sidebar slice will be generated in alphabetical order (using file and folder names).
Doc item metadata
Section titled βDoc item metadataβThe label, className, key, and customProps attributes are declared in front matter as sidebar_label, sidebar_class_name, sidebar_key and sidebar_custom_props, respectively. Position can be specified in the same way, via sidebar_position front matter.
---sidebar_position: 2sidebar_label: Easysidebar_class_name: greensidebar_key: unique-sidebar-item-key---# Easy TutorialThis is the easy tutorial!Category item metadata
Section titled βCategory item metadataβAdd a _category_.json or _category_.yml file in the respective folder. You can specify any category metadata and also the position metadata. label, className, key, position, and customProps will default to the respective values of the category's linked doc, if there is one.
{
"position": 2.5,
"label": "Tutorial",
"key": "unique-sidebar-item-key",
"collapsible": true,
"collapsed": false,
"className": "red",
"link": {
"type": "generated-index",
"title": "Tutorial overview"
},
"customProps": {
"description": "This description can be used in the swizzled DocCard"
}
}position: 2.5 # float position is supported
label: 'Tutorial'
collapsible: true # make the category collapsible
collapsed: false # keep the category open by default
className: red
link:
type: generated-index
title: Tutorial overview
customProps:
description: This description can be used in the swizzled DocCardUsing number prefixes
Section titled βUsing number prefixesβA simple way to order an autogenerated sidebar is to prefix docs and folders by number prefixes, which also makes them appear in the file system in the same order when sorted by file name:
docs
βββ 01-Intro.md
βββ 02-Tutorial Easy
β βββ 01-First Part.md
β βββ 02-Second Part.md
β βββ 03-End.md
βββ 03-Tutorial Advanced
β βββ 01-First Part.md
β βββ 02-Second Part.md
β βββ 03-Third Part.md
β βββ 04-End.md
βββ 04-End.mdTo make it easier to adopt, Docusaurus supports multiple number prefix patterns.
By default, Docusaurus will remove the number prefix from the doc id, title, label, and URL paths.
Customize the sidebar items generator
Section titled βCustomize the sidebar items generatorβYou can provide a custom sidebarItemsGenerator function in the docs plugin (or preset) config:
export default { plugins: [ [ '@docusaurus/plugin-content-docs', { async sidebarItemsGenerator({ defaultSidebarItemsGenerator, numberPrefixParser, item, version, docs, categoriesMetadata, isCategoryIndex, }) { // Example: return an hardcoded list of static sidebar items return [ {type: 'doc', id: 'doc1'}, {type: 'doc', id: 'doc2'}, ]; }, }, ], ],};