Search code examples
sidebardocusaurus

How to omit "index" entry from an autogenerated Docusaurus V2 sidebar


At work we have a basic Docusaurus v2 page for user documentation, and I can't share it for privacy reasons. Suffice it to say it has a sidebar which is autogenerated, where the top level contains a number of folders as categories and each category only contains .md files.

At the top level (the level of the categories) there is an empty index.md file that only exists so that the page will load. The autogenerated sidebar includes an index entry that points to a blank page. I would like to hide/get rid of this entry.

I have looked at this github discussion on something similar, but I haven't been able to make the solutions work. The sidebar.js file has the following simple contents:

module.exports = {
  docs: [
    {
      type: 'autogenerated',
      dirName: '.'
    },
  ],
};

I have tried adding an exclude: ['path\to\index\file'] line, but this results in the error "exclude" is not allowed.

What is the proper way of hiding this index entry from the sidebar? Alternatively, is there a way to set up the site so that the index.md file is not needed at all?


Solution

  • I have the same setup:

    /folder1
      /file
    /folder2
      /file
    index
    

    And I wand to autogenerate the sidebar with two categories only:

    • folder1
    • folder2

    Moreover, I wanted to click on the navbar and see index.

    I was able to do so by:

    Create a custom sidebar

    function skipIndex(items) {
      return items.filter(({ type, id }) => {
        return type !== 'doc' || id !== 'index';
      });
    }
    
    module.exports = async function sidebarItemsGenerator({ defaultSidebarItemsGenerator, ...args }) {
      const sidebarItems = await defaultSidebarItemsGenerator(args);
      return skipIndex(sidebarItems);
    }
    

    Then in the docusaurus.config.js

      presets: [
        [
          'classic',
          ({
            // https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs#configuration
            docs: {
              sidebarItemsGenerator: require('./sidebar.js'),
            },
    

    And finally in the index.md file I must add this metadata, otherwise when I reach the index page, the sidebar disappears because the page is not included:

    ---
    displayed_sidebar: docsSidebar
    ---