跳到正文

theme-default

@vuepress/theme-default

VuePress 的默认主题。它是从 VitePress 默认主题移植而来的薄布局层:主题只提供布局和样式(导航栏、侧边栏、文档大纲、首页、页脚和暗色模式),其余功能全部交给生态插件实现。

旧版默认主题

过去以 @vuepress/theme-default 发布的主题现在是 @vuepress/theme-classic。新的默认主题刻意让布局和 CSS 变量名贴近 VitePress 而不是经典主题,因此两个主题不能互相直接替换 —— 迁移站点时请参考 从经典主题迁移。

使用方法

安装主题:

npm i -D @vuepress/theme-default@next

在配置中使用它:

.vuepress/config.ts
import { defaultTheme } from '@vuepress/theme-default'
import { defineUserConfig } from 'vuepress'

export default defineUserConfig({
  theme: defaultTheme({
    siteTitle: 'My Site',
    logo: '/logo.svg',
    navbar: [{ text: 'Guide', link: '/guide/' }],
    sidebar: ['/guide/', '/guide/install'],
    appearance: true,
  }),
})

defaultTheme() 的每个配置项都在 配置 中说明。主题还接受一个 hostname 配置项,它不属于主题配置本身,而是 SEO 和 sitemap 插件所必需的:

export default defineUserConfig({
  theme: defaultTheme({
    hostname: 'https://example.com',
  }),
})

页面类型

主题只注册一个 Layout(另外为 404 页面注册 NotFound),并根据页面 frontmatter 的 type 字段决定在其中渲染什么。默认类型是 doc。

type渲染内容
homeVPHome:Hero、特性卡片,以及它们下方的页面内容
doc(默认)VPDoc:带有大纲侧边栏、最后更新时间以及上一篇/下一篇页脚的正文
pageVPPage:仅 Markdown 内容,没有文档侧边栏和页脚
content仅原始 Markdown 内容,没有任何布局外壳
任意自定义 layout客户端配置中注册的同名布局

每种页面类型接受哪些字段见 Frontmatter,自定义布局见 继承。

各项配置的位置

关注点通过什么配置页面
导航栏、侧边栏、大纲、编辑链接、页脚、主题 UIdefaultTheme() 配置项配置
页面类型、首页 Hero 与特性、单页 UI页面 frontmatterFrontmatter
内置插件及其开关themePlugins插件
UI 文案和按语言区分的配置项locales语言
导出的组件、团队与赞助章节客户端 API内置组件
Markdown 语法内置插件Markdown
CSS 变量、用户样式普通 CSS样式
自定义布局和布局插槽.vuepress/client.ts继承
从经典主题迁移—从经典主题迁移

内置插件

主题会自动接入以下插件。每个插件都可以通过 themePlugins 或 markdownPlugins 设为 false 禁用,或传入配置对象进行配置;见 插件。

插件选项默认值
@vuepress/plugin-theme-data—始终启用
@vuepress/plugin-copy-codecopyCode启用
@vuepress/plugin-gitgit启用
@vuepress/plugin-shikimarkdownPlugins.shiki启用,github-light/github-dark
@vuepress/plugin-markdown-hintmarkdownPlugins.hint启用,hint 和 alert
@vuepress/plugin-markdown-tabmarkdownPlugins.tab启用,codeTabs
@vuepress/plugin-markdown-extmarkdownPlugins.ext启用,vPre
@vuepress/plugin-markdown-rawmarkdownPlugins.raw启用,样式隔离开启
@vuepress/plugin-active-header-linksactiveHeaderLinks启用
@vuepress/plugin-iconicon启用,assets: 'iconify'
@vuepress/plugin-medium-zoommediumZoom启用
@vuepress/plugin-nprogressnprogress启用
@vuepress/plugin-back-to-topbackToTop启用
@vuepress/plugin-links-checkmarkdownPlugins.linksCheck启用
@vuepress/plugin-seoseo仅在设置了 hostname 时启用
@vuepress/plugin-sitemapsitemap仅在设置了 hostname 时启用

搜索并未内置

主题无法为站点选择搜索后端,因此搜索不属于主题的一部分。请安装 @vuepress/plugin-slimsearch(本地搜索)或 @vuepress/plugin-docsearch(Algolia),并在 plugins 中注册;导航栏会自动渲染全局注册的 SearchBox 组件。

客户端 API

Node 入口(@vuepress/theme-default)导出 defaultTheme 以及描述主题的类型:ThemeOptions、ThemeConfig、ThemeLayout、ThemePageData、ThemePluginsOptions、ThemeContributor、AppearanceOptions,以及导航项、侧边栏项、大纲、页脚、团队成员等类型(NavItem、SidebarItem、Outline、Footer、TeamMember 等)。

客户端入口(@vuepress/theme-default/client)导出内置组件和组合式函数。导入它还会加载主题样式和 Inter 网络字体。

组件:VPBadge、VPButton、VPDocAsideSponsors、VPDocFooterContributors、VPFeatures、VPHomeContent、VPHomeFeatures、VPHomeHero、VPHomeSponsors、VPImage、VPLink、VPNavBarSearch、VPSocialLink、VPSocialLinks、VPSponsors、VPTeamMembers、VPTeamPage、VPTeamPageSection、VPTeamPageTitle、VPThemeIcon。

组合式函数与辅助工具:useContributors、useLayout、usePageIsNotFound、usePageLastUpdated、usePageRelativePath、usePageType、useThemeData、useThemeLocaleData、useThemePageData、useThemePageFrontmatter、useThemeLocalePath、useThemeLocales、useRuntimeBase、useDarkMode、createDarkModeRef、themeDarkModeSymbol、COLOR_MODE_STORAGE_KEY。

组件属性见 内置组件,useLayout 返回的布局状态见 继承。

下一步

MIT 协议