使用插件
The Docusaurus core doesn't provide any feature of its own. All features are delegated to individual plugins: the docs feature provided by the docs plugin; the blog feature provided by the blog plugin; or individual pages provided by the pages plugin. 如果没有安装插件,站点就不会包含任何路径。
You may not need to install common plugins one-by-one though: they can be distributed as a bundle in a preset. 对于大多数用户来说,插件是通过预设选项来配置的。
We maintain a list of official plugins, but the community has also created some unofficial plugins. If you want to add any feature: autogenerating doc pages, executing custom scripts, integrating other services... be sure to check out the list: someone may have implemented it for you!
If you are feeling energetic, you can also read the plugin guide or plugin method references for how to make a plugin yourself.
Installing a plugin
插件通常为 npm 软件包,所以你可以像其他 npm 包一样安装。
- npm
- Yarn
- pnpm
npm install --save docusaurus-plugin-name
yarn add docusaurus-plugin-name
pnpm add docusaurus-plugin-name
Then you add it in your site's docusaurus.config.js
's plugins
option:
export default {
// ...
plugins: ['@docusaurus/plugin-content-pages'],
};
Docusaurus 也可以从你的本地目录加载插件,只需要像这样填写:
export default {
// ...
plugins: ['./src/plugins/docusaurus-local-plugin'],
};
路径需要是绝对的,或者相对于配置文件。
Configuring plugins
最简单的插件用法就是提供插件名称或插件路径。
如果要声明选项,需要在配置里把名称和配置对象放在一个二元组中。 这种风格通常被称作 Babel 风格。
export default {
// ...
plugins: [
[
'@docusaurus/plugin-xxx',
{
/* options */
},
],
],
};
示例:
export default {
plugins: [
// Basic usage.
'@docusaurus/plugin-debug',
// With options object (babel style)
[
'@docusaurus/plugin-sitemap',
{
changefreq: 'weekly',
},
],
],
};
Multi-instance plugins and plugin IDs
所有的 Docusaurus 内容插件都可支持多个插件实例。 For example, it may be useful to have multiple docs plugin instances or multiple blogs. It is required to assign a unique ID to each plugin instance, and by default, the plugin ID is default
.
export default {
plugins: [
[
'@docusaurus/plugin-content-docs',
{
id: 'docs-1',
// 其它可选项
},
],
[
'@docusaurus/plugin-content-docs',
{
id: 'docs-2',
// 其它可选项
},
],
],
};