Skip to main content
Version: 3.5.1

📦 plugin-content-docs

提供 文档 功能,是 Docusaurus 的默认文档插件。

¥Provides the Docs functionality and is the default docs plugin for Docusaurus.

安装

¥Installation

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

如果你使用预设 @docusaurus/preset-classic,则无需安装此插件作为依赖。

¥If you use the preset @docusaurus/preset-classic, you don't need to install this plugin as a dependency.

你可以通过 预设选项.conf 配置该插件。

¥You can configure this plugin through the preset options.

配置

¥Configuration

接受的字段:

¥Accepted fields:

名称类型默认描述
pathstring'docs'文件系统上文档内容目录的路径,相对于站点目录。
editUrlstring | EditUrlFunctionundefined用于编辑站点的基本 URL。最终 URL 由 editUrl + relativeDocPath 计算。使用函数可以对每个文件进行更细致的控制。完全省略此变量将禁用编辑链接。
editLocalizedFilesbooleanfalse编辑 URL 将定位本地化文件,而不是原始的未本地化文件。当 editUrl 是函数时被忽略。
editCurrentVersionbooleanfalse编辑 URL 将始终针对当前版本文档而不是旧版本。当 editUrl 是函数时被忽略。
routeBasePathstring'docs'你网站的文档部分的 URL 路由。不要包含尾部斜杠。使用 / 发送没有基本路径的文档。
tagsBasePathstring'tags'你网站的标签列表页面的 URL 路由。它位于 routeBasePath 之前。
includestring[]['**/*.{md,mdx}']与要构建的 Markdown 文件相匹配的 glob 模式数组,相对于内容路径。
excludestring[]查看示例配置与要排除的 Markdown 文件匹配的 glob 模式数组。用作基于 include 选项的细化。
sidebarPathfalse | stringundefined侧边栏配置的路径。使用 false 禁用侧边栏,或使用 undefined 创建完全自动生成的侧边栏。
sidebarCollapsiblebooleantrue侧边栏类别默认情况下是否可折叠。另见 可折叠类别
sidebarCollapsedbooleantrue侧边栏类别是否默认折叠。另见 默认扩展类别
sidebarItemsGeneratorSidebarGenerator省略用于将 'autogenerated' 类型的侧边栏项目替换为真实侧边栏项目(文档、类别、链接...)的函数。另见 自定义侧边栏项目生成器
numberPrefixParserboolean | PrefixParser省略自定义解析逻辑从文件名中提取数字前缀。使用 false 禁用此行为并保持文档不变,使用 true 使用默认解析器。另见 使用数字前缀
docsRootComponentstring'@theme/DocsRoot'所有文档插件页面(包括所有版本)的父组件。在文档页面和版本之间导航时保持安装状态。
docVersionRootComponentstring'@theme/DocVersionLayout'单个版本的所有文档页面的父组件(带有侧边栏的文档页面、标签页面)。在该特定版本的页面之间导航时保持安装状态。
docRootComponentstring'@theme/DocRoot'所有带有侧边栏的文档页面的父组件(常规文档页面、类别生成的索引页面)。在此类页面之间导航时保持安装状态。
docItemComponentstring'@theme/DocItem'主文档容器,包含目录、分页等。
docTagsListComponentstring'@theme/DocTagsListPage'标签列表页面的根组件
docTagDocListComponentstring'@theme/DocTagDocListPage'"包含标签 X 的文档" 页面的根组件。
docCategoryGeneratedIndexComponentstring'@theme/DocCategoryGeneratedIndexPage'生成的类别索引页的根组件。
remarkPluginsany[][]备注传递给 MDX 的插件。
rehypePluginsany[][]Rehype 插件传递给 MDX。
rehypePluginsany[][]传递给 MDX 的 Recma 插件。
beforeDefaultRemarkPluginsany[][]自定义 Remark 插件在默认 Docusaurus Remark 插件之前传递到 MDX。
beforeDefaultRehypePluginsany[][]自定义 Rehype 插件在默认 Docusaurus Rehype 插件之前传递到 MDX。
showLastUpdateAuthorbooleanfalse是否显示最后更新文档的作者。
showLastUpdateTimebooleanfalse是否显示文档的最后更新日期。这需要在构建期间访问 git 历史记录,因此无法与浅克隆(CI 系统的常见默认设置)一起正常工作。使用 GitHub actions/checkout,使用 fetch-depth: 0
disableVersioningbooleanfalse即使存在多个版本,也应显式禁用版本控制。这将使该网站仅包含当前版本。如果 includeCurrentVersion: falsedisableVersioning: true 将会出错。
includeCurrentVersionbooleantrue包括文档的当前版本。
lastVersionstringversions.json 年的第一个版本文档导航栏项目优先导航到并默认显示的版本。
onlyIncludeVersionsstring[]所有版本均可用仅包含所有可用版本的子集。
versionsVersionsConfig{}独立定制每个版本的属性。
tagsstring | false | null | undefinedtags.yml列出预定义标签的 YAML 文件的路径。相对于文档版本内容目录。
onInlineTags'ignore' | 'log' | 'warn' | 'throw'warn当文档包含内联标签(未出现在预定义标签列表中,通常为 docs/tags.yml)时的插件行为。

类型

¥Types

EditUrlFunction

type EditUrlFunction = (params: {
version: string;
versionDocsDirPath: string;
docPath: string;
permalink: string;
locale: string;
}) => string | undefined;

PrefixParser

type PrefixParser = (filename: string) => {
filename: string;
numberPrefix?: number;
};

SidebarGenerator

type SidebarGenerator = (generatorArgs: {
/** The sidebar item with type "autogenerated" to be transformed. */
item: {type: 'autogenerated'; dirName: string};
/** Useful metadata for the version this sidebar belongs to. */
version: {contentPath: string; versionName: string};
/** All the docs of that version (unfiltered). */
docs: {
id: string;
title: string;
frontMatter: DocFrontMatter & Record<string, unknown>;
source: string;
sourceDirName: string;
sidebarPosition?: number | undefined;
}[];
/** Number prefix parser configured for this plugin. */
numberPrefixParser: PrefixParser;
/** The default category index matcher which you can override. */
isCategoryIndex: CategoryIndexMatcher;
/**

* key is the path relative to the doc content directory, value is the

* category metadata file's content.
*/
categoriesMetadata: {[filePath: string]: CategoryMetadata};
/**

* Useful to re-use/enhance the default sidebar generation logic from

* Docusaurus.
*/
defaultSidebarItemsGenerator: SidebarGenerator;
// Returns an array of sidebar items — same as what you can declare in
// sidebars.js, except for shorthands. See https://docusaurus.nodejs.cn/docs/sidebar/items
}) => Promise<SidebarItem[]>;

type CategoryIndexMatcher = (param: {
/** The file name, without extension */
fileName: string;
/**

* The list of directories, from lowest level to highest.

* If there's no dir name, directories is ['.']
*/
directories: string[];
/** The extension, with a leading dot */
extension: string;
}) => boolean;

VersionsConfig

type VersionConfig = {
/**

* The base path of the version, will be appended to `baseUrl` +

* `routeBasePath`.
*/
path?: string;
/** The label of the version to be used in badges, dropdowns, etc. */
label?: string;
/** The banner to show at the top of a doc of that version. */
banner?: 'none' | 'unreleased' | 'unmaintained';
/** Show a badge with the version label at the top of each doc. */
badge?: boolean;
/** Prevents search engines from indexing this version */
noIndex?: boolean;
/** Add a custom class name to the <html> element of each doc */
className?: string;
};

type VersionsConfig = {[versionName: string]: VersionConfig};

配置示例

¥Example configuration

你可以通过预设选项或插件选项配置此插件。

¥You can configure this plugin through preset options or plugin options.

提示

大多数 Docusaurus 用户通过预设选项配置此插件。

¥Most Docusaurus users configure this plugin through the preset options.

If you use a preset, configure this plugin through the preset options:

docusaurus.config.js
module.exports = {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
path: 'docs',
breadcrumbs: true,
// Simple use-case: string editUrl
// editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/',
// Advanced use-case: functional editUrl
editUrl: ({versionDocsDirPath, docPath}) =>
`https://github.com/facebook/docusaurus/edit/main/website/${versionDocsDirPath}/${docPath}`,
editLocalizedFiles: false,
editCurrentVersion: false,
routeBasePath: 'docs',
include: ['**/*.md', '**/*.mdx'],
exclude: [
'**/_*.{js,jsx,ts,tsx,md,mdx}',
'**/_*/**',
'**/*.test.{js,jsx,ts,tsx}',
'**/__tests__/**',
],
sidebarPath: 'sidebars.js',
async sidebarItemsGenerator({
defaultSidebarItemsGenerator,
numberPrefixParser,
item,
version,
docs,
isCategoryIndex,
}) {
// Use the provided data to generate a custom sidebar slice
return [
{type: 'doc', id: 'intro'},
{
type: 'category',
label: 'Tutorials',
items: [
{type: 'doc', id: 'tutorial1'},
{type: 'doc', id: 'tutorial2'},
],
},
];
},
numberPrefixParser(filename) {
// Implement your own logic to extract a potential number prefix
const numberPrefix = findNumberPrefix(filename);
// Prefix found: return it with the cleaned filename
if (numberPrefix) {
return {
numberPrefix,
filename: filename.replace(prefix, ''),
};
}
// No number prefix found
return {numberPrefix: undefined, filename};
},
docsRootComponent: '@theme/DocsRoot',
docVersionRootComponent: '@theme/DocVersionRoot',
docRootComponent: '@theme/DocRoot',
docItemComponent: '@theme/DocItem',
remarkPlugins: [require('./my-remark-plugin')],
rehypePlugins: [],
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [],
showLastUpdateAuthor: false,
showLastUpdateTime: false,
disableVersioning: false,
includeCurrentVersion: true,
lastVersion: undefined,
versions: {
current: {
label: 'Android SDK v2.0.0 (WIP)',
path: 'android-2.0.0',
banner: 'none',
},
'1.0.0': {
label: 'Android SDK v1.0.0',
path: 'android-1.0.0',
banner: 'unmaintained',
},
},
onlyIncludeVersions: ['current', '1.0.0', '2.0.0'],
},
},
],
],
};

Markdown 前言

¥Markdown front matter

Markdown 文档可以使用以下 Markdown 头条新闻 元数据字段,两侧用 --- 行括起来。

¥Markdown documents can use the following Markdown front matter metadata fields, enclosed by a line --- on either side.

接受的字段:

¥Accepted fields:

名称类型默认描述
idstring文件路径(包括文件夹,不带扩展名)唯一的文档 ID。
titlestringMarkdown 标题或 id文档的文本标题。用于页面元数据并作为多个位置的后备值(侧边栏、下一个/上一个按钮...)。如果文档不包含任何 Markdown 标题,则会自动添加到文档顶部。
pagination_labelstringsidebar_labeltitle本文档的下一个/上一个按钮中使用的文本。
displayed_sidebarstringundefined浏览当前文档时强制显示给定的侧边栏。详细信息请阅读 多个侧边栏指南
hide_titlebooleanfalse是否隐藏文档顶部的标题。它仅隐藏通过前面内容声明的标题,并且对文档顶部的 Markdown 标题没有影响。
hide_table_of_contentsbooleanfalse是否隐藏右侧目录。
toc_min_heading_levelnumber2目录中显示的最低标题级别。必须介于 2 到 6 之间且小于或等于最大值。
toc_max_heading_levelnumber3目录中显示的最大标题级别。必须在 2 到 6 之间。
pagination_nextstring | null侧边栏中的下一个文档你希望 "下一个" 分页链接到的文档的 ID。使用 null 禁止显示该页面的 "下一个"。
pagination_prevstring | null侧边栏中的上一个文档你希望 "以前的" 分页链接到的文档的 ID。使用 null 禁止显示该页面的 "以前的"。
parse_number_prefixesbooleannumberPrefixParser 插件选项此文档是否禁用数字前缀解析。另见 使用数字前缀
custom_edit_urlstring | null使用 editUrl 插件选项计算用于编辑此文档的 URL。使用 null 禁止显示该页面的 "编辑这个页面"。
keywordsstring[]undefined文档页面的关键字元标记,用于搜索引擎。
descriptionstring第一行 Markdown 内容你的文档的描述,将成为 <head> 中的 <meta name="description" content="..."/><meta property="og:description" content="..."/>,供搜索引擎使用。
imagestringundefined将用作 <head> 中的 <meta property="og:image" content="..."/> 的封面或缩略图,增强社交媒体和消息平台上的链接预览。
slugstring文件路径允许自定义文档 URL (/<routeBasePath>/<slug>)。支持多种模式:slug: my-docslug: /my/path/myDocslug: /
tagsTag[]undefined要标记到你的文档的两个字符串字段 labelpermalink 的字符串或对象的列表。字符串可以是对 标签文件(通常是 tags.yml)的键的引用
draftbooleanfalse草稿文件仅在开发期间可用。
unlistedbooleanfalse未列出的文件将在开发和生产过程中提供。它们在生产中将是 "hidden",未编入索引,从站点地图中排除,并且只能由具有直接链接的用户访问。
last_updateFrontMatterLastUpdateundefined允许覆盖上次更新作者/日期。日期可以是任意 可解析的日期字符串
type FrontMatterLastUpdate = {date?: string; author?: string};

type Tag = string | {label: string; permalink: string};

示例:

¥Example:

---
id: doc-markdown
title: Docs Markdown Features
hide_title: false
hide_table_of_contents: false
sidebar_label: Markdown
sidebar_position: 3
pagination_label: Markdown features
custom_edit_url: https://github.com/facebook/docusaurus/edit/main/docs/api-doc-markdown.md
description: How do I find you when I cannot solve this problem
keywords:
- docs
- docusaurus
tags: [docusaurus]
image: https://i.imgur.com/mErPwqL.png
slug: /myDoc
last_update:
date: 1/1/2000
author: custom author name
---

# Markdown Features

My Document Markdown content

标签文件

¥Tags File

使用 tags 插件选项 配置 YAML 标签文件的路径。

¥Use the tags plugin option to configure the path of a YAML tags file.

按照惯例,插件将在你的内容文件夹的根目录中查找 tags.yml 文件。

¥By convention, the plugin will look for a tags.yml file at the root of your content folder(s).

此文件可以包含预定义标签列表。借助 tags 前言,你可以在 Markdown 文件中通过它们的键引用这些标签。

¥This file can contain a list of predefined tags. You can reference these tags by their keys in Markdown files thanks to the tags front matter.

保持标签一致

使用标签文件,你可以确保你的标签使用在整个插件内容集中保持一致。使用 onInlineTags: 'throw' 插件选项来强制这种一致性并防止使用动态声明的内联标签。

¥Using a tags file, you can ensure that your tags usage is consistent across your plugin content set. Use the onInlineTags: 'throw' plugin option to enforce this consistency and prevent usage of inline tags declared on the fly.

类型

¥Types

提供的标签文件的 YAML 内容应遵循以下形状:

¥The YAML content of the provided tags file should respect the following shape:

type Tag = {
label?: string; // Tag display label
permalink?: string; // Tag URL pathname segment
description?: string; // Tag description displayed in the tag page
};

type TagsFileInput = Record<string, Partial<Tag> | null>;

示例

¥Example

tags.yml
releases:
label: 'Product releases'
permalink: '/product-releases'
description: 'Content related to product releases.'

# A partial tag definition is also valid
announcements:
label: 'Announcements'

# An empty tag definition is also valid
# Other attributes will be inferred from the key
emptyTag:
content.md
---
tags: [releases, announcements, emptyTag]
---

# Title

Content

国际化

¥i18n

首先阅读 国际化介绍

¥Read the i18n introduction first.

翻译文件位置

¥Translation files location

  • 基本路径:website/i18n/[locale]/docusaurus-plugin-content-docs

    ¥Base path: website/i18n/[locale]/docusaurus-plugin-content-docs

  • 多实例路径:website/i18n/[locale]/docusaurus-plugin-content-docs-[pluginId]

    ¥Multi-instance path: website/i18n/[locale]/docusaurus-plugin-content-docs-[pluginId]

  • JSON 文件:用 docusaurus write-translations 提取

    ¥JSON files: extracted with docusaurus write-translations

  • Markdown 文件:website/i18n/[locale]/docusaurus-plugin-content-docs/[versionName]

    ¥Markdown files: website/i18n/[locale]/docusaurus-plugin-content-docs/[versionName]

文件系统结构示例

¥Example file-system structure

website/i18n/[locale]/docusaurus-plugin-content-docs

# translations for website/docs
├── current
│ ├── api
│ │ └── config.md
│ └── getting-started.md
├── current.json

# translations for website/versioned_docs/version-1.0.0
├── version-1.0.0
│ ├── api
│ │ └── config.md
│ └── getting-started.md
└── version-1.0.0.json