Plugin API

Plugins should be used before initialization. The basic options will be handled once the plugin is used:

The following hooks will be processed when initializing app:

The following hooks will be processed when preparing files:

The following hooks will be processed in dev / build:

Basic Options

name

  • Type: string

  • Details:

    Name of the plugin.

    It will be used for identifying plugins to avoid using a same plugin multiple times, so make sure to use a unique plugin name.

    It should follow the naming convention:

    • Non-scoped: vuepress-plugin-foo
    • Scoped: @org/vuepress-plugin-foo
  • Also see:

multiple

  • Type: boolean

  • Default: false

  • Details:

    Declare whether the plugin can be used multiple times.

    If set to false, when using plugins with the same name, the one used previously will be replaced by the one used later.

    If set to true, plugins with the same name could be used multiple times and won’t be replaced.

  • Also see:

plugins

  • Type: PluginConfig[]

  • Details:

    Plugins to use.

    A plugin can use other plugins via this option.

    This option accepts an array, each item of which is a two-element tuple:

    • The first element is the plugin name or the plugin itself. It accepts plugin name, plugin name shorthand, absolute path to plugin, or the plugin object.
    • The second element is the plugin options. It accepts boolean or object. Set it to false to disable the plugin. Set it to true to enable the plugin without any options. Use object to enable the plugin with options.

    For simplicity, you can use the first element of the tuple that described above as the array item, which equals enabling the plugin without any options.

  • Example:

  1. module.exports = {
  2. plugins: [
  3. // two-element tuple
  4. ['vuepress-plugin-foo', false],
  5. ['bar', true],
  6. [path.resolve(__dirname, './path/to/local/plugin'), { /* options */ }],
  7. [require('vuepress-plugin-baz'), true],
  8. // only use the first element
  9. 'foobar', // equals to ['foobar', true]
  10. ],
  11. }

Development Hooks

alias

  • Type: Record<string, any> | ((app: App) => Record<string, any>)

  • Details:

    Path aliases definition.

    This hook accepts an object or a function that returns an object.

  • Example:

  1. module.exports = {
  2. alias: {
  3. '@alias': path.resolve(__dirname, './path/to/alias'),
  4. },
  5. }

define

  • Type: Record<string, any> | ((app: App) => Record<string, any>)

  • Details:

    Define global constants replacements.

    This hook accepts an object or a function that returns an object.

    This can be useful for passing variables to client files. Note that the values will be automatically processed by JSON.stringify().

  • Example:

  1. module.exports = {
  2. define: {
  3. __GLOBAL_BOOLEAN__: true,
  4. __GLOBAL_STRING__: 'foobar',
  5. __GLOBAL_OBJECT__: { foo: 'bar' },
  6. },
  7. }

extendsMarkdown

  • Type: (md: Markdown, app: App) => void | Promise<void>

  • Details:

    Markdown enhancement.

    This hook accepts a function that will receive an instance of Markdown powered by markdown-itPlugin API - 图1open in new window in its arguments.

    This can be used for using extra markdown-it plugins and implementing customizations.

  • Example:

  1. module.exports = {
  2. extendsMarkdown: (md) => {
  3. md.use(plugin1)
  4. md.linkify.set({ fuzzyEmail: false })
  5. },
  6. }

extendsPageOptions

  • Type: (filePath: string, app: App) => PageOptions | Promise<PageOptions>

  • Details:

    Page options extension.

    This hook accepts a function that will receive the relative file path of the page. The returned object will be merged into page options, which will be used to create the page.

  • Example:

Set permalink pattern for pages in _posts directory:

  1. module.exports = {
  2. extendsPageOptions: (filePath) => {
  3. if (filePath.startsWith('_posts/')) {
  4. return {
  5. frontmatter: {
  6. permalinkPattern: '/:year/:month/:day/:slug.html',
  7. },
  8. }
  9. }
  10. return {}
  11. },
  12. }

extendsPageData

  • Type: (page: Page, app: App) => Record<string, any> | Promise<Record<string, any>>

  • Details:

    Page data extension.

    This hook accepts a function that will receive an instance of Page. The returned object will be merged into page data, which can be used in client side code.

  • Example:

  1. module.exports = {
  2. extendsPageData: (page) => {
  3. const meta = 'foobar'
  4. return { meta }
  5. },
  6. }

In client component:

  1. import { usePageData } from '@vuepress/client'
  2. export default {
  3. setup() {
  4. const page = usePageData()
  5. console.log(page.value.meta) // foobar
  6. },
  7. }

Client Files Hooks

clientAppEnhanceFiles

  • Type: string | string[] | ((app: App) => string | string[] | Promise<string | string[]>)

  • Details:

    Paths of client app enhancement files.

    This hook accepts absolute file paths, or a function that returns the paths.

    Files listed in this hook will be invoked after the client app is created to make some enhancement to it.

  • Example:

  1. module.exports = {
  2. clientAppEnhanceFiles: path.resolve(__dirname, './path/to/clientAppEnhance.js'),
  3. }

clientAppRootComponentFiles

  • Type: string | string[] | ((app: App) => string | string[] | Promise<string | string[]>)

  • Details:

    Paths of client app root component files.

    This hook accepts absolute file paths, or a function that returns the paths.

    Components listed in this hook will be rendered to the root node of the client app.

  • Example:

  1. module.exports = {
  2. clientAppRootComponentFiles: path.resolve(__dirname, './path/to/RootComponent.vue'),
  3. }

clientAppSetupFiles

  • Type: string | string[] | ((app: App) => string | string[] | Promise<string | string[]>)

  • Details:

    Paths of client app setup files.

    This hook accepts absolute file paths, or a function that returns the paths.

    Files listed in this hook will be invoked in the setupPlugin API - 图2open in new window function of the client app.

  • Example:

  1. module.exports = {
  2. clientAppSetupFiles: path.resolve(__dirname, './path/to/clientAppSetup.js'),
  3. }

Lifecycle Hooks

onInitialized

  • Type: (app: App) => void | Promise<void>

  • Details:

    This hook will be invoked once VuePress app has been initialized.

onPrepared

  • Type: (app: App) => void | Promise<void>

  • Details:

    This hook will be invoked once VuePress app has finished preparation.

onWatched

  • Type: (app: App, watchers: Closable[], restart: () => Promise<void>) => void | Promise<void>

  • Details:

    This hook will be invoked once VuePress app has started dev-server and watched files change.

    The watchers is an array of file watchers. When changing config file, the dev command will be restarted and those watchers will be closed. If you are adding new watchers in this hook, you should push your watchers to the watchers array, so that they can be closed correctly when restarting.

    The restart is a method to restart the dev command. When calling this method, the watchers array will be closed automatically.

onGenerated

  • Type: (app: App) => void | Promise<void>

  • Details:

    This hook will be invoked once VuePress app has generated static files.