在Docusaurus 2中有没有办法拥有两个文档?

22
据我所知,Docusaurus支持自定义页面,但是否有一种方式可以在同一个Docusaurus项目中拥有两个文档呢?
原始的Navbar选项有:
- 文档 (Docs) - 博客 (Blog) - ...
我希望有类似于这样的内容:
- 文档1 (Docs 1) - 文档2 (Docs 2) - 博客 (Blog) - ...
我知道我可以在一个文档中创建许多子文件夹,但由于某些原因,我想要一个两个文档结构,这可以为我提供更清晰的访问文档的方式。
如果Docusaurus目前无法提供此功能,我想问是否还有其他文档框架提供此功能?

1
这个官方文档在这里:https://docusaurus.io/docs/docs-multi-instance - Sebastien Lorber
7个回答

47
你需要使用 plugin-content-docs 插件。
首先,创建其他的 docs 文件夹,例如 docsdocs-apidocs-system(1) 在你的 docusaurus.config.js 文件中,配置你的默认文档:
(module.exports = { // start of the module.export declaration
[…]

    presets: [
        [
          '@docusaurus/preset-classic',
          {
            docs: {
              routeBasePath: 'docs',
              path: 'docs',
              sidebarPath: require.resolve('./sidebars.js'),
              lastVersion: 'current',
              onlyIncludeVersions: ['current'],
            },
            theme: {
              customCss: require.resolve('./src/css/custom.css'),
            },
          },
        ],
      ],

[…] 
}); // end of the module-export declaration

(2)现在,神奇的事情发生了!:在同一文件中,配置您的其他文档:

(module.exports = { // start of the module.export declaration
[…]

plugins: [
    […]
    [
      '@docusaurus/plugin-content-docs',
      {
        id: 'docs-api',
        path: 'docs-api',
        routeBasePath: 'docs-api',
        sidebarPath: require.resolve('./sidebars.js'),
      }, 
    ],
    [
      '@docusaurus/plugin-content-docs',
      {
        id: 'docs-system',
        path: 'docs-system',
        routeBasePath: 'docs-system',
        sidebarPath: require.resolve('./sidebars.js'),
      }, 
    ],
],

[…]
}); // end of the module-export declaration

(3) 现在您可能希望将这些文档添加到您的导航栏中,是吗?那么就加入它们吧!

(module.exports = { // start of the module.export declaration
[…]

navbar: {
  hideOnScroll: true,
  title: 'your title',
  logo: {
    alt: '',
    src: 'img/favicon.ico',
  },
  items: [
    {
      to: '/docs/Intro',    // ./docs/Intro.md
      label: 'Docs Title',
      position: 'left',
      activeBaseRegex: `/docs/`,
    },
    {
      to: '/docs-api/Intro',    // ./docs-api/Intro.md
      label: 'API',
      position: 'left',
      activeBaseRegex: `/docs-api/`,
    },
    {
      to: '/docs-system/Introducao',  // ./docs-system/Intro.md
      label: 'My System',
      position: 'left',
      activeBaseRegex: `/docs-system/`,
    },
  ],
},

[…]
}); // end of the module-export declaration

重要提示

有时您会修改docusaurus.config.js文件,但不会“工作”,因此请关闭docusaurus service(在终端/ powershell 中只需Ctrl+C),然后重新启动它——如果我之前知道这一点,我可能会节省几个小时。

如果您没有plugin-content-docs插件,请安装它:

npm install --save @docusaurus/plugin-content-docs


路线图

我花了很大的力气才弄清楚这个问题。 我所做的是下载整个docusaurus项目,获取网站部分,删除所有我不需要的内容,这就是我得到的结果。


参考资料 (更新于2022年03月02日)

https://docusaurus.io/docs/docs-multi-instance


2
这个答案值得更多的赞扬,谢谢你!! - regexAgainstTheMachine
1
你节省了我很多时间! - Poeta Kodu
2
谢谢。给了我正确的方向。为了使其在导航栏中工作,我还必须使用docsPluginIddocId - elpddev
2
这里有官方文档记录:https://docusaurus.io/docs/docs-multi-instance - Sebastien Lorber
请注意,额外的实例需要指向唯一的文档,即确保为每个实例指定唯一的sidebar-<subinstance>.js文件。 - Zenahr

7

这个解决方案对我有效。在Docusaurus v2.0.0-beta.15中,使用“autogenerated”侧边栏即可。

sidebars.js

/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */

const sidebars = {

  // tutorialSidebar: [{type: 'autogenerated', dirName: '.'}],
  newone: [{type: 'autogenerated', dirName: 'newone'}],  // foldername
  newtwo: [{type: 'autogenerated', dirName: 'newtwo'}],  // foldername

};

module.exports = sidebars;

docusaurus.config.js

      navbar: {
        title: 'My Site',
        logo: {
          alt: 'My Site Logo',
          src: 'img/logo.svg',
        },
        items: [
          // {
          //   type: 'doc',
          //   docId: 'intro',
          //   position: 'left',
          //   label: 'Tutorials',
          // },

          {
            type: 'docSidebar',  // docSidebar
            position: 'left',
            sidebarId: 'newone', // foldername
            label: 'NEWONE',     // navbar title
          },
          {
            type: 'docSidebar',  // docSidebar
            position: 'left',
            sidebarId: 'newtwo', // foldername
            label: 'NEWTWO',     // navbar title
          },

          {to: '/blog', label: 'Blog', position: 'left'},
          {
            href: 'https://github.com/facebook/docusaurus',
            label: 'GitHub',
            position: 'right',
          },
        ],
      },

您的文档文件夹:
docs/
    newone/
        intro.md
    newtwo/
        intro.md

完美的答案!谢谢 - Andreas Prang

1
我尝试了这种方法,它是有效的。 [编辑1]:但当我选择API时,导航栏中的API和文档都变成了绿色。你能告诉我们这背后的原因吗@Yangshun Tay?你能为此提供修改建议吗? [编辑2]:我阅读了文档,它是在@docusaurus/theme-classic中编写的,如果我们设置activeBasePath属性,则具有相同路径(在本例中为docs)的链接将具有活动属性。

sidebar.js

module.exports = {
    someSidebar: {
        Docusaurus: ['doc1', 'doc2'],
        Features: ['doc3']
    },
    someOtherSidebar: {
        Test: ['mdx']
    }
};

docusaurus.config.js

导航栏链接如下 -
links: [
    {
        to: 'docs/doc1',
        // activeBasePath: 'docs', // [Edit 3]
        label: 'Docs',
        position: 'left'
    },
    {
        to: 'docs/mdx',
        label: 'API',
        position: 'left'
    },
]

docs文件夹的文件夹结构如下所示 -

docs
├── docs1.md
├── mdx.md

0
无论您使用的是v1还是v2,sidebars.js配置都可以包含多个键, 每个键都有自己的侧边栏。

2
我认为他的意思是另外一件事情,借助于 sidebars.js 我们可以在 /docs 路径下创建多个分区。但他所要求的是创建两个独立的文档文件夹 APIdocs,它们具有与 docs 相同的功能。这在 docusaurus 导航栏的 v1 版本中已经存在。 - Germa Vinsmoke
他的意思是导航栏。 - Germa Vinsmoke
导航栏项目只是 href,您可以在其中放置任何内容。 - Yangshun Tay
如果我想要一个与博客页面完全相似的新闻页面,那么在v2中创建一个是否有可能?我尝试创建一个名为news的文件夹,并将.mdx文件移动到其中。但是它没有起作用。 - Germa Vinsmoke
1
是的!GermaVinsmoke所说的正是我的意思。 @YangshunTay 我明白Navbar项只是hrefs,我可以自定义它。但是文档生成是自动的,包括所有路由和侧边栏。我可以创建一个新页面并放置在导航栏上,但如何创建另一个具有不同侧边栏的新文档呢? - Echo Yang
@EchoYang 请尝试我的解决方案。 - Germa Vinsmoke

0
在我的测试中,你必须包括路径"docs-xxxxxxxxx"!不要创建其他名称,比如"education",否则页面会崩溃!

0

将Docusaurus设置为多实例需要跨越许多文件进行更改。为了使其更容易设置,我创建了一个基本安装程序,其中包含所有必要的更改以进行多实例,并将其发布为GitHub模板。

在此处分叉:
mg0716/docusaurus-multi

该存储库中的许多更改都是由@d-kastier的原始comment导致的。

非常乐意接受反馈和拉取请求,所以请随时尝试!


0

你需要在docusaurus配置中使用文档类型。我认为“to”类型是针对页面而不是文档的。

为了使侧边栏正确,你还需要设置配置中的activeSidebarClassName值,以便它知道你想要为这个文档使用哪个侧边栏(在sidebars.js中导出的那些侧边栏之一)。

activeSidebarClassName: 'navbar__link--active',

https://docusaurus.io/docs/api/themes/configuration#navbar-doc-link


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接