简体中文
插件配置
主题是布局层:导航栏、侧边栏、文档页面、首页和未找到页面由主题提供,而布局之外的所有功能都由插件提供。主题会以合理的默认值替你接入这些插件,因此典型站点从不需要自己安装或配置它们。
主题数据插件始终启用且不可配置,因为整个主题配置都通过它传给客户端。其他每个插件都可以在 themePlugins 中开关。
内置插件
| 插件 | themePlugins 键 | 默认值 | 说明 |
|---|---|---|---|
@vuepress/plugin-theme-data | — | 启用 | 始终启用。 |
@vuepress/plugin-copy-code | copyCode | true | 作用范围限定为页面内容中的代码块。 |
@vuepress/plugin-git | git | true | 收集最后更新时间和贡献者。 |
@vuepress/plugin-shiki | shiki | true | github-light 和 github-dark 主题。 |
@vuepress/plugin-markdown-ext | markdownExt | true | v-pre 容器。 |
@vuepress/plugin-markdown-raw | markdownRaw | true | raw 容器,带样式隔离。 |
@vuepress/plugin-markdown-hint | markdownHint | true | 提示容器和 GFM 警告。 |
@vuepress/plugin-markdown-tab | markdownTab | true | 代码标签页。其他标签页分组需要手动开启。 |
@vuepress/plugin-active-header-links | activeHeaderLinks | true | 让 URL 哈希与大纲保持同步。 |
@vuepress/plugin-back-to-top | backToTop | true | 返回顶部按钮。 |
@vuepress/plugin-links-check | linksCheck | true | 死链检测,使用插件默认配置。 |
@vuepress/plugin-icon | icon | true | 运行时加载的 Iconify 资源。 |
@vuepress/plugin-medium-zoom | mediumZoom | true | 文档图片的图片缩放。 |
@vuepress/plugin-nprogress | nprogress | true | 导航时的进度条。 |
@vuepress/plugin-seo | seo | true | 仅在设置了 hostname 时接入。 |
@vuepress/plugin-sitemap | sitemap | true | 仅在设置了 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.copyCode
CopyCodePluginOptions | 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.git
GitPluginOptions | 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.shiki
ShikiPluginOptions | 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.markdownExt
MarkdownExtPluginOptions | boolean默认值
true接入 Markdown Ext,但只启用 v-pre 容器,即 { vPre: true }。
该插件的其他 Markdown 扩展需要手动开启。gfm: true 会启用 GFM 支持 —— 脚注、任务列表、软换行和自动链接的 URL:
themePlugins: {
markdownExt: { gfm: true },
}themePlugins.markdownRaw
MarkdownRawPluginOptions | boolean默认值
true接入 Markdown Raw,并设置 isolateStyles: true,从而让主题样式与原始内容隔离。
themePlugins.markdownHint
MarkdownHintPluginOptions | boolean默认值
true接入 Markdown Hint,设置 hint: true 和 alert: true,并传入主题配置项中的本地化标签。
themePlugins: {
// disable the GFM alerts, keep the containers
markdownHint: { alert: false },
}themePlugins.markdownTab
MarkdownTabPluginOptions | boolean默认值
true接入 Markdown Tab,并设置 codeTabs: true。代码标签页开箱即用;通用的 ::: tabs 分组需要手动开启:
themePlugins: {
markdownTab: { tabs: true },
}themePlugins.activeHeaderLinks
boolean默认值
true接入 Active Header Links,并设置 headerLinkSelector: '.outline-link',从而在滚动时让 URL 哈希跟随大纲。只接受布尔值:选择器由主题固定,因为它与大纲的标记绑定。
themePlugins.backToTop
BackToTopPluginOptions | boolean默认值
true接入 返回顶部。按钮的标签来自插件的语言配置。
themePlugins: {
// show the button after 200px of scrolling, with a progress bar
backToTop: { threshold: 200, progress: true },
}themePlugins.linksCheck
LinksCheckPluginOptions | boolean默认值
true接入链接检查,使用插件默认配置。对象就是插件配置项:
themePlugins: {
linksCheck: {
// do not check the anchor links in development
dev: false,
// fail the build on a dead link
build: 'error',
},
}themePlugins.icon
IconPluginOptions | 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.mediumZoom
MediumZoomPluginOptions | boolean默认值
true接入 Medium Zoom。
themePlugins: {
mediumZoom: { selector: '.vp-doc img' },
}themePlugins.seo
Partial<SeoPluginOptions> | boolean默认值
true搜索
主题不提供搜索实现。只要搜索插件注册了搜索框,导航栏就会渲染 SearchBox 占位符,而大纲、侧边栏和键盘快捷键仍由插件负责。
import { slimsearchPlugin } from '@vuepress/plugin-slimsearch'
export default defineUserConfig({
theme: defaultTheme({}),
plugins: [slimsearchPlugin()],
})可用的搜索插件有 SlimSearch、DocSearch、Orama、MeiliSearch、FlexSearch 以及旧版的 Search。
