多实例多版本文档配置
本文介绍如何配置多个文档,并且每个文档有其自己的版本管理功能。此部分官方文档中有详细说明,但是分布在不同地方。本文将这些内容整理在一起,形成一份完整的指导性文档。
官方指导文档地址:
创建多实例文档
此功能需要借助插件 @docusaurus/plugin-content-docs,此插件无需安装,为自带插件。
备注
此功能仅为支持版本化文档,如果仅需要增加侧边栏(sidebar),请使用多侧边栏功能。
1. 添加文档配置
首选需要创建文档文件夹如:docs2。然后在 docusaurus.config.ts 中增加:
docusaurus.config.js
{
plugins: [
[
'@docusaurus/plugin-content-docs',
{
id: 'docs2',
path: 'docs2',
routeBasePath: 'docs2',
sidebarPath: './sidebarsDocs2.js',
// ... other options
},
],
],
}
id: 通过插件配置的话,这个ID是必须的,如果不配置会与默认配置重复,导致项目无法正常使用path:文档对应的文件夹routeBasePath:文档的访问地址,如上,访问地址就是:localhost:3000/docs2sidebarPath:是文档边栏的配置文件地址
更多配置项,请参考:https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs
为何要为 @docusaurus/plugin-content-docs 设置ID?
如果您使用了@docusaurus/preset-classic,默认会有以下配置。
{
presets: [
[
'classic',
{
docs: {
//id: 'default' // 如果不配置ID,默认ID是default
sidebarPath: './sidebars.ts'
}
}
]
];
}
这其实相当于已经启用了一个@docusaurus/plugin-content-docs插件实例,在plugins中又配置了一个相当于又启用一个实例。如果这时候不配置 id,两个实例都会用默认id,就会出现id重复的问题。因此plugins中的@docusaurus/plugin-content-docs这里必须配置一个id,如果添加了多个,则每个都需要配置id,且id不能重复。
2. 添加侧边栏配置
这里侧边栏我们使用默认配置
sidebarsDocs2.ts
import type {SidebarsConfig} from '@docusaurus/plugin-content-docs';
const sidebars: SidebarsConfig = {
// By default, Docusaurus generates a sidebar from the docs folder structure
docs2Sidebar: [{type: 'autogenerated', dirName: '.'}],
};
export default sidebars;
3. 添加nav配置
配置位置 themeConfig -> navbar -> items。
{
themeConfig: {
navbar: {
items: [
{
type: 'docSidebar',
sidebarId: 'tutorialSidebar',
position: 'left',
label: 'Tutorial',
},
{
type: 'docSidebar',
sidebarId: 'docs2Sidebar',
position: 'left',
label: 'Tutorial2',
docsPluginId: 'docs2'
},
{to: '/blog', label: 'Blog', position: 'left'},
{
href: 'https://github.com/facebook/docusaurus',
label: 'GitHub',
position: 'right',
},
],
}
}
添加版本支持
版本化文档的目录结构
一个典型的版本化文档结构如下:
website
├── sidebars.json # sidebar for the current docs version
├── docs # docs directory for the current docs version
│ ├── foo
│ │ └── bar.md # https://mysite.com/docs/next/foo/bar
│ └── hello.md # https://mysite.com/docs/next/hello
├── versions.json # file to indicate what versions are available
├── versioned_docs
│ ├── version-1.1.0
│ │ ├── foo