Skip to main content
Version: 3.5.1

静态方法

¥Static methods

静态方法不是插件实例的一部分 - 它们附加到构造函数。这些方法用于验证和规范化插件选项和主题配置,然后将其用作构造函数参数来初始化插件实例。

¥Static methods are not part of the plugin instance—they are attached to the constructor function. These methods are used to validate and normalize the plugin options and theme config, which are then used as constructor parameters to initialize the plugin instance.

validateOptions({options, validate})

返回插件的经过验证和标准化的选项。该方法在插件初始化之前调用。你必须返回选项,因为它们将在初始化期间传递给插件。

¥Returns validated and normalized options for the plugin. This method is called before the plugin is initialized. You must return the options since they will be passed to the plugin during initialization.

options

调用 validateOptions,并将 options 传递给插件进行验证和规范化。

¥validateOptions is called with options passed to plugin for validation and normalization.

validate

validateOptions 使用 validate 函数调用,该函数采用 Joi 模式和选项作为参数,返回经过验证和规范化的选项。validate 将自动处理错误和验证配置。

¥validateOptions is called with validate function which takes a Joi schema and options as the arguments, returns validated and normalized options. validate will automatically handle error and validation config.

提示

建议使用 Joi 对选项进行验证和标准化。

¥Joi is recommended for validation and normalization of options.

为了避免混合 Joi 版本,请使用 import {Joi} from '@docusaurus/utils-validation'

¥To avoid mixing Joi versions, use import {Joi} from '@docusaurus/utils-validation'

如果你不使用 Joi 进行验证,你可以在选项无效时抛出错误,并在成功时返回选项。

¥If you don't use Joi for validation you can throw an Error in case of invalid options and return options in case of success.

my-plugin/src/index.js
export default function myPlugin(context, options) {
return {
name: 'docusaurus-plugin',
// rest of methods
};
}

export function validateOptions({options, validate}) {
const validatedOptions = validate(myValidationSchema, options);
return validatedOptions;
}

validateThemeConfig({themeConfig, validate})

返回主题的经过验证和规范化的配置。

¥Return validated and normalized configuration for the theme.

themeConfig

validateThemeConfigdocusaurus.config.js 中提供的 themeConfig 一起调用,以进行验证和标准化。

¥validateThemeConfig is called with themeConfig provided in docusaurus.config.js for validation and normalization.

validate

validateThemeConfig 使用 validate 函数调用,该函数采用 Joi 模式和 themeConfig 作为参数,返回经过验证和规范化的选项。validate 将自动处理错误和验证配置。

¥validateThemeConfig is called with validate function which takes a Joi schema and themeConfig as the arguments, returns validated and normalized options. validate will automatically handle error and validation config.

提示

建议使用 Joi 来验证和标准化主题配置。

¥Joi is recommended for validation and normalization of theme config.

为了避免混合 Joi 版本,请使用 import {Joi} from '@docusaurus/utils-validation'

¥To avoid mixing Joi versions, use import {Joi} from '@docusaurus/utils-validation'

如果你不使用 Joi 进行验证,则在选项无效的情况下可能会抛出错误。

¥If you don't use Joi for validation you can throw an Error in case of invalid options.

my-theme/src/index.js
export default function myPlugin(context, options) {
return {
name: 'docusaurus-plugin',
// rest of methods
};
}

export function validateThemeConfig({themeConfig, validate}) {
const validatedThemeConfig = validate(myValidationSchema, options);
return validatedThemeConfig;
}