外掛方法參考
警告
本節仍在進行中。錨點連結甚至 URL 都不能保證穩定。
外掛 API 由佈景主題和外掛共用—佈景主題的載入方式與外掛相同。
外掛模組
每個外掛都作為一個模組導入。該模組預計具有以下成員
- 預設匯出:外掛的建構函式。
- 命名匯出:在初始化外掛之前呼叫的 靜態方法。
外掛建構函式
外掛模組的預設匯出是一個建構函式,其簽章為 (context: LoadContext, options: PluginOptions) => Plugin | Promise<Plugin>
。
context
context
與外掛無關,並且相同的物件將被傳遞到用於 Docusaurus 網站的所有外掛中。 context
物件包含以下欄位
type LoadContext = {
siteDir: string;
generatedFilesDir: string;
siteConfig: DocusaurusConfig;
outDir: string;
baseUrl: string;
};
options
options
是 使用外掛時第二個可選參數。 options
是特定於外掛的,並且由使用者在 docusaurus.config.js
中使用它們時指定。如果有匯出的 validateOptions
函式,則會事先驗證和標準化 options
。
或者,如果預設集包含外掛,則預設集將負責將正確的選項傳遞到外掛中。由個別外掛來定義它需要哪些選項。
範例
這是一個假設外掛實作的心智模型。
// A JavaScript function that returns an object.
// `context` is provided by Docusaurus. Example: siteConfig can be accessed from context.
// `opts` is the user-defined options.
export default async function myPlugin(context, opts) {
return {
// A compulsory field used as the namespace for directories to cache
// the intermediate data for each plugin.
// If you're writing your own local plugin, you will want it to
// be unique in order not to potentially conflict with imported plugins.
// A good way will be to add your own project name within.
name: 'docusaurus-my-project-cool-plugin',
async loadContent() {
// The loadContent hook is executed after siteConfig and env has been loaded.
// You can return a JavaScript object that will be passed to contentLoaded hook.
},
async contentLoaded({content, actions}) {
// The contentLoaded hook is done after loadContent hook is done.
// `actions` are set of functional API provided by Docusaurus (e.g. addRoute)
},
async postBuild(props) {
// After docusaurus <build> finish.
},
// TODO
async postStart(props) {
// docusaurus <start> finish
},
// TODO
afterDevServer(app, server) {
// https://webpack.dev.org.tw/configuration/dev-server/#devserverbefore
},
// TODO
beforeDevServer(app, server) {
// https://webpack.dev.org.tw/configuration/dev-server/#devserverafter
},
configureWebpack(config, isServer, utils, content) {
// Modify internal webpack config. If returned value is an Object, it
// will be merged into the final config using webpack-merge;
// If the returned value is a function, it will receive the config as the 1st argument and an isServer flag as the 2nd argument.
},
getPathsToWatch() {
// Paths to watch.
},
getThemePath() {
// Returns the path to the directory where the theme components can
// be found.
},
getClientModules() {
// Return an array of paths to the modules that are to be imported
// in the client bundle. These modules are imported globally before
// React even renders the initial UI.
},
extendCli(cli) {
// Register an extra command to enhance the CLI of Docusaurus
},
injectHtmlTags({content}) {
// Inject head and/or body HTML tags.
},
async getTranslationFiles({content}) {
// Return translation files
},
translateContent({content, translationFiles}) {
// translate the plugin content here
},
translateThemeConfig({themeConfig, translationFiles}) {
// translate the site themeConfig here
},
async getDefaultCodeTranslationMessages() {
// return default theme translations here
},
};
}
export function validateOptions({options, validate}) {
const validatedOptions = validate(myValidationSchema, options);
return validatedOptions;
}
export function validateThemeConfig({themeConfig, validate}) {
const validatedThemeConfig = validate(myValidationSchema, options);
return validatedThemeConfig;
}