Skip to content

Allow routes generated by addRoute to be listed in the plugin-content-docs sidebar #6067

@fsmaia

Description

@fsmaia

Have you 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?

https://docusaurus.io/feature-requests/p/allow-routes-generated-by-addroute-to-be-listed-in-the-plugin-content-docs-sideb

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 component may be used instead of a raw component generator
  • This React component may 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed: wontfixA fix will bring significant overhead, or is out of scope (for feature requests)featureThis is not a bug or issue with Docusausus, per se. It is a feature request for the future.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions