Plugins
Plugins are the building blocks of features in a Docusaurus 2 site. Each plugin handles its own individual feature. Plugins may work and be distributed as part of bundle via presets.
Available plugins
We maintain a list of official plugins, but the community has also created some unofficial plugins.
Installing a plugin
A plugin is usually a npm package, so you install them like other npm packages using npm.
- npm
- Yarn
npm install --save docusaurus-plugin-name
yarn add docusaurus-plugin-name
Then you add it in your site's docusaurus.config.js
's plugins
option:
module.exports = {
// ...
plugins: ['@docusaurus/plugin-content-pages'],
};
Docusaurus can also load plugins from your local directory, you can do something like the following:
const path = require('path');
module.exports = {
// ...
plugins: [path.resolve(__dirname, '/path/to/docusaurus-local-plugin')],
};
Configuring plugins
For the most basic usage of plugins, you can provide just the plugin name or the absolute path to the plugin.
However, plugins can have options specified by wrapping the name and an options object in an array inside your config. This style is usually called Babel Style
.
module.exports = {
// ...
plugins: [
[
'@docusaurus/plugin-xxx',
{
/* options */
},
],
],
};
Example:
module.exports = {
plugins: [
// Basic usage.
'@docusaurus/plugin-google-analytics',
// With options object (babel style)
[
'@docusaurus/plugin-sitemap',
{
changefreq: 'weekly',
},
],
],
};
Multi-instance plugins and plugin ids
All Docusaurus content plugins can support multiple plugin instances.
The Docs plugin has additional multi-instance documentation
It is required to assign a unique id to each plugin instance.
By default, the plugin id is default
.
module.exports = {
plugins: [
[
'@docusaurus/plugin-xxx',
{
id: 'plugin-xxx-1',
// other options
},
],
[
'@docusaurus/plugin-xxx',
{
id: 'plugin-xxx-2',
// other options
},
],
],
};
note
At most one plugin instance can be the "default plugin instance", by omitting the id
attribute, or using id: 'default'
.
Plugins design
Docusaurus' implementation of the plugins system provides us with a convenient way to hook into the website's lifecycle to modify what goes on during development/build, which involves (but not limited to) extending the webpack config, modifying the data being loaded and creating new components to be used in a page.
Creating plugins
A plugin is a function that takes two parameters: context
and options
.
It returns a plugin instance object, containing plugin lifecycle APIs.
It can be defined as a function or a module.
Functional definition
You can use a plugin as a function, directly in the Docusaurus config file:
module.exports = {
// ...
plugins: [
function myPlugin(context, options) {
// ...
return {
name: 'my-plugin',
async loadContent() {
// ...
},
async contentLoaded({content, actions}) {
// ...
},
/* other lifecycle API */
};
},
],
};
Module definition
You can use a plugin as a module, loading it from a separate file or NPM package:
module.exports = {
// ...
plugins: [
// without options:
'./my-plugin',
// or with options:
['./my-plugin', options],
],
};
Then in the folder my-plugin
you can create an index.js such as this
module.exports = function myPlugin(context, options) {
// ...
return {
name: 'my-plugin',
async loadContent() {
/* ... */
},
async contentLoaded({content, actions}) {
/* ... */
},
/* other lifecycle API */
};
};
context
context
is plugin-agnostic, and the same object will be passed into all plugins used for a Docusaurus website. The context
object contains the following fields:
interface LoadContext {
siteDir: string;
generatedFilesDir: string;
siteConfig: DocusaurusConfig;
outDir: string;
baseUrl: string;
}
options
options
are the second optional parameter when the plugins are used. options
are plugin-specific and are specified by users when they use them in docusaurus.config.js
. Alternatively, if preset contains the plugin, the preset will then be in charge of passing the correct options into the plugin. It is up to individual plugin to define what options it takes.
Return value
The returned object value should implement the lifecycle APIs.