-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Description
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Description
Currently, when a plugin/theme uses addRoute to register dynamic routes, these routes cannot be used in the sidebar. Besides, when accessing them directly, they have no wrapping layout (header/sidebar).
As we have an autogenerated sidebar item type, we could have a specific type to list all the sub-routes of a base route.
Has this been requested on Canny?
Motivation
For plugin developers, using addRoute is way more powerful than performing an fs.createFileSync with a raw template, due to the following reasons:
- A React
componentmay be used instead of a raw component generator - This React
componentmay be a part of the theme, and also be swizzled or overridden - There is no need to manage generated files/folders
This resource would be extremely useful for producing API Reference, such as documenting all the React components in a project, or all the TS typings, in a extensible scalable way.
API design
Add a new type to SidebarItemConfig to list all the sub-routes within a route:
export type SidebarItemRoutes = SidebarItemBase & {
type: 'routes';
label: string;
baseRoute: string;
};Real-life example:
A custom plugin generating dynamic routes (simplified version):
async contentLoaded({ content, actions }): Promise<void> {
components.forEach(async component => {
const componentData = await createData(`${component}.json`, JSON.stringify(component));
addRoute({
path: `/docs/react/${component.displayName}`,
component: '@theme/ReactComponent',
modules: {
data: componentData,
},
});
});
});A Docusaurus configuration:
presets: [
[
'@docusaurus/preset-classic',
/** @type {import('@docusaurus/preset-classic').Options} */
{
docs: {
sidebarPath: require.resolve('./sidebars.js'),
}
}
]
],
plugins: [
[
'my-plugin',
{
src: ['../src/components/**'],
tsconfig: '../tsconfig.json',
},
],
],And finally, a docusaurus sidebar configuration:
module.exports = {
sidebar: [
{
type: 'routes',
label: 'React components',
baseRoute: '/docs/react'
},
];Have you tried building it?
As this affects docusaurus-plugin-content-docs, I'm afraid it can't be customized outside it.
Also it seems that content-docs has its own way to determine if a route should be wrapped by the layout or not.
Self-service
- I'd be willing to contribute this feature to Docusaurus myself.