跳到正文

从经典主题迁移

@vuepress/theme-classic 是曾经作为 VuePress 默认主题的主题。它依然可用,也保持着自己的外观,但新的默认主题是 @vuepress/theme-default —— 一个从 VitePress 移植而来的轻量布局层。

本页把经典主题的配置项、frontmatter 和样式映射到新主题。大部分配置项为了与 VitePress 对齐而改了名,而依赖 SASS 的配置项则由 CSS 自定义属性替代。

@vuepress/theme-default

更新依赖包

替换主题的包和工厂函数的导入:

- npm install -D @vuepress/theme-classic@next
+ npm install -D @vuepress/theme-default@next
- import { themeClassic } from '@vuepress/theme-classic'
+ import { themeDefault } from '@vuepress/theme-default'

  export default defineUserConfig({
-   theme: themeClassic({ /* ... */ }),
+   theme: themeDefault({ /* ... */ }),
  })

如果你的站点仍在从旧的 @vuepress/theme-default 导入 defaultTheme,请改用 themeDefault 并安装新的包,因为该导入名已不再存在。

迁移主题配置项

通用

经典主题新主题说明
hostnamehostname相同。它会启用 SEO 插件和 sitemap 插件。
localeslocales为每个语言添加 label、lang 和 link,因为语言菜单依赖它们。
colorModeappearance见颜色模式。
logologo一个字符串,或一个 ThemeableImage 对象。
logoDarklogo: { light, dark }logo 的暗色变体。
logoAltlogo: { alt }logo 的替代文本。
homelogoLink导航栏中 logo 的链接,以及站点的首页。404 按钮使用 notFound.link。
reposocialLinkssocialLinks: [{ icon: 'github', link: 'https://github.com/example/repo' }]。
repoLabelsocialLinks[].ariaLabel仓库链接的标签。
navbarnav见导航栏。
sidebarsidebar相同的默认值('heading'),新的条目形状。
sidebarDepthsidebarDepth相同,且仅在 sidebar 为 'heading' 时使用。
externalLinkIconexternalLinkIcon相同,默认值为 false。
themePluginsthemePlugins见主题插件。

经典主题的 navbar 配置项就是新的 nav 配置项。它的条目是 NavItem 对象,接受 text、link、icon、activeMatch,如果是下拉菜单则还有 items:

经典主题新主题说明
navbar: [](默认)不写 nav两者都渲染不带条目的导航栏。
navbar: falsenav: []隐藏导航条目。导航栏本身由 navbar frontmatter 配置。
{ text, link }{ text, link }相同。
{ text, children }{ text, items }下拉菜单的条目。
{ text, prefix, children }{ text, items }没有前缀:为每个条目写完整的链接。
{ text, link, activeMatch }{ text, link, activeMatch }相同,作为正则表达式。
{ text, ariaLabel }—不支持;添加一个 icon 或保留文本。
—{ component, props }在导航栏中渲染自定义组件。
—socialLinks社交链接与导航条目是分开的。

navbarLabel 和 pageNavbarLabel 曾是导航的无障碍标签。导航栏请使用 navMenuLabel;页面导航栏不再有自己的标签。

侧边栏的条目形状有所变化,因为主题现在在侧边栏和导航栏中使用相同的条目类型:

经典主题新主题说明
'/guide/'(字符串条目){ link: '/guide/' }文本在运行时从页面标题解析。
{ text, link }{ text, link }相同。
{ text, children }{ text, items }嵌套条目。
{ text, prefix, children }{ text, base, items }base 是各条目链接的前缀。
{ text, collapsible }{ text, collapsed }设置 collapsed 时分组可折叠,且它为 true 时分组初始处于折叠状态。
{ text, ariaLabel }—不支持。
—{ icon }条目的图标。
—{ docFooterText }上一篇/下一篇链接使用的文本。
以页面路径为键的侧边栏对象相同每个值可以是数组、'heading',或 { items, base }。
经典主题新主题说明
editLinkeditLink设置了 editLink.pattern 时才渲染链接。
editLinkTexteditLink.text默认值为 'Edit this page'。
editLinkPatterneditLink.pattern带 :path 的 URL 模板,或页面数据的函数。
docsRepo—自行构建 pattern,例如 https://github.com/example/repo/edit/main/:path。
docsBranch—同上。
docsDir—同上。
lastUpdatedlastUpdated相同,或带 text 和 formatOptions 的对象。
lastUpdatedTextlastUpdated.text默认值为 'Last updated'。
contributorscontributors相同,默认值为 true。
contributorsTextcontributorsText相同,默认值为 'Contributors'。
prevdocFooter.prev一个字符串标签,或 false 以隐藏上一篇链接。
nextdocFooter.next一个字符串标签,或 false 以隐藏下一篇链接。

颜色模式

经典主题新主题
colorMode: 'auto'appearance: true
colorMode: 'light'appearance: false
colorMode: 'dark'appearance: 'dark'
—appearance: 'force-dark'(始终为暗色)
—appearance: 'force-auto'(跟随系统,不显示切换按钮)
—appearance: { initialValue: 'dark' }(初始为暗色,显示切换按钮)

colorModeSwitch: false 没有直接对应项。外观固定时切换按钮会被隐藏,因此要隐藏它请使用 appearance: false、'dark'、'force-dark' 或 'force-auto'。如果只想修改初始值而保留切换按钮,请使用对象形式。

存储键仍然是 vuepress-color-scheme,但暗色模式现在由 <html> 上的 data-theme 属性驱动,而不再由 dark class 驱动。针对 .dark 的样式表必须改为针对 [data-theme='dark']。

首页与 404

经典主题(frontmatter)新主题(frontmatter)说明
home: truetype: home页面类型取代了布尔标记。
heroImagehero.image一个字符串,或 { src }。
heroImageDarkhero.image: { light, dark }图片的两个变体。
heroAlthero.image: { alt }替代文本。
heroHeight—不可配置。
heroTexthero.name项目的名称。
taglinehero.texthero 的文本。
actions[{ text, link, type }]hero.actions[{ text, link, theme }]type: 'primary' 变为 theme: 'brand','secondary' 变为 'alt'。
features[{ title, details }]features[{ title, details }]新条目还接受 icon、link 和 linkText。
footerfooter.message(主题配置项)首页页脚现在就是站点页脚。
footerHtml—页脚内容始终以 HTML 渲染。
notFound(字符串数组)notFound(对象)见下文。
backToHomenotFound.linkText默认值为 'Take me home'。
—notFound.title、quote、link、linkLabel、code404 页面的各个部分。

经典主题会从 notFound 中随机渲染一条消息。新主题只有一个 quote,因此请从原来的消息中选一条:

themeDefault({
  notFound: {
    title: 'PAGE NOT FOUND',
    quote: "There's nothing here.",
    linkText: 'Take me home',
  },
})

无障碍标签

经典主题新主题
toggleColorModedarkModeSwitchLabel,以及 lightModeSwitchTitle 和 darkModeSwitchTitle
toggleSidebarsidebarMenuLabel 和 mobileMenuLabel
selectLanguageTextlangMenuLabel
selectLanguageAriaLabellangMenuLabel
selectLanguageNamelocales.<localePath>.label
navbarLabelnavMenuLabel
pageNavbarLabel—
openInNewWindow—
—returnToTopLabel、extraMenuLabel、skipToContentLabel

主题插件

经典主题新主题说明
activeHeaderLinksactiveHeaderLinks只接受布尔值。
backToTopbackToTop相同的配置项。
copyCodecopyCode相同的配置项。
gitgit相同的配置项。
hintmarkdownHint相同的配置项。
linksChecklinksCheck相同的配置项。
mediumZoommediumZoom含义相同,接受配置对象。
nprogressnprogress相同。
prismjsshiki高亮器变了。关掉 Shiki 并注册 prismjsPlugin 即可保留 Prism。
seoseo相同的配置项,需要 hostname。
sitemapsitemap相同的配置项,需要 hostname。
tabmarkdownTab代码选项卡默认开启;::: tabs 需要 markdownTab: { tabs: true }。
—icon使用 Iconify 渲染图标。
—markdownExtGFM、脚注、任务列表和 v-pre 容器。
—markdownRaw带样式隔离的 raw 容器。

SASS 调色板已被移除。主题不再接入 @vuepress/plugin-palette,因为主题使用 CSS 自定义属性编写样式,站点在 .vuepress/styles/index.css 中覆盖它们即可。见样式。

插件仍然用 false 禁用、用对象配置,和以前一样:

themeDefault({
  themePlugins: {
    shiki: { themes: { light: 'nord', dark: 'nord' } },
    linksCheck: { exclude: [/^#\//u] },
    sitemap: false,
  },
})

要保留 Prism 作为高亮器,请禁用 Shiki 并自行注册 Prism 插件:

import { prismjsPlugin } from '@vuepress/plugin-prismjs'

export default defineUserConfig({
  plugins: [prismjsPlugin()],
  theme: themeDefault({
    themePlugins: { shiki: false },
  }),
})

迁移 frontmatter

经典主题新主题说明
home: truetype: home其他页面默认为 type: doc。
navbarnavbar相同。
externalLinkIconexternalLinkIcon相同。
pageClasspageClass相同,且该 class 会加在页面容器上。
editLinkeditLink一个布尔值,用于启用或禁用该页面的链接。
editLinkPattern—pattern 现在是主题配置项。
lastUpdatedlastUpdated相同,一个布尔值或一个时间戳。
contributorscontributors相同。
sidebarsidebar形状相同,只是条目有上面的变化。
sidebarDepthsidebarDepth相同。
prev / nextprev / next字符串即文本;对象接受 text、link、target 和 rel;false 隐藏链接。
footer(字符串)footer.message(主题配置项)首页页脚现在就是站点页脚。
footerHtml—页脚内容始终以 HTML 渲染。
—footer: false在该页面隐藏站点页脚。

上一篇/下一篇 frontmatter 的行为变了。在经典主题中,字符串是页面文件的路径,会被转换成链接。在新主题中,字符串就是链接的文本,目标来自侧边栏顺序:

- prev: /guide/getting-started.md
+ prev: { text: 'Get started', link: '/guide/getting-started.html' }

样式

经典主题使用 SASS:.vuepress/styles/palette.scss 存放变量,.vuepress/styles/index.scss 存放规则。新主题使用纯 CSS 和 CSS 自定义属性,因此不再需要调色板插件:

- /* .vuepress/styles/palette.scss */
- $vp-c-accent: #2c3e50;
- $vp-c-accent-bg: #2c3e50;
+ /* .vuepress/styles/index.css */
+ :root {
+   --vp-c-brand: #2c3e50;
+   --vp-c-brand-hover: #2c3e50;
+   --vp-c-brand-bg: #2c3e50;
+ }

自定义规则从 .vuepress/styles/index.scss 移到 .vuepress/styles/index.css,主题会自动导入后者。由于该文件是纯 CSS,只有站点自己配置了预处理器时才需要保留 SCSS。

经典主题的变量名会作为别名保留(--vp-c-accent、--vp-c-text、--vp-c-control 等等),因此生态插件的样式仍能正常工作。覆盖经典变量的站点应当改为覆盖它别名到的新变量,也就是主题自身使用的那个变量。见样式。

自定义布局和组件

经典主题的 @theme/* 别名在新主题中不存在,内部组件也不再导出。替代它们的两样东西是 theme.Layout 组件和布局的插槽:

- import ParentLayout from '@vuepress/theme-classic/layouts/Layout.vue'
+ import theme from '@vuepress/theme-default/client'
+
+ const ParentLayout = theme.Layout

经典主题的布局从 @vuepress/theme-classic/components/* 或通过 @theme/* 导入它的组件。新主题只导出公共组件,并保持它们的 VP 名称:VPBadge、VPButton、VPImage、VPLink、首页组件、团队组件和赞助商组件。当插槽存在时用插槽替换内部组件,否则用自己的标记替换。见内置组件。

类型

经典主题新主题
ClassicThemeOptionsThemeOptions
ClassicThemeDataThemeConfig
ClassicThemeLocaleDataThemeConfig
ClassicThemePageDataThemePageData
ClassicThemePageFrontmatterThemePageFrontmatter
ClassicThemeHomePageFrontmatterThemePageFrontmatter(带 type: 'home' 和 hero)
ClassicThemeNormalPageFrontmatterThemePageFrontmatter(带 type: 'doc')
ClassicThemePluginsOptionsThemePluginsOptions
NavbarOptionsDefaultTheme.NavItem[]
SidebarOptionsDefaultTheme.Sidebar

新主题的 DefaultTheme 命名空间存放导航条目、侧边栏条目、hero 和特性、页脚、团队成员以及赞助商的类型。以类型形式导入它,即可为配置对象标注类型:

import type { DefaultTheme } from '@vuepress/theme-default'

const nav: DefaultTheme.NavItem[] = [{ text: 'Guide', link: '/guide/' }]

客户端 API

经典主题新主题
useData()useThemePageData() 和 vuepress/client 的组合式函数
useThemeData()useThemeData()(同名)
useThemeLocaleData()useThemeLocaleData()(同名)
useDarkMode()useDarkMode()(返回暗色模式是否开启)
darkModeSymbolthemeDarkModeSymbol
useEditLink()—
useHeaders()useLayout().headers
useSidebarItems()useLayout().sidebar 和 useLayout().sidebarGroups
useNavbarConfig()—
useNavbarRepo()—

新主题不会重新导出 vuepress/client 的组合式函数。请直接从 vuepress/client 导入 usePageData、usePageFrontmatter、useRouteLocale、useSiteData 等。

没有对应新特性的功能

有几个经典特性是被移除而不是改名:

  • 导航栏中的 repo 图标及其标签。请改用 socialLinks 条目。
  • 由 docsRepo、docsBranch 和 docsDir 自动构建的编辑链接。请自行构建 editLink.pattern。
  • 随机化的 404 引文列表。新主题只有单个 quote。
  • 外链的 openInNewWindow 文本。新主题只有 externalLinkIcon 布尔值。
  • @theme/* 别名和可替换的内部组件。请改用插槽和布局。
  • pageNavbarLabel 标签。
  • SASS 调色板。请使用 CSS 自定义属性。

检查清单

  1. 把 @vuepress/theme-classic 替换为 @vuepress/theme-default,把 themeClassic 替换为 themeDefault。
  2. 把 navbar 转换为 nav,并把侧边栏条目转换为新的形状。
  3. 把 colorMode / colorModeSwitch 转换为 appearance,把语言切换器配置项转换为 locales.<path>.label 和 langMenuLabel。
  4. 把 notFound、backToHome 和首页 frontmatter 转换为新的 frontmatter。
  5. 重命名主题插件开关,如果站点必须保留 Prism 则关掉 Shiki。
  6. 把 SASS 调色板迁移为 .vuepress/styles/index.css 中的 CSS 变量。
  7. 用 theme.Layout 和公共组件替换 ParentLayout 与 @theme/* 导入。
  8. 更新类型名称。
  9. 运行站点,检查导航栏、侧边栏、首页、404 页面和暗色模式。

MIT 协议