Skip to content

主题配置

默认主题提供了丰富的配置选项,可以通过 themeConfig 字段定制站点外观。

快速导航: 快速参考 | 基础配置 | 导航栏 | 侧边栏 | 社交链接 | 搜索 | 编辑链接 | 页脚 | 大纲 | 本地化 | 其他配置


快速参考

所有 themeConfig 字段一览:

字段类型默认值说明
logostring站点 Logo
siteTitlestring | falsetitle 配置站点标题
navNavItem[]导航栏配置
sidebarSidebar | SidebarMulti侧边栏配置
socialLinksSocialLink[]社交媒体链接
searchSearchConfig本地搜索搜索配置
editLinkEditLinkConfig编辑链接配置
footerFooterConfig页脚配置
outlineOutlineConfig{ level: 2 }大纲配置
docFooterDocFooterConfig文档页脚配置
lastUpdatedLastUpdatedConfig最后更新配置
returnToTopLabelstring'Return to top'返回顶部文本
sidebarMenuLabelstring'Menu'侧边栏菜单文本
darkModeSwitchLabelstring'Appearance'深色模式切换文本
lightModeSwitchTitlestring'Light'浅色模式标题
darkModeSwitchTitlestring'Dark'深色模式标题
externalLinkIconbooleanfalse外部链接图标
notFoundNotFoundConfig404 页面配置

基础配置

  • 类型: string
  • 默认值:

站点 Logo,显示在导航栏左侧。路径相对于 public 目录。

ts
export default defineConfig({
  themeConfig: {
    logo: '/logo.svg'
  }
})

深色模式 Logo

如需为深色模式使用不同 Logo,可通过 CSS:

css
/* .vitepress/theme/style.css */
[src$="/logo.svg"] {
  display: none;
}

.dark [src$="/logo.svg"] {
  display: none;
}

.dark [src$="/logo-dark.svg"] {
  display: inline;
}

siteTitle

  • 类型: string | false
  • 默认值: 站点 title 配置

导航栏显示的站点标题。设为 false 可隐藏(通常配合 Logo 使用)。

ts
export default defineConfig({
  themeConfig: {
    logo: '/logo.svg',
    siteTitle: '我的博客'  // 自定义标题
    // siteTitle: false   // 隐藏标题
  }
})

导航栏配置

  • 类型: NavItem[]
  • 默认值:

导航栏配置,支持单层链接和下拉菜单。

单层链接

ts
export default defineConfig({
  themeConfig: {
    nav: [
      { text: '首页', link: '/' },
      { text: '指南', link: '/guide/' },
      { text: 'API', link: '/reference/' }
    ]
  }
})

下拉菜单

ts
export default defineConfig({
  themeConfig: {
    nav: [
      { text: '首页', link: '/' },
      {
        text: '更多',
        items: [
          { text: '配置', link: '/reference/site-config' },
          { text: 'API', link: '/reference/runtime-api' },
          { text: 'CLI', link: '/reference/cli' }
        ]
      }
    ]
  }
})

嵌套下拉菜单

ts
nav: [
  {
    text: '更多',
    items: [
      {
        text: '社区',
        items: [
          { text: 'GitHub', link: 'https://github.com/vuejs/vitepress' },
          { text: 'Discord', link: 'https://discord.gg/vue' }
        ]
      },
      {
        text: '资源',
        items: [
          { text: '教程', link: '/tutorial/' },
          { text: '示例', link: '/examples/' }
        ]
      }
    ]
  }
]

带徽章的导航

ts
nav: [
  {
    text: '新功能',
    link: '/new-feature',
    badge: { text: '新', type: 'tip' }
  }
]
字段类型必需说明
textstring导航项文本
linkstring链接路径(与 items 二选一)
itemsNavItem[]子菜单项(与 link 二选一)
activeMatchstring | RegExp自定义激活匹配规则
targetstring链接 target 属性,如 '_blank'
relstring链接 rel 属性,如 'noopener'
badgeBadgeConfig徽章配置

BadgeConfig 字段:

字段类型默认值说明
textstring徽章文本
type'info' | 'tip' | 'warning' | 'danger''info'徽章样式类型

activeMatch 示例

ts
nav: [
  {
    text: '指南',
    link: '/guide/',
    // 匹配 /guide/ 下所有页面
    activeMatch: '/guide/'
  },
  {
    text: 'API',
    link: '/reference/',
    // 使用正则匹配多个路径
    activeMatch: '^/(reference|api)/'
  }
]

侧边栏配置

  • 类型: Sidebar | SidebarMulti
  • 默认值:

侧边栏配置,支持单侧边栏和多侧边栏两种模式。

单侧边栏

所有页面共享同一侧边栏:

ts
export default defineConfig({
  themeConfig: {
    sidebar: [
      {
        text: '开始',
        items: [
          { text: '介绍', link: '/guide/' },
          { text: '安装', link: '/guide/installation' },
          { text: '快速开始', link: '/guide/getting-started' }
        ]
      },
      {
        text: '进阶',
        collapsed: true,
        items: [
          { text: 'SSR', link: '/advanced/ssr' },
          { text: 'MPA', link: '/advanced/mpa' }
        ]
      }
    ]
  }
})

多侧边栏

不同路径使用不同侧边栏:

ts
export default defineConfig({
  themeConfig: {
    sidebar: {
      '/guide/': [
        {
          text: '指南',
          items: [
            { text: '介绍', link: '/guide/' },
            { text: '安装', link: '/guide/installation' }
          ]
        }
      ],
      '/reference/': [
        {
          text: 'API 参考',
          items: [
            { text: '运行时 API', link: '/reference/runtime-api' },
            { text: '构建 API', link: '/reference/build-api' }
          ]
        }
      ]
    }
  }
})

SidebarItem 类型详解

字段类型必需说明
textstring分组/链接标题
linkstring链接路径(与 items 二选一)
itemsSidebarItem[]子项列表(与 link 二选一)
collapsedboolean是否默认折叠(仅对有 items 的分组有效)
basestring子项链接的基础路径前缀
badgeBadgeConfig徽章配置
relstring链接 rel 属性
targetstring链接 target 属性

折叠分组

ts
sidebar: [
  {
    text: '进阶',
    collapsed: true,  // 默认折叠
    items: [
      { text: 'SSR', link: '/advanced/ssr' },
      { text: 'MPA', link: '/advanced/mpa' }
    ]
  }
]

使用 base 简化路径

ts
sidebar: [
  {
    text: '指南',
    base: '/guide/',
    items: [
      { text: '介绍', link: '' },           // → /guide/
      { text: '安装', link: 'installation' }, // → /guide/installation
      { text: '配置', link: 'configuration' } // → /guide/configuration
    ]
  }
]

社交链接

  • 类型: SocialLink[]
  • 默认值:

导航栏右侧的社交媒体图标链接。

ts
export default defineConfig({
  themeConfig: {
    socialLinks: [
      { icon: 'github', link: 'https://github.com/vuejs/vitepress' },
      { icon: 'twitter', link: 'https://twitter.com/vitepress' },
      { icon: 'discord', link: 'https://discord.gg/vue' }
    ]
  }
})

内置图标

图标名称说明
githubGitHub
twitterTwitter / X
discordDiscord
facebookFacebook
instagramInstagram
linkedinLinkedIn
slackSlack
youtubeYouTube
mastodonMastodon

自定义 SVG 图标

ts
socialLinks: [
  {
    icon: {
      svg: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="..."/></svg>'
    },
    link: 'https://example.com'
  }
]

社交链接 + aria-label

ts
socialLinks: [
  {
    icon: 'github',
    link: 'https://github.com/...',
    // 自定义无障碍标签
    ariaLabel: 'GitHub 仓库'
  }
]

SocialLink 类型详解:

字段类型必需说明
iconstring | { svg: string }图标名称或自定义 SVG
linkstring链接地址
ariaLabelstring无障碍标签

搜索配置

  • 类型: SearchConfig
  • 默认值: 本地搜索

VitePress 内置搜索支持,可选择本地搜索或 Algolia 搜索。

本地搜索

ts
export default defineConfig({
  themeConfig: {
    search: {
      provider: 'local',
      options: {
        // 自定义搜索翻译
        translations: {
          button: {
            buttonText: '搜索文档'
          },
          modal: {
            noResultsText: '无法找到相关结果',
            resetButtonTitle: '清除查询条件',
            footer: {
              selectText: '选择',
              navigateText: '切换',
              closeText: '关闭'
            }
          }
        }
      }
    }
  }
})

本地搜索选项

选项类型默认值说明
translationsobject搜索 UI 翻译
_metaobject元数据配置

Algolia 搜索

ts
export default defineConfig({
  themeConfig: {
    search: {
      provider: 'algolia',
      options: {
        appId: 'YOUR_APP_ID',
        apiKey: 'YOUR_API_KEY',
        indexName: 'YOUR_INDEX_NAME',
        // 可选配置
        locales: {
          zh: {
            placeholder: '搜索文档',
            translations: {
              button: { buttonText: '搜索' },
              modal: { noResultsText: '没有找到相关结果' }
            }
          }
        }
      }
    }
  }
})

Algolia 选项详解

选项类型必需说明
appIdstringAlgolia Application ID
apiKeystringAlgolia Search-Only API Key
indexNamestringAlgolia 索引名称
localesobject多语言搜索配置
placeholderstring搜索框占位文本
searchParametersobjectAlgolia 搜索参数

编辑链接

  • 类型: EditLinkConfig
  • 默认值:

在每个页面底部显示"编辑此页"链接,引导用户参与文档贡献。

ts
export default defineConfig({
  themeConfig: {
    editLink: {
      pattern: 'https://github.com/user/repo/edit/main/docs/:path',
      text: '在 GitHub 上编辑此页'
    }
  }
})

EditLinkConfig 字段详解:

字段类型必需说明
patternstring编辑链接模板,:path 会被替换为当前页面路径
textstring链接文本,默认 'Edit this page'

不同平台的 pattern

平台pattern 示例
GitHubhttps://github.com/user/repo/edit/main/docs/:path
GitLabhttps://gitlab.com/user/repo/-/edit/main/docs/:path
Giteehttps://gitee.com/user/repo/edit/main/docs/:path
CNBhttps://cnb.cool/user/repo/-/edit/main/docs/:path

页脚配置

  • 类型: FooterConfig
  • 默认值:

页面底部页脚,仅在 layout: doc 时显示。

ts
export default defineConfig({
  themeConfig: {
    footer: {
      message: '基于 MIT 许可发布',
      copyright: 'Copyright © 2026-present 张三'
    }
  }
})

FooterConfig 字段详解:

字段类型说明
messagestring页脚消息(支持 HTML)
copyrightstring版权信息

页脚支持 HTML

message 字段支持 HTML 标签,可以添加链接:

ts
footer: {
  message: '基于 <a href="https://opensource.org/licenses/MIT">MIT 许可</a>发布',
  copyright: 'Copyright © 2026-present'
}

大纲配置

outline

  • 类型: OutlineConfig
  • 默认值: { level: 2, label: 'On this page' }

右侧大纲(目录)配置。

ts
export default defineConfig({
  themeConfig: {
    outline: {
      level: [2, 4],  // 显示 h2 到 h4
      label: '目录导航'
    }
  }
})

OutlineConfig 字段详解:

字段类型默认值说明
levelnumber | [number, number]2显示的标题级别范围
labelstring'On this page'大纲标题文本

level 值说明:

效果
2仅显示 h2
3显示 h2 和 h3
[2, 3]显示 h2 和 h3
[2, 4]显示 h2 到 h4
false不显示大纲

文档页脚

docFooter

  • 类型: DocFooterConfig
  • 默认值: { prev: 'Previous', next: 'Next' }

文档页面底部"上一篇/下一篇"的文本配置。

ts
export default defineConfig({
  themeConfig: {
    docFooter: {
      prev: '上一页',
      next: '下一页'
    }
  }
})

最后更新

lastUpdated

  • 类型: LastUpdatedConfig
  • 默认值: { text: 'Last updated' }

最后更新时间显示配置。需同时启用站点级 lastUpdated: true

ts
export default defineConfig({
  lastUpdated: true,
  themeConfig: {
    lastUpdated: {
      text: '最后更新于',
      formatOptions: {
        dateStyle: 'full',
        timeStyle: 'short'
      }
    }
  }
})

LastUpdatedConfig 字段详解:

字段类型默认值说明
textstring'Last updated'前缀文本
formatOptionsIntl.DateTimeFormatOptions日期格式化选项

常用 formatOptions:

配置效果
{ dateStyle: 'full' }2026 年 5 月 9 日星期六
{ dateStyle: 'long' }2026 年 5 月 9 日
{ dateStyle: 'medium' }2026/5/9
{ dateStyle: 'short' }26/5/9
{ dateStyle: 'full', timeStyle: 'short' }2026 年 5 月 9 日星期六 14:30

本地化文本

以下配置用于自定义 UI 中各处显示的文本:

字段类型默认值说明
returnToTopLabelstring'Return to top'返回顶部按钮文本
sidebarMenuLabelstring'Menu'移动端侧边栏菜单文本
darkModeSwitchLabelstring'Appearance'深色模式切换按钮文本
lightModeSwitchTitlestring'Light'浅色模式选项标题
darkModeSwitchTitlestring'Dark'深色模式选项标题
skipToContentLabelstring'Skip to content'跳转到内容链接文本
ts
export default defineConfig({
  themeConfig: {
    returnToTopLabel: '返回顶部',
    sidebarMenuLabel: '菜单',
    darkModeSwitchLabel: '主题',
    lightModeSwitchTitle: '浅色',
    darkModeSwitchTitle: '深色',
    skipToContentLabel: '跳转到正文'
  }
})

其他配置

externalLinkIcon

  • 类型: boolean
  • 默认值: false

是否在外部链接后显示图标。

ts
export default defineConfig({
  themeConfig: {
    externalLinkIcon: true  // 外部链接显示 ↗ 图标
  }
})

notFound

  • 类型: NotFoundConfig
  • 默认值: 默认 404 页面

自定义 404 页面配置。

ts
export default defineConfig({
  themeConfig: {
    notFound: {
      title: '页面未找到',
      quote: '看起来你访问了一个不存在的页面',
      linkLabel: '返回首页',
      linkText: '回到首页'
    }
  }
})

NotFoundConfig 字段详解:

字段类型默认值说明
titlestring'PAGE NOT FOUND'404 标题
quotestring'But if you don\'t change your direction...'引用文字
linkLabelstring'Take me home'链接标签
linkTextstring'Take me home'链接文本
codestring'404'状态码

完整配置示例

ts
// .vitepress/config.mts
import { defineConfig } from 'vitepress'

export default defineConfig({
  title: '我的文档',
  description: '一个 VitePress 站点',

  themeConfig: {
    // 基础配置
    logo: '/logo.svg',
    siteTitle: '我的文档',

    // 导航栏
    nav: [
      { text: '首页', link: '/' },
      { text: '指南', link: '/guide/' },
      { text: 'API', link: '/reference/' },
      {
        text: '更多',
        items: [
          { text: '教程', link: '/tutorial/' },
          { text: '示例', link: '/examples/' }
        ]
      }
    ],

    // 多侧边栏
    sidebar: {
      '/guide/': [
        {
          text: '开始',
          items: [
            { text: '介绍', link: '/guide/' },
            { text: '安装', link: '/guide/installation' }
          ]
        }
      ],
      '/reference/': [
        {
          text: 'API 参考',
          base: '/reference/',
          items: [
            { text: '运行时 API', link: 'runtime-api' },
            { text: '构建 API', link: 'build-api' }
          ]
        }
      ]
    },

    // 社交链接
    socialLinks: [
      { icon: 'github', link: 'https://github.com/...' }
    ],

    // 搜索
    search: {
      provider: 'local',
      options: {
        translations: {
          button: { buttonText: '搜索文档' }
        }
      }
    },

    // 编辑链接
    editLink: {
      pattern: 'https://github.com/.../edit/main/docs/:path',
      text: '编辑此页'
    },

    // 页脚
    footer: {
      message: '基于 MIT 许可发布',
      copyright: 'Copyright © 2026'
    },

    // 大纲
    outline: {
      level: [2, 3],
      label: '目录'
    },

    // 最后更新
    lastUpdated: {
      text: '最后更新',
      formatOptions: { dateStyle: 'full' }
    },

    // 本地化文本
    returnToTopLabel: '返回顶部',
    sidebarMenuLabel: '菜单',
    darkModeSwitchLabel: '主题',

    // 其他
    externalLinkIcon: true
  }
})

参考链接

贡献者

加载中...

想要成为贡献者?

在 CNB 上参与贡献