Skip to content

速查表

VitePress 常用配置和语法的快速参考。

配置速查

基础配置

ts
import { defineConfig } from 'vitepress'

export default defineConfig({
  title: '站点标题',
  description: '站点描述',
  lang: 'zh-CN',
  base: '/',           // 部署路径
  appearance: true,    // 主题切换
  cleanUrls: true,     // 清理 URL
  lastUpdated: true    // 显示更新时间
})

导航栏

ts
themeConfig: {
  nav: [
    { text: '首页', link: '/' },
    { text: '指南', link: '/guide/' },
    {
      text: '更多',
      items: [
        { text: 'API', link: '/api/' },
        { text: 'GitHub', link: 'https://github.com' }
      ]
    }
  ]
}

侧边栏

ts
themeConfig: {
  sidebar: {
    '/guide/': [
      {
        text: '入门',
        collapsed: false,
        items: [
          { text: '介绍', link: '/guide/' },
          { text: '安装', link: '/guide/installation' }
        ]
      }
    ]
  }
}

搜索

ts
// 本地搜索
themeConfig: {
  search: { provider: 'local' }
}

// Algolia
themeConfig: {
  search: {
    provider: 'algolia',
    options: {
      appId: 'xxx',
      apiKey: 'xxx',
      indexName: 'xxx'
    }
  }
}

Markdown 语法

Frontmatter

yaml
---
title: 页面标题
description: 页面描述
layout: doc          # doc | page | home
sidebar: true
outline: [2, 3]
---

自定义容器

markdown
::: tip
提示信息
:::

::: warning
警告信息
:::

::: danger
危险信息
:::

::: details 标题
折叠内容
:::

代码块

markdown
```javascript {2-3}
function hello() {
  console.log('Hello')  // 高亮
  return true           // 高亮
}
```

```typescript [filename.ts]
const a: string = 'hello'
```

代码组

markdown
::: code-group

```bash [npm]
npm install package
bash
yarn add package
bash
pnpm add package

:::


### 表格

```markdown
| 列1 | 列2 | 列3 |
|-----|-----|-----|
| 内容 | 内容 | 内容 |

目录

markdown
[[toc]]

组件使用

内置组件

markdown
<!-- 徽章 -->
<Badge type="tip" text="新功能" />

<!-- 客户端渲染 -->
<ClientOnly>
  <div>{{ window.innerWidth }}</div>
</ClientOnly>

访问数据

vue
<script setup>
import { useData, useRoute, useRouter } from 'vitepress'

const { page, frontmatter, isDark } = useData()
const route = useRoute()
const router = useRouter()
</script>

主题定制

CSS 变量

css
:root {
  /* 品牌色 */
  --vp-c-brand-1: #6366f1;
  --vp-c-brand-2: #4f46e5;
  --vp-c-brand-3: #4338ca;
  
  /* 首页渐变 */
  --vp-home-hero-name-background: linear-gradient(120deg, #6366f1, #818cf8);
}

.dark {
  --vp-c-brand-1: #818cf8;
}

布局插槽

vue
<template>
  <Layout>
    <template #doc-before>
      内容前
    </template>
    <template #doc-after>
      内容后
    </template>
  </Layout>
</template>

常用命令

命令说明
vitepress dev docs启动开发服务器
vitepress build docs构建生产版本
vitepress preview docs预览构建结果

路由映射

文件URL
index.md/
guide.md/guide
guide/index.md/guide/
guide/start.md/guide/start

常用插件

插件用途
vitepress-plugin-mermaidMermaid 图表
markdown-it-mathjax3数学公式
vite-plugin-pwaPWA 支持
medium-zoom图片缩放

部署平台

平台配置文件
GitHub Pages.github/workflows/deploy.yml
Vercelvercel.json
Netlifynetlify.toml

环境变量

bash
# 定义
VITE_API_URL=https://api.example.com

# 使用
const url = import.meta.env.VITE_API_URL

常见问题解决

问题解决方案
图片不显示放在 public/ 目录,使用 /image.png
端口被占用配置 vite.server.port
链接 404检查 base 配置
样式不生效检查 CSS 变量选择器
构建失败清除 .vitepress/cache 重试

目录结构

docs/
├── .vitepress/
│   ├── config.mts      # 配置文件
│   ├── theme/          # 主题目录
│   │   ├── index.ts
│   │   └── style.css
│   └── cache/          # 缓存目录
├── public/             # 静态资源
│   └── images/
├── guide/              # 文档目录
│   └── index.md
└── index.md            # 首页

提示

将此页面加入书签,方便随时查阅!

贡献者

加载中...

想要成为贡献者?

在 CNB 上参与贡献