跳到主要内容
版本:3.8.1

📦 plugin-content-docs(文档内容插件)

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

安装

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

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

您可以通过 预设选项来配置此插件。

配置

接受的字段

名称类型默认值描述
pathstring'docs'文档内容目录在文件系统中的路径,相对于站点目录。
editUrlstring | EditUrlFunctionundefined编辑站点的基本 URL。最终 URL 通过 editUrl + relativeDocPath 计算。使用函数可以对每个文件进行更精细的控制。完全省略此变量将禁用编辑链接。
editLocalizedFilesbooleanfalse编辑 URL 将指向本地化文件,而不是原始未本地化文件。当 editUrl 是函数时忽略。
editCurrentVersionbooleanfalse编辑 URL 将始终指向当前版本的文档,而不是旧版本。当 editUrl 是函数时忽略。
routeBasePathstring'docs'站点文档部分的 URL 路由。请勿包含尾部斜杠。使用 / 表示不带基本路径的文档。
tagsBasePathstring'tags'站点标签列表页面的 URL 路由。它会预置到 routeBasePath 之前。
includestring[]['**/*.{md,mdx}']Glob 模式数组,匹配要构建的 Markdown 文件,相对于内容路径。
excludestring[]参见示例配置Glob 模式数组,匹配要排除的 Markdown 文件。作为 include 选项的细化。
sidebarPathfalse | stringundefined侧边栏配置文件的路径,在 Node.js 上下文中加载。使用 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 的 Remark 插件。
rehypePluginsany[][]传递给 MDX 的 Rehype 插件。
rehypePluginsany[][]传递给 MDX 的 Recma 插件。
beforeDefaultRemarkPluginsany[][]在 Docusaurus 默认 Remark 插件之前传递给 MDX 的自定义 Remark 插件。
beforeDefaultRehypePluginsany[][]在 Docusaurus 默认 Rehype 插件之前传递给 MDX 的自定义 Rehype 插件。
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 中)时插件的行为。

类型

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.org.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};

示例配置

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

提示

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

如果您使用预设,请通过 预设选项配置此插件

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 Front Matter

Markdown 文档可以使用以下 Markdown Front Matter 元数据字段,两侧用 --- 分隔。

接受的字段

名称类型默认值描述
idstring文件路径(包括文件夹,不带扩展名)唯一的文档 ID。
titlestringMarkdown 标题或 id文档的文本标题。用于页面元数据,并作为多处(侧边栏、下一页/上一页按钮等)的备用值。如果文档不包含任何 Markdown 标题,则会自动添加到文档顶部。
pagination_labelstringsidebar_labeltitle用于此文档的下一页/上一页按钮中的文本。
displayed_sidebarstringundefined浏览当前文档时强制显示给定侧边栏。详情请阅读多侧边栏指南
hide_titlebooleanfalse是否隐藏文档顶部的标题。它只隐藏通过 Front Matter 声明的标题,对文档顶部的 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文档页面用于搜索引擎的关键字元标签。
描述stringMarkdown 内容的第一行文档的描述,将成为 <head> 中的 <meta name="description" content="..."/><meta property="og:description" content="..."/>,供搜索引擎使用。
imagestringundefined封面或缩略图,将用作 <head> 中的 <meta property="og:image" content="..."/>,增强社交媒体和消息平台上的链接预览。
slugstring文件路径允许自定义文档 URL (/<routeBasePath>/<slug>)。支持多种模式:slug: my-doc, slug: /my/path/myDoc, slug: /
tagsTag[]undefined字符串列表或包含两个字符串字段 labelpermalink 的对象,用于标记您的文档。字符串可以引用标签文件(通常是 tags.yml)的键
draftbooleanfalse草稿文档仅在开发期间可用。
unlistedbooleanfalse未列出的文档在开发和生产环境中都可用。它们在生产环境中将“隐藏”,不被索引,不包含在站点地图中,并且只能通过拥有直接链接的用户访问。
last_updateFrontMatterLastUpdateundefined允许覆盖最后更新的作者/日期。日期可以是任何可解析的日期字符串
type FrontMatterLastUpdate = {date?: string; author?: string};

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

示例

---
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 插件选项配置 YAML 标签文件的路径。

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

此文件可以包含预定义标签的列表。您可以通过tags front matter在 Markdown 文件中通过其键引用这些标签。

保持标签一致性

使用标签文件,您可以确保标签在整个插件内容集中保持一致。使用onInlineTags: 'throw'插件选项可以强制执行此一致性并防止使用即时声明的内联标签。

类型

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

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>;

示例

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 简介

翻译文件位置

  • 基本路径website/i18n/[locale]/docusaurus-plugin-content-docs
  • 多实例路径website/i18n/[locale]/docusaurus-plugin-content-docs-[pluginId]
  • JSON 文件:使用 docusaurus write-translations 提取
  • Markdown 文件website/i18n/[locale]/docusaurus-plugin-content-docs/[versionName]

文件系统结构示例

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