跳到主要内容
版本:Canary 🚧

文档介绍

文档功能允许用户以层级格式组织编排 Markdown 文件。

信息

Check the Docs Plugin API Reference documentation for an exhaustive list of options.

你的网站的文档从低到高有四层组织架构:

  1. 独立页面。
  2. 侧边栏。
  3. 版本。
  4. 插件实例。

The guide will introduce them in that order: starting from how individual pages can be configured, to how to create a sidebar or multiple ones, to how to create and manage versions, to how to use multiple docs plugin instances.

仅文档模式

新创建的 Docusaurus 网站会有这样的结构:

example.com/                                -> 生成自 `src/pages/index.js`

example.com/docs/intro -> 生成自 `docs/intro.md`
example.com/docs/tutorial-basics/... -> 生成自 `docs/tutorial-basics/...`
...

example.com/blog/2021/08/26/welcome -> 生成自 `blog/2021-08-26-welcome/index.md`
example.com/blog/2021/08/01/mdx-blog-post -> 生成自 `blog/2021-08-01-mdx-blog-post.mdx`
...

所有文档都会放在 docs/ 子路由下。 但是如果你的网站只有文档,或者你想要把文档放在网站根部,突出它的重要性呢?

假设你的配置中包含以下内容:

docusaurus.config.js
export default {
// ...
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
/* docs plugin options */
},
blog: {
/* blog plugin options */
},
// ...
},
],
],
};

要进入仅文档模式,可以把它改成类似这样:

docusaurus.config.js
export default {
// ...
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
routeBasePath: '/', // Serve the docs at the site's root
/* other docs plugin options */
},
blog: false, // Optional: disable the blog plugin
// ...
},
],
],
};

注意,你不一定要放弃使用博客或者其他插件。设置 routeBasePath: '/' 所产生的唯一效果就是把文档从 https://example.com/docs/some-doc 移到了网站根部:https://example.com/some-doc。 如果启用了博客,还是可以通过 blog/ 子路由访问到它。

别忘了通过添加前言把某个页面放置在网站顶部 (https://example.com/):

docs/intro.md
---
slug: /
---

这一页会是用户访问 https://example.com/ 时出现的主页。
warning

如果你用 slug: / 把某篇文档变成了主页,你就需要删掉 src/pages/index.js 的现有首页,否则会有两个文件映射到同一个路径!

现在,网站的结构会长得像这样:

example.com/                       -> 生成自 `docs/intro.md`
example.com/tutorial-basics/... -> 生成自 `docs/tutorial-basics/...`
...
提示

There's also a "blog-only mode" for those who only want to use the blog feature of Docusaurus. 你可以用类似上述的方法实现。 你可以在仅博客模式一节了解如何配置。