On this page

Sidebar

Creating a sidebar is useful to:

  • Group multiple related documents
  • Display a sidebar on each of those documents

To use sidebars on your Docuo site:

  1. Define a file that contains a dictionary of sidebar objects.
  2. 将文件中的 sidebar 对应的 key 在 navbar 的 docSidebar item type 中使用。
docuo.config
{
  "themeConfig": {
    "navbar": {
      "items": [
        {
          "type": "docSidebar",
          "label": "Docs",
          "sidebarIds": ["mySidebar"],
          "position": "left"
        }
      ]
    }
  }
}
1
Copied!

上面概述了文档侧边栏的基本用法,下面将更系统地介绍下 Sidebar items 和 Autogenerated 的概念。

We have introduced three types of item types in the example above: doc, category, and link, whose usages are fairly intuitive. We will formally introduce their APIs. There's also a fourth type: autogenerated, which we will explain in detail later.

  • Doc: link to a doc page, associating it with the sidebar
  • Link: link to any internal or external page
  • Category: creates a dropdown of sidebar items
  • Autogenerated: generate a sidebar slice automatically

Use the doc type to link to a doc page and assign that doc to a sidebar:

type
type SidebarItemDoc =
  // Normal syntax
  | {
      type: "doc";
      id: string;
      label: string; // Sidebar label text
    }

  // Shorthand syntax
  | string; // docId shortcut
1
Copied!

这里的 id 表示文档唯一标识,是文档相对 docs 目录的路径统一处理成纯小写且空格替换成链接符(-)后的字符串,比如文档的路径是“Overview/Quick Start”,那 id 就是 “overview/quick-start”。

Example:

sidebars
{
  "mySidebar": [
    // Normal syntax
    {
      "type": "doc",
      "id": "doc1", // document ID
      "label": "Getting started" // sidebar label
    },

    // Shorthand syntax
    "doc2" // document ID
  ]
}
1
Copied!

If you use the doc shorthand or autogenerated sidebar, you would lose the ability to customize the sidebar label through item definition.

WARNING

A doc item sets an implicit sidebar association. Don't assign the same doc to multiple sidebars.

Use the link type to link to any page (internal or external) that is not a doc.

type
type SidebarItemLink = {
  type: "link";
  label: string;
  href: string;
};
1
Copied!

Example:

sidebars
{
  "myLinksSidebar": [
    // External link
    {
      "type": "link",
      "label": "Facebook", // The link label
      "href": "https://facebook.com" // The external URL
    },

    // Internal link
    {
      "type": "link",
      "label": "Home", // The link label
      "href": "/" // The internal path
    }
  ]
}
1
Copied!

Category: create a hierarchy

Use the category type to create a hierarchy of sidebar items.

type
type SidebarItemCategory = {
  type: "category";
  label: string; // Sidebar label text.
  items: SidebarItem[]; // Array of sidebar items.
};
1
Copied!

Example:

sidebars
{
  "mySidebar": [
    {
      "type": "category",
      "label": "Guides",
      "items": [
        "creating-pages",
        {
          "type": "category",
          "label": "Docs",
          "items": [
            "introduction",
            "sidebar",
            "markdown-features",
            "versioning"
          ]
        }
      ]
    }
  ]
}
1
Copied!
TIP

Use the shorthand syntax when you don't need customizations:

sidebars
{
  "mySidebar": {
    "Guides": [
      "creating-pages",
      {
        "Docs": ["introduction", "sidebar", "markdown-features", "versioning"]
      }
    ]
  }
}
1
Copied!

Using shorthands

You can express typical sidebar items without much customization more concisely with shorthand syntaxes. There are two parts to this: doc shorthand and category shorthand.

Doc shorthand

An item with type doc can be simply a string representing its ID:

sidebars(Longhand)
sidebars(Shorthand)
{
  "sidebar": [
    {
      "type": "doc",
      "id": "myDoc"
    }
  ]
}
1
Copied!
{
  "sidebar": ["myDoc"]
}
1
Copied!

So it's possible to simplify the example above to:

sidebars
{
  "mySidebar": [
    {
      "type": "category",
      "label": "Getting Started",
      "items": ["doc1"]
    },
    {
      "type": "category",
      "label": "Docuo",
      "items": ["doc2", "doc3"]
    },
    {
      "type": "link",
      "label": "Learn more",
      "href": "https://example.com"
    }
  ]
}
1
Copied!

Category shorthand

A category item can be represented by an object whose key is its label, and the value is an array of subitems.

sidebars(Longhand)
sidebars(Shorthand)
{
  "sidebar": [
    {
      "type": "category",
      "label": "Getting started",
      "items": ["doc1", "doc2"]
    }
  ]
}
1
Copied!
{
  "sidebar": [
    {
      "Getting started": ["doc1", "doc2"]
    }
  ]
}
1
Copied!

This permits us to simplify that example to:

sidebars
{
  "mySidebar": [
    {
      "Getting started": ["doc1"]
    },
    {
      "Docuo": ["doc2", "doc3"]
    },
    {
      "type": "link",
      "label": "Learn more",
      "href": "https://example.com"
    }
  ]
}
1
Copied!

Each shorthand object after this transformation will contain exactly one entry. Now consider the further simplified example below:

sidebars
{
  "mySidebar": [
    {
      "Getting started": ["doc1"],
      "Docuo": ["doc2", "doc3"]
    },
    {
      "type": "link",
      "label": "Learn more",
      "href": "https://example.com"
    }
  ]
}
1
Copied!

Autogenerated

Docuo can create a sidebar automatically from your filesystem structure: each folder creates a sidebar category, and each file creates a doc link.

type
type SidebarItemAutogenerated = {
  type: "autogenerated";
  dirName: string; // Source folder to generate the sidebar slice from (relative to docs)
};
1
Copied!

Docuo can generate a full sidebar from your docs folder:

sidebars
{
  "myAutogeneratedSidebar": [
    {
      "type": "autogenerated",
      "dirName": "." // "." means the current docs folder
    }
  ]
}
1
Copied!

An autogenerated item is converted by Docuo to a sidebar slice (also discussed in category shorthand): 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.

Default sidebar

If the sidebarPath is unspecified, Docuo automatically use a default sidebar for you, by using the filesystem structure of the docs folder:

sidebars
{
  "mySidebar": [
    {
      "type": "autogenerated",
      "dirName": "." // generate sidebar from the docs folder
    }
  ]
}
1
Copied!

You can also define your sidebars explicitly.

A sidebar at its crux is a hierarchy of categories, doc links, and other hyperlinks.

type
type Sidebar =
  // Normal syntax
  | SidebarItem[]
  // Shorthand syntax
  | { [categoryLabel: string]: SidebarItem[] };
1
Copied!

For example:

sidebars
{
  "mySidebar": [
    {
      "type": "category",
      "label": "Getting Started",
      "items": [
        {
          "type": "doc",
          "id": "doc1"
        }
      ]
    },
    {
      "type": "category",
      "label": "Docuo",
      "items": [
        {
          "type": "doc",
          "id": "doc2"
        },
        {
          "type": "doc",
          "id": "doc3"
        }
      ]
    },
    {
      "type": "link",
      "label": "Learn more",
      "href": "https://example.com"
    }
  ]
}
1
Copied!

This is a sidebars file that exports one sidebar, called mySidebar. It has three top-level items: two categories and one external link. Within each category, there are a few doc links.

Complex sidebars example

以下是一个完整的复杂示例:

sidebars
{
  "mySidebar": [
    {
      "Quick Start": [
        "doc1",
        "doc2",
        {
          "type": "autogenerated",
          "dirName": "intro"
        }
      ]
    },
    {
      "type": "autogenerated",
      "dirName": "faq"
    },
    {
      "type": "link",
      "label": "Learn more",
      "href": "https://example.com"
    }
  ]
}
1
Copied!