跳到正文

插件配置

主题是布局层:导航栏、侧边栏、文档页面、首页和未找到页面由主题提供,而布局之外的所有功能都由插件提供。主题会以合理的默认值替你接入这些插件,因此典型站点从不需要自己安装或配置它们。

主题数据插件始终启用且不可配置,因为整个主题配置都通过它传给客户端。其他每个插件都可以在 themePlugins 中开关。

内置插件

插件themePlugins 键默认值说明
@vuepress/plugin-theme-data—启用始终启用。
@vuepress/plugin-copy-codecopyCodetrue作用范围限定为页面内容中的代码块。
@vuepress/plugin-gitgittrue收集最后更新时间和贡献者。
@vuepress/plugin-shikishikitruegithub-light 和 github-dark 主题。
@vuepress/plugin-markdown-extmarkdownExttruev-pre 容器。
@vuepress/plugin-markdown-rawmarkdownRawtrueraw 容器,带样式隔离。
@vuepress/plugin-markdown-hintmarkdownHinttrue提示容器和 GFM 警告。
@vuepress/plugin-markdown-tabmarkdownTabtrue代码标签页。其他标签页分组需要手动开启。
@vuepress/plugin-active-header-linksactiveHeaderLinkstrue让 URL 哈希与大纲保持同步。
@vuepress/plugin-back-to-topbackToToptrue返回顶部按钮。
@vuepress/plugin-links-checklinksChecktrue死链检测,使用插件默认配置。
@vuepress/plugin-iconicontrue运行时加载的 Iconify 资源。
@vuepress/plugin-medium-zoommediumZoomtrue文档图片的图片缩放。
@vuepress/plugin-nprogressnprogresstrue导航时的进度条。
@vuepress/plugin-seoseotrue仅在设置了 hostname 时接入。
@vuepress/plugin-sitemapsitemaptrue仅在设置了 hostname 时接入。

搜索并未内置。导航栏会渲染一个 SearchBox 占位符,由搜索插件填充:SlimSearch 提供本地索引,DocSearch 提供 Algolia 搜索。见 搜索。

配置插件

themePlugins 的条目接受:

  • true(或不写)—— 主题用自己的默认值启用该插件。
  • false —— 完全不接入该插件,因此它的功能和客户端资源都不会加载。
  • 一个对象 —— 启用该插件,该对象会合并到主题传入的默认值之上。
import { defaultTheme } from '@vuepress/theme-default'
import { defineUserConfig } from 'vuepress'

export default defineUserConfig({
  theme: defaultTheme({
    themePlugins: {
      copyCode: { showInMobile: true },
      mediumZoom: { selector: '.vp-doc img' },
      icon: false,
    },
  }),
})

有些插件是由主题配置项本身启用的,而不是由 themePlugins 条目启用的:

  • git 会从 lastUpdated 和 contributors 接收 updatedTime 和 contributors。当所有语言都禁用贡献者时,git 插件不会收集它们。
  • markdownHint 会从 tip、warning、danger、important 和 note 接收提示容器标签。
  • seo 和 sitemap 需要 hostname。
  • shiki、activeHeaderLinks 和 copyCode 会把功能限定到主题渲染的类上,因此覆盖这些配置项可能破坏它们依赖的集成。

提示

禁用插件不会移除主题为它渲染的标记。例如 icon: false 只是表示不解析图标,因此图标名称会以文本形式保留。

themePlugins

themePlugins.copyCodeCopyCodePluginOptions | boolean
默认值true

接入复制代码。主题通过 selector: '[vp-content] div[class*="language-"] pre' 把它限定到页面内容中的代码块。

对象会合并到该选择器之上,因此它只配置行为:

themePlugins: {
  copyCode: {
    // copy the inline code on double click
    inline: true,
    // show the hint for 3000ms
    duration: 3000,
    showInMobile: true,
  },
}
themePlugins.gitGitPluginOptions | boolean
默认值true

接入 Git。主题会禁用创建时间和变更日志,并根据主题配置项启用更新时间和贡献者。

对象配置其余部分:

themePlugins: {
  git: {
    // show the avatar of the contributors
    contributors: { avatar: true },
    // collect the changelog, which the theme does not render by default
    changelog: { repoUrl: 'https://github.com/vuepress/ecosystem' },
  },
}
themePlugins.shikiShikiPluginOptions | boolean
默认值true

接入 Shiki,使用 github-light 和 github-dark 主题,主题 CSS 会同时把它们作为亮色和暗色主题使用。

传入 themes 对象可以替换它们:

themePlugins: {
  shiki: {
    themes: { light: 'vitesse-light', dark: 'vitesse-dark' },
  },
}

themePlugins.shiki: false 会让代码块不带高亮。如果改用 Prism,可以配合 @vuepress/plugin-prismjs。

themePlugins.markdownExtMarkdownExtPluginOptions | boolean
默认值true

接入 Markdown Ext,但只启用 v-pre 容器,即 { vPre: true }。

该插件的其他 Markdown 扩展需要手动开启。gfm: true 会启用 GFM 支持 —— 脚注、任务列表、软换行和自动链接的 URL:

themePlugins: {
  markdownExt: { gfm: true },
}
themePlugins.markdownRawMarkdownRawPluginOptions | boolean
默认值true

接入 Markdown Raw,并设置 isolateStyles: true,从而让主题样式与原始内容隔离。

themePlugins.markdownHintMarkdownHintPluginOptions | boolean
默认值true

接入 Markdown Hint,设置 hint: true 和 alert: true,并传入主题配置项中的本地化标签。

themePlugins: {
  // disable the GFM alerts, keep the containers
  markdownHint: { alert: false },
}
themePlugins.markdownTabMarkdownTabPluginOptions | boolean
默认值true

接入 Markdown Tab,并设置 codeTabs: true。代码标签页开箱即用;通用的 ::: tabs 分组需要手动开启:

themePlugins: {
  markdownTab: { tabs: true },
}
themePlugins.activeHeaderLinksboolean
默认值true

接入 Active Header Links,并设置 headerLinkSelector: '.outline-link',从而在滚动时让 URL 哈希跟随大纲。只接受布尔值:选择器由主题固定,因为它与大纲的标记绑定。

themePlugins.backToTopBackToTopPluginOptions | boolean
默认值true

接入 返回顶部。按钮的标签来自插件的语言配置。

themePlugins: {
  // show the button after 200px of scrolling, with a progress bar
  backToTop: { threshold: 200, progress: true },
}
themePlugins.linksCheckLinksCheckPluginOptions | boolean
默认值true

接入链接检查,使用插件默认配置。对象就是插件配置项:

themePlugins: {
  linksCheck: {
    // do not check the anchor links in development
    dev: false,
    // fail the build on a dead link
    build: 'error',
  },
}
themePlugins.iconIconPluginOptions | boolean
默认值true

接入图标,并设置 assets: 'iconify',即在运行时从 iconify CDN 解析 iconify Web Component,导航栏、侧边栏和特性图标都使用它。

themePlugins: {
  icon: {
    // bundle the icon sets instead of loading them from the CDN
    offline: true,
    // resolve `icon: 'mdi:github'` as `icon: 'github'`
    prefix: 'mdi:',
  },
}
themePlugins.mediumZoomMediumZoomPluginOptions | boolean
默认值true

接入 Medium Zoom。

themePlugins: {
  mediumZoom: { selector: '.vp-doc img' },
}
themePlugins.nprogressboolean
默认值true

接入 NProgress,即导航时显示的进度条。该插件不接受配置项,因此只接受布尔值。进度条的样式由主题 CSS 提供。

themePlugins.seoPartial<SeoPluginOptions> | boolean
默认值true

在配置了 hostname 时接入 SEO。hostname 由主题传入,对象补充其余配置:

defaultTheme({
  hostname: 'https://example.com',
  themePlugins: {
    seo: {
      author: { name: 'VuePress' },
      autoDescription: true,
    },
  },
})
themePlugins.sitemapPartial<SitemapPluginOptions> | boolean
默认值true

在配置了 hostname 时接入 Sitemap。

defaultTheme({
  hostname: 'https://example.com',
  themePlugins: {
    sitemap: { changefreq: 'weekly', excludePaths: ['/404.html'] },
  },
})

主题不提供搜索实现。只要搜索插件注册了搜索框,导航栏就会渲染 SearchBox 占位符,而大纲、侧边栏和键盘快捷键仍由插件负责。

import { slimsearchPlugin } from '@vuepress/plugin-slimsearch'

export default defineUserConfig({
  theme: defaultTheme({}),
  plugins: [slimsearchPlugin()],
})

可用的搜索插件有 SlimSearch、DocSearch、Orama、MeiliSearch、FlexSearch 以及旧版的 Search。

MIT 协议