You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto generate sidebar according to the docs folder structure #3464
For me, it seems to be redundant to manage both the sidebars file and the docs folder structure, since the sidebars can be inferred from it.
Also, every time I add a new md file, I have to add it to the sidebar. This might not be a problem for fully manual work, but it's a problem when the md files can be generated automatically so we don't know beforehand what would be their IDs.
This could be inferred automatically rather than requiring the user to maintain a separate file. Further customizations could be made through Front Matter in each doc. In case a folder requires personalization, a README.md could also be created for it.
Reacted by Fanny, Matt Sardi, Matt Cromwell, Doodles, Tyler Yep, Simon Victory, Michal Cialowicz, Latiif AlSharif, Tatyana, Matt Yosefpor and 20 more
Perhaps prefixing numbers in the files: 1-b.md, 2-a.md.
Why not, but you'd rather add slug frontmatter to have nice url paths
I guess the category labels should be inferred from the folder names. So this means folder names should also have a number prefix for ordering.
That also means it's not possible to have docs without sidebar, nor multiple sidebars, nor refs or external links in sidebars, so this option is limited compared to have a proper sidebars file.
Wonder if it's really worth it. It may as well be quite confusing and add some complexity for no real gains except replacing centralised config boilerplate by number prefixes and frontmatter boilerrplate.
Do you really see yourself using such a feature? If so, I'd like to ask you a commitment first.
For example:
show me a Docusaurus site with a sidebars file (browsable online + github repository).
make a git branch and show me the exact docs FS structure and frontmatter you'd like to use to be able to get the first site running without the sidebar (I don't expect the site to be running obviously, as the feature is not implemented)
I wrote a script for one of our monorepos that generates markdown from jsdoc comments, keeping the folder structure, and then auto-generates the sidebars.js while preserving any custom entries. It needs a few tweaks like alphabetizing and removing non-existent files, but it should give you a starting point for your project.
I guess the category labels should be inferred from the folder names. So this means folder names should also have a number prefix for ordering.
Not only. As per my initial proposal, further customizations for the categories could be made through an index.md in the root of each folder/category.
That also means it's not possible to have docs without sidebar, nor multiple sidebars, nor refs or external links in sidebars, so this option is limited compared to have a proper sidebars file.
I was not thinking this way. I think we must add this feature in a way that you are free to use both methods (manual or auto-generated sidebars). Something like:
Notice that in API category, I referenced a folder, this means it's supposed to be auto-generated. But probably we think of a more clean way.
Do you really see yourself using such a feature? If so, I'd like to ask you a commitment first.
Yes, but I totally understand that you should not waste time to implement a feature if you have a single user. Perhaps some kind of votation could help? (I'm just imagining)
show me a Docusaurus site with a sidebars file (browsable online + github repository).
make a git branch and show me the exact docs FS structure and frontmatter you'd like to use to be able to get the first site running without the sidebar (I don't expect the site to be running obviously, as the feature is not implemented)
The master branch is already a good example:
- docs
- index.md # auto-generated by website/scripts/import-readme.md
- api # auto-generated by docusaurus-plugin-typedoc
- globals.md
- Classes
- Image.md
- SaveImage.md
The sidebars labels are already customized because of the title Front Matter in each of the files.
I need to work on i18n, not sure there's a huge need for this right now, and we need to figure out a good API surface for this feature first, so unless someone wants to help design/implement this will wait, but we can keep the discussion about API design.
An idea could be to use a special kind of category type, somehow quite similar to your proposal but maybe more explicit.
One important thing to consider is that it should work well with versioning. If we copy the sidebars.js to the versioned folders, it should still work so we should avoid relative paths/imports in this sidebars file, so creating "utils" to generate a sidebars slice from a relative path is likely not a good idea (related to #3285 (comment)).
or if the category is an i18n name or something like Why Docsaurus?, we have a problem. Thus, I suggest to a _category_.md (like __init.py) file defining how the details of the category, e.g.
A "sidebar slice" will generate a list of sidebar items (be replaced with), and you can "concat" 2 sidebar slices under a single sidebar category if you want:
Docusaurus will look for a _category_.json or _category_.yml file in autogenerated dirs, on which you will be able to provide sidebar category item attributes like collapsible, label
When we'll implement category index files, we'll read this metadata directly on the Category/index.md frontmatter too.
Sidebar ordering
A new category and doc metadata/frontmatter sidebar_position will be used in priority
By default, Docusaurus will look for number prefix patterns in filenames and dirnames, and try to use those when there is no explicit sidebar_position
The number prefixes will be stripped to keep clean slugs/urls, titles and ids, so that in most cases you don't need to provide custom frontmatter. You can opt-out from extracting the number prefix from filenames if your doc really needs to start with a number (strip_number_prefixes: false).
Provide your own generator
Docusaurus has a default way to generate a sidebar from the FS. We could allow users to provide their own generator function by using docs plugin like sidebarGenerator: ({item, version, docs}) => ([]).
It would be a hook/function that would receive the "autogenerated" sidebar items we found, and would have to return the sidebar slice that would replace the autogenerated item. It would receive the version and all available docs metadatas.
Should we allow float numbers or limit to ints? Can float be useful to "interleave" a new doc anywhere between 2 existing docs?
What should be the name of the docs/category sidebar metadata/frontmatter used for ordering. Is sidebar_position appropriate?
Is the position used only to order inside the current sidebar slice, or it is used to order the category in general (ie if a category contains 2 sidebar slices, the items of the 2 sidebar slices could be interleaved?)
Should we allow to add a position metadata to docs/categories also in the regular sidebars.js docs and categories? (non auto-generated items).
My opinion is that the position metadata should only sort "inside a sidebar slice", or it could be confusing: you could position manually a doc in a sidebar, and have it unexpectedly repositioned differently after the sidebar has been sorted.
About number prefixes:
What are the most common patterns for prefixing dirs/filenames with numbers? Do you have doc links to share?
About sidebarGenerator custom generator function:
What is the minimal version/docs metadata we should provide? (we probably don't want to create a too large API surface here or it can be complicated to avoid breaking changes)
Should we allow any custom metadata on the autogenerated item, so that you could use it on the fn?
Should we inject all available docs of the version, or only a subset (ie filtered by a dir? which means the "dirPath" option is always required?)
Should we provide dir paths / docs paths relative to the version docs root website/docs or relative to the dirPath folder?
Worth mentioning:
We only generate categories for folders containing actual docs
Using number prefixes can be annoying for linking with relative md file paths, as changing the prefix means changing the markdown link
Reacted by Felipe Santos, hitochan777, Xaber, Giovanni Coutinho and Artem Dyachuk
@slorber I read through this the other day, and had some time to process. Overall I think this is a great approach, well done. I don't think we would have a use case for numerical sort ordering of auto-generated content, we prefer the default alphabetical ordering of file names/titles so it's easier to find in the nav. But maybe there are others who would use numbering?
Is autogenerated a good name for this?
Sounds good to me. It's explicit about its purpose.
Is the position used only to order inside the current sidebar slice, or it is used to order the category in general (ie if a category contains 2 sidebar slices, the items of the 2 sidebar slices could be interleaved?)
I would think the numbering would be scoped to the slice, so not interleaved. I think the complexity of interleaving could be its own feature - or some sort of param boolean to enable or merge sort order. How would conflicts be handled?
Should we allow to add a position metadata to docs/categories also in the regular sidebars.js docs and categories? (non auto-generated items).
My opinion is that the position metadata should only sort "inside a sidebar slice", or it could be confusing: you could position manually a doc in a sidebar, and have it unexpectedly repositioned differently after the sidebar has been sorted.
Agreed. Re-sorting a manually sorted list could lead to unintended confusion/bugs.
If we want a custom sort order, we would manually do that in the sidebar.js file and use slices for the rest. So maybe slices would ignore plucked files, kind of how object spread works in React?
If we want a custom sort order, we would manually do that in the sidebar.js file and use slices for the rest. So maybe slices would ignore plucked files, kind of how object spread works in React?
const { title, url, ...rest } = props
For now we don't "deduplicate" items, so basically you can do that and end-up with the sidebar slice being generated twice:
@slorber is there a way to specify items within an autogenerated slice by file?
Possibly use the items key inside of a _category_.yml?
I want just the top level of my slice to be autogenerated to avoid confusion for others maintaining another section of our docs that want to use autogenerated slices all the way down.
@0Grit I'd appreciate to understand better your use-case: why deeply nested auto-generation is confusing and why do you want to opt-out of it? Why do you want to keep subfolders in the autogenerated folder?
@slorber I have a perception that git diffs will be cleaner and rebases will also have less merge conflicts if I'm encoding my documentation structure in a file versus using the file-system concept to set the doc structure.
Documentation structure as code versus documentation structure as filesystem. It is easier to change the contents of a limited number of files versus moving files around the filesystem and having to modify lots of __category_.yml files.
This is my personal preference having experiences with this sort of thing in the past. Other people on the team including maintainer of our docs repository currently prefer the autogeneration.
If you just auto-generate on a single level, why don't you just not auto-generate and declare an explicit sidebar category instead?
Subcategories can still be auto-generated if needed.
You can put all your docs flat in a single folder and create your sidebar tree manually: no file move needed
@slorber Yes, your recommendation, is what I was going to end up doing.
I didn't want to need to change sidebar.js to do it.
I also thought it might be nice to be able to specify my sidebar in a more modular fashion with __category__.yml or some other file as desired.
Activity
fanny commentedon Sep 22, 2020
We could have the both options. It seems a good proposal for me.
slorber commentedon Sep 23, 2020
felipecrs commentedon Sep 23, 2020
If this ends up with a new option in the
docusaurus.config.js, such asinferSidebarsFromFolder, we could simply make it default tofalse.Perhaps prefixing numbers in the files:
1-b.md,2-a.md.slorber commentedon Sep 28, 2020
Why not, but you'd rather add slug frontmatter to have nice url paths
I guess the category labels should be inferred from the folder names. So this means folder names should also have a number prefix for ordering.
That also means it's not possible to have docs without sidebar, nor multiple sidebars, nor refs or external links in sidebars, so this option is limited compared to have a proper sidebars file.
Wonder if it's really worth it. It may as well be quite confusing and add some complexity for no real gains except replacing centralised config boilerplate by number prefixes and frontmatter boilerrplate.
Do you really see yourself using such a feature? If so, I'd like to ask you a commitment first.
For example:
arinthros commentedon Sep 29, 2020
I wrote a script for one of our monorepos that generates markdown from jsdoc comments, keeping the folder structure, and then auto-generates the sidebars.js while preserving any custom entries. It needs a few tweaks like alphabetizing and removing non-existent files, but it should give you a starting point for your project.
https://gist.github.com/arinthros/526df3ef12383d26903c1c8588211cca
The sidebar structure looks like this
felipecrs commentedon Sep 29, 2020
@arinthros Sounds very cool!
Just to mention, the
docusaurus-plugin-typedocalso does that. And this is how I use it:https://github.com/felipecrs/megatar/blob/master/website/sidebars.js#L5
The plugin generates the
typedoc-sidebars.jsfile, then I import it from mysidebars.js.Result: https://felipesantos.dev/megatar/api/
felipecrs commentedon Sep 29, 2020
Not only. As per my initial proposal, further customizations for the categories could be made through an
index.mdin the root of each folder/category.I was not thinking this way. I think we must add this feature in a way that you are free to use both methods (manual or auto-generated sidebars). Something like:
Notice that in
APIcategory, I referenced a folder, this means it's supposed to be auto-generated. But probably we think of a more clean way.Yes, but I totally understand that you should not waste time to implement a feature if you have a single user. Perhaps some kind of votation could help? (I'm just imagining)
https://felipesantos.dev/megatar
https://github.com/felipecrs/megatar/tree/master/website
Not a production-grade example though.
The
masterbranch is already a good example:The sidebars labels are already customized because of the
titleFront Matter in each of the files.slorber commentedon Sep 30, 2020
Thanks for the infos.
I need to work on i18n, not sure there's a huge need for this right now, and we need to figure out a good API surface for this feature first, so unless someone wants to help design/implement this will wait, but we can keep the discussion about API design.
An idea could be to use a special kind of category type, somehow quite similar to your proposal but maybe more explicit.
One important thing to consider is that it should work well with versioning. If we copy the sidebars.js to the versioned folders, it should still work so we should avoid relative paths/imports in this sidebars file, so creating "utils" to generate a sidebars slice from a relative path is likely not a good idea (related to #3285 (comment)).
felipecrs commentedon Sep 30, 2020
Awesome, your design is more refined than what I proposed. LGTM.
Clindbergh commentedon Oct 19, 2020
Above example would be generated from this directory:
[tree credits], where all .md files have a
titleset, correct?If later Design Principles itself becomes a folder (which will be then be an autogenerated category), e.g.
or if the category is an i18n name or something like Why Docsaurus?, we have a problem. Thus, I suggest to a
_category_.md(like __init.py) file defining how the details of the category, e.g.This would make the Docs only mode much more useful.
11 remaining items
slorber commentedon Apr 13, 2021
Asking for feedbacks
I've made some progress on this feature and would like to have your feedbacks.
Sidebar configuration:
You can use a new sidebar item type
autogenerated, and it will generate a sidebar slice :By default, a Docusaurus site will auto-generate from its
website/docsfolder.The default/fallback sidebar config in case sidebars.js is absent:
A "sidebar slice" will generate a list of sidebar items (be replaced with), and you can "concat" 2 sidebar slices under a single sidebar category if you want:
Category metadata file
Docusaurus will look for a
_category_.jsonor_category_.ymlfile in autogenerated dirs, on which you will be able to provide sidebar category item attributes like collapsible, labelWhen we'll implement category index files, we'll read this metadata directly on the
Category/index.mdfrontmatter too.Sidebar ordering
A new category and doc metadata/frontmatter
sidebar_positionwill be used in priorityBy default, Docusaurus will look for number prefix patterns in filenames and dirnames, and try to use those when there is no explicit
sidebar_positionThe number prefixes will be stripped to keep clean slugs/urls, titles and ids, so that in most cases you don't need to provide custom frontmatter. You can opt-out from extracting the number prefix from filenames if your doc really needs to start with a number (
strip_number_prefixes: false).Provide your own generator
Docusaurus has a default way to generate a sidebar from the FS. We could allow users to provide their own generator function by using docs plugin like
sidebarGenerator: ({item, version, docs}) => ([]).It would be a hook/function that would receive the "autogenerated" sidebar items we found, and would have to return the sidebar slice that would replace the autogenerated item. It would receive the version and all available docs metadatas.
Signature
Feedback questions
About the new sidebar item
autogenerated:autogenerateda good name for this?About position metadata:
sidebar_positionappropriate?sidebars.jsdocs and categories? (non auto-generated items).About number prefixes:
About
sidebarGeneratorcustom generator function:autogenerateditem, so that you could use it on the fn?website/docsor relative to thedirPathfolder?Worth mentioning:
slorber commentedon Apr 14, 2021
Let me know if anyone has any feedback or concern with my implementation, names or anything.
The PR is ready to merge, just need to add the doc now so will likely merge it tomorrow.
felipecrs commentedon Apr 14, 2021
The proposed design seems very good to my perspective. I don't have any concerns.
arinthros commentedon Apr 14, 2021
@slorber I read through this the other day, and had some time to process. Overall I think this is a great approach, well done. I don't think we would have a use case for numerical sort ordering of auto-generated content, we prefer the default alphabetical ordering of file names/titles so it's easier to find in the nav. But maybe there are others who would use numbering?
Sounds good to me. It's explicit about its purpose.
I would think the numbering would be scoped to the slice, so not interleaved. I think the complexity of interleaving could be its own feature - or some sort of param boolean to enable or merge sort order. How would conflicts be handled?
Agreed. Re-sorting a manually sorted list could lead to unintended confusion/bugs.
arinthros commentedon Apr 14, 2021
If we want a custom sort order, we would manually do that in the sidebar.js file and use slices for the rest. So maybe slices would ignore plucked files, kind of how object spread works in React?
const { title, url, ...rest } = propsslorber commentedon Apr 15, 2021
Thanks for the feedbacks, so it seems my implementation is good enough to merge.
@arinthros
For now we don't "deduplicate" items, so basically you can do that and end-up with the sidebar slice being generated twice:
I don't think we shuld do anything clever regarding this kind of situation.
We could add a way to include/exclude some docs by using glob patterns:
I don't think this is needed now, but that can be added later if this is requested.
0Grit commentedon Nov 11, 2021
@slorber is there a way to specify items within an autogenerated slice by file?
Possibly use the
itemskey inside of a_category_.yml?I want just the top level of my slice to be autogenerated to avoid confusion for others maintaining another section of our docs that want to use autogenerated slices all the way down.
Josh-Cena commentedon Nov 11, 2021
@0Grit See #5689. This is another enhancement request.
slorber commentedon Nov 12, 2021
yes #5689 is a good place to discuss that.
@0Grit I'd appreciate to understand better your use-case: why deeply nested auto-generation is confusing and why do you want to opt-out of it? Why do you want to keep subfolders in the autogenerated folder?
0Grit commentedon Nov 12, 2021
@slorber I have a perception that git diffs will be cleaner and rebases will also have less merge conflicts if I'm encoding my documentation structure in a file versus using the file-system concept to set the doc structure.
0Grit commentedon Nov 12, 2021
Documentation structure as code versus documentation structure as filesystem. It is easier to change the contents of a limited number of files versus moving files around the filesystem and having to modify lots of
__category_.ymlfiles.0Grit commentedon Nov 12, 2021
This is my personal preference having experiences with this sort of thing in the past. Other people on the team including maintainer of our docs repository currently prefer the autogeneration.
slorber commentedon Nov 12, 2021
@0Grit it's still hard to understand to me.
If you just auto-generate on a single level, why don't you just not auto-generate and declare an explicit sidebar category instead?
Subcategories can still be auto-generated if needed.
You can put all your docs flat in a single folder and create your sidebar tree manually: no file move needed
0Grit commentedon Nov 12, 2021
@slorber Yes, your recommendation, is what I was going to end up doing.
I didn't want to need to change
sidebar.jsto do it.I also thought it might be nice to be able to specify my sidebar in a more modular fashion with
__category__.ymlor some other file as desired.