技术文档
使用 VitePress 构建专业的技术文档网站,为产品、API、教程等提供完整的文档解决方案。
项目概述
技术文档是产品成功的关键因素之一,好的文档能够帮助用户快速上手,减少支持成本,提升用户体验。VitePress 作为现代化的静态站点生成器,为构建技术文档提供了完美的解决方案。
核心特性
- 📝 Markdown 优先 - 使用 Markdown 编写,简单高效
- 🎨 美观界面 - 现代化的设计和用户体验
- 🔍 全文搜索 - 内置强大的搜索功能
- 🌍 国际化 - 完整的多语言支持
- 📱 响应式 - 完美适配各种设备
- ⚡ 极速加载 - 基于 Vite 的快速构建和热更新
- 🎯 SEO 友好 - 优秀的搜索引擎优化
- 🔧 高度定制 - 灵活的主题和插件系统
- 📊 数据分析 - 集成 Google Analytics 等分析工具
- 🚀 自动部署 - 支持多种部署平台
技术架构
核心技术栈
json
{
"generator": "VitePress",
"framework": "Vue 3",
"bundler": "Vite",
"language": "TypeScript",
"styling": "CSS3 + PostCSS",
"search": "MiniSearch",
"deployment": [
"Vercel",
"Netlify",
"GitHub Pages",
"CloudFlare Pages"
]
}
项目结构
tech-docs/
├── docs/
│ ├── .vitepress/
│ │ ├── config.ts
│ │ ├── theme/
│ │ │ ├── index.ts
│ │ │ ├── Layout.vue
│ │ │ └── components/
│ │ └── public/
│ ├── guide/
│ │ ├── index.md
│ │ ├── getting-started.md
│ │ └── advanced.md
│ ├── api/
│ │ ├── index.md
│ │ └── reference.md
│ ├── examples/
│ ├── tutorials/
│ └── index.md
├── scripts/
├── package.json
└── README.md
实现步骤
1. 项目初始化
bash
# 创建项目
npm create vitepress@latest tech-docs
cd tech-docs
# 安装依赖
npm install
# 启动开发服务器
npm run docs:dev
2. 基础配置
typescript
// .vitepress/config.ts
import { defineConfig } from 'vitepress'
export default defineConfig({
title: '技术文档',
description: '专业的技术文档解决方案',
// 站点配置
lang: 'zh-CN',
base: '/',
cleanUrls: true,
// 头部配置
head: [
['link', { rel: 'icon', href: '/favicon.ico' }],
['meta', { name: 'theme-color', content: '#3c82f6' }],
['meta', { name: 'og:type', content: 'website' }],
['meta', { name: 'og:locale', content: 'zh-CN' }],
['meta', { name: 'og:site_name', content: '技术文档' }],
['script', { async: '', src: 'https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID' }],
['script', {}, `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
`]
],
// 主题配置
themeConfig: {
logo: '/logo.svg',
siteTitle: '技术文档',
// 导航菜单
nav: [
{ text: '指南', link: '/guide/' },
{ text: 'API', link: '/api/' },
{ text: '示例', link: '/examples/' },
{ text: '教程', link: '/tutorials/' },
{
text: '更多',
items: [
{ text: '更新日志', link: '/changelog' },
{ text: '贡献指南', link: '/contributing' },
{ text: 'FAQ', link: '/faq' }
]
}
],
// 侧边栏
sidebar: {
'/guide/': [
{
text: '开始使用',
collapsed: false,
items: [
{ text: '介绍', link: '/guide/' },
{ text: '快速开始', link: '/guide/getting-started' },
{ text: '安装配置', link: '/guide/installation' },
{ text: '基本概念', link: '/guide/concepts' }
]
},
{
text: '核心功能',
collapsed: false,
items: [
{ text: '文档编写', link: '/guide/writing' },
{ text: '主题定制', link: '/guide/theming' },
{ text: '插件系统', link: '/guide/plugins' },
{ text: '部署发布', link: '/guide/deployment' }
]
},
{
text: '高级用法',
collapsed: true,
items: [
{ text: '自定义组件', link: '/guide/custom-components' },
{ text: '数据获取', link: '/guide/data-loading' },
{ text: '国际化', link: '/guide/i18n' },
{ text: '性能优化', link: '/guide/performance' }
]
}
],
'/api/': [
{
text: 'API 参考',
items: [
{ text: '配置选项', link: '/api/config' },
{ text: '主题 API', link: '/api/theme' },
{ text: '插件 API', link: '/api/plugins' },
{ text: 'CLI 命令', link: '/api/cli' }
]
}
],
'/examples/': [
{
text: '示例集合',
items: [
{ text: '基础示例', link: '/examples/basic' },
{ text: '高级示例', link: '/examples/advanced' },
{ text: '集成示例', link: '/examples/integrations' }
]
}
]
},
// 社交链接
socialLinks: [
{ icon: 'github', link: 'https://github.com/your-org/tech-docs' },
{ icon: 'twitter', link: 'https://twitter.com/your-handle' }
],
// 页脚
footer: {
message: '基于 MIT 许可发布',
copyright: 'Copyright © 2024 Your Organization'
},
// 编辑链接
editLink: {
pattern: 'https://github.com/your-org/tech-docs/edit/main/docs/:path',
text: '在 GitHub 上编辑此页'
},
// 最后更新时间
lastUpdated: {
text: '最后更新于',
formatOptions: {
dateStyle: 'short',
timeStyle: 'medium'
}
},
// 搜索
search: {
provider: 'local',
options: {
locales: {
zh: {
translations: {
button: {
buttonText: '搜索文档',
buttonAriaLabel: '搜索文档'
},
modal: {
noResultsText: '无法找到相关结果',
resetButtonTitle: '清除查询条件',
footer: {
selectText: '选择',
navigateText: '切换'
}
}
}
}
}
}
},
// 大纲配置
outline: {
level: [2, 3],
label: '页面导航'
},
// 返回顶部
returnToTopLabel: '回到顶部',
// 侧边栏菜单标签
sidebarMenuLabel: '菜单',
// 深色模式切换标签
darkModeSwitchLabel: '主题',
lightModeSwitchTitle: '切换到浅色模式',
darkModeSwitchTitle: '切换到深色模式'
},
// Markdown 配置
markdown: {
theme: {
light: 'github-light',
dark: 'github-dark'
},
lineNumbers: true,
config: (md) => {
// 自定义 markdown 插件
md.use(require('markdown-it-task-lists'))
md.use(require('markdown-it-footnote'))
}
},
// Vite 配置
vite: {
plugins: [],
server: {
host: true,
port: 3000
}
},
// 构建配置
buildEnd: async (siteConfig) => {
// 构建完成后的钩子
console.log('文档构建完成!')
}
})
3. 自定义主题
vue
<!-- .vitepress/theme/Layout.vue -->
<template>
<Layout>
<template #nav-bar-title-after>
<span class="version-badge">v2.0</span>
</template>
<template #nav-bar-content-after>
<div class="nav-actions">
<a href="https://github.com/your-org/tech-docs" target="_blank" class="github-link">
<GitHubIcon />
</a>
</div>
</template>
<template #doc-before>
<div v-if="frontmatter.announcement" class="announcement">
<div class="announcement-content">
{{ frontmatter.announcement }}
</div>
</div>
</template>
<template #doc-after>
<div class="doc-footer">
<div class="feedback">
<h4>这篇文档有帮助吗?</h4>
<div class="feedback-buttons">
<button @click="sendFeedback('positive')" class="feedback-btn positive">
👍 有帮助
</button>
<button @click="sendFeedback('negative')" class="feedback-btn negative">
👎 需要改进
</button>
</div>
</div>
<div class="contribute">
<p>发现错误或想要贡献?</p>
<a :href="editLink" target="_blank" class="edit-link">
在 GitHub 上编辑此页
</a>
</div>
</div>
</template>
<template #aside-outline-after>
<div class="toc-links">
<h4>相关链接</h4>
<ul>
<li><a href="/api/">API 参考</a></li>
<li><a href="/examples/">示例代码</a></li>
<li><a href="/faq">常见问题</a></li>
</ul>
</div>
</template>
</Layout>
</template>
<script setup>
import { computed } from 'vue'
import { useData, useRouter } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import GitHubIcon from './components/GitHubIcon.vue'
const { Layout } = DefaultTheme
const { page, frontmatter } = useData()
const router = useRouter()
const editLink = computed(() => {
const base = 'https://github.com/your-org/tech-docs/edit/main/docs'
return `${base}${page.value.relativePath}`
})
function sendFeedback(type) {
// 发送反馈数据
if (typeof gtag !== 'undefined') {
gtag('event', 'feedback', {
event_category: 'documentation',
event_label: page.value.relativePath,
value: type === 'positive' ? 1 : 0
})
}
// 显示感谢消息
alert(type === 'positive' ? '感谢您的反馈!' : '我们会努力改进的!')
}
</script>
<style scoped>
.version-badge {
display: inline-block;
margin-left: 8px;
padding: 2px 6px;
background: var(--vp-c-brand);
color: white;
font-size: 12px;
border-radius: 4px;
font-weight: 500;
}
.nav-actions {
display: flex;
align-items: center;
gap: 12px;
}
.github-link {
display: flex;
align-items: center;
color: var(--vp-c-text-2);
transition: color 0.2s;
}
.github-link:hover {
color: var(--vp-c-text-1);
}
.announcement {
margin: 16px 0;
padding: 12px 16px;
background: var(--vp-c-tip-soft);
border: 1px solid var(--vp-c-tip);
border-radius: 8px;
}
.announcement-content {
color: var(--vp-c-tip-text);
font-size: 14px;
}
.doc-footer {
margin-top: 48px;
padding-top: 24px;
border-top: 1px solid var(--vp-c-divider);
}
.feedback {
margin-bottom: 32px;
}
.feedback h4 {
margin-bottom: 12px;
font-size: 16px;
color: var(--vp-c-text-1);
}
.feedback-buttons {
display: flex;
gap: 12px;
}
.feedback-btn {
padding: 8px 16px;
border: 1px solid var(--vp-c-border);
border-radius: 6px;
background: var(--vp-c-bg);
color: var(--vp-c-text-2);
cursor: pointer;
transition: all 0.2s;
font-size: 14px;
}
.feedback-btn:hover {
border-color: var(--vp-c-brand);
color: var(--vp-c-brand);
}
.contribute p {
margin-bottom: 8px;
color: var(--vp-c-text-2);
font-size: 14px;
}
.edit-link {
color: var(--vp-c-brand);
text-decoration: none;
font-size: 14px;
}
.edit-link:hover {
text-decoration: underline;
}
.toc-links {
margin-top: 24px;
padding-top: 16px;
border-top: 1px solid var(--vp-c-divider);
}
.toc-links h4 {
margin-bottom: 8px;
font-size: 13px;
font-weight: 600;
color: var(--vp-c-text-1);
}
.toc-links ul {
list-style: none;
padding: 0;
margin: 0;
}
.toc-links li {
margin-bottom: 4px;
}
.toc-links a {
color: var(--vp-c-text-2);
text-decoration: none;
font-size: 12px;
transition: color 0.2s;
}
.toc-links a:hover {
color: var(--vp-c-brand);
}
</style>
4. 自定义组件
vue
<!-- .vitepress/theme/components/CodeGroup.vue -->
<template>
<div class="code-group">
<div class="code-group-tabs">
<button
v-for="(tab, index) in tabs"
:key="index"
:class="['tab', { active: activeTab === index }]"
@click="activeTab = index"
>
{{ tab.title }}
</button>
</div>
<div class="code-group-content">
<div
v-for="(tab, index) in tabs"
:key="index"
v-show="activeTab === index"
class="tab-content"
>
<div v-html="tab.content"></div>
</div>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const props = defineProps({
tabs: {
type: Array,
required: true
}
})
const activeTab = ref(0)
onMounted(() => {
// 处理代码高亮
if (typeof Prism !== 'undefined') {
Prism.highlightAll()
}
})
</script>
<style scoped>
.code-group {
margin: 16px 0;
border: 1px solid var(--vp-c-border);
border-radius: 8px;
overflow: hidden;
}
.code-group-tabs {
display: flex;
background: var(--vp-c-bg-mute);
border-bottom: 1px solid var(--vp-c-border);
}
.tab {
padding: 8px 16px;
background: transparent;
border: none;
color: var(--vp-c-text-2);
cursor: pointer;
transition: all 0.2s;
font-size: 13px;
border-right: 1px solid var(--vp-c-border);
}
.tab:last-child {
border-right: none;
}
.tab.active {
background: var(--vp-c-bg);
color: var(--vp-c-text-1);
}
.tab:hover:not(.active) {
color: var(--vp-c-text-1);
}
.code-group-content {
background: var(--vp-code-block-bg);
}
.tab-content {
padding: 0;
}
.tab-content :deep(pre) {
margin: 0;
border-radius: 0;
}
</style>
5. 内容组织
markdown
<!-- docs/guide/index.md -->
---
title: 指南
description: 学习如何使用我们的产品
---
# 指南
欢迎来到我们的技术文档!这里包含了所有你需要了解的信息。
## 快速导航
<div class="feature-grid">
<div class="feature-card">
<h3>🚀 快速开始</h3>
<p>5分钟内完成安装和配置</p>
<a href="/guide/getting-started">开始使用 →</a>
</div>
<div class="feature-card">
<h3>📖 API 参考</h3>
<p>完整的 API 文档和示例</p>
<a href="/api/">查看 API →</a>
</div>
<div class="feature-card">
<h3>💡 示例代码</h3>
<p>丰富的使用示例和最佳实践</p>
<a href="/examples/">查看示例 →</a>
</div>
<div class="feature-card">
<h3>🎓 教程</h3>
<p>从入门到精通的完整教程</p>
<a href="/tutorials/">开始学习 →</a>
</div>
</div>
## 主要特性
- ✅ **易于使用** - 简单直观的 API 设计
- ✅ **高性能** - 优化的性能和最小的资源占用
- ✅ **可扩展** - 灵活的插件系统
- ✅ **类型安全** - 完整的 TypeScript 支持
## 下一步
1. [安装配置](/guide/installation) - 了解如何安装和配置
2. [基本概念](/guide/concepts) - 理解核心概念
3. [快速开始](/guide/getting-started) - 创建你的第一个项目
<style>
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin: 32px 0;
}
.feature-card {
padding: 24px;
border: 1px solid var(--vp-c-border);
border-radius: 8px;
background: var(--vp-c-bg-soft);
transition: all 0.2s;
}
.feature-card:hover {
border-color: var(--vp-c-brand);
transform: translateY(-2px);
}
.feature-card h3 {
margin-top: 0;
margin-bottom: 12px;
color: var(--vp-c-text-1);
}
.feature-card p {
margin-bottom: 16px;
color: var(--vp-c-text-2);
font-size: 14px;
}
.feature-card a {
color: var(--vp-c-brand);
text-decoration: none;
font-weight: 500;
}
.feature-card a:hover {
text-decoration: underline;
}
</style>
6. 部署配置
yaml
# .github/workflows/deploy.yml
name: Deploy Documentation
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build documentation
run: npm run docs:build
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/.vitepress/dist
7. 搜索优化
typescript
// .vitepress/config.ts (搜索配置部分)
export default defineConfig({
// ...其他配置
transformHead: ({ pageData }) => {
const head = []
// 添加结构化数据
head.push([
'script',
{ type: 'application/ld+json' },
JSON.stringify({
'@context': 'https://schema.org',
'@type': 'TechArticle',
headline: pageData.title,
description: pageData.description,
author: {
'@type': 'Organization',
name: 'Your Organization'
},
datePublished: pageData.lastUpdated,
dateModified: pageData.lastUpdated
})
])
return head
},
sitemap: {
hostname: 'https://your-docs-site.com'
}
})
最佳实践
1. 内容组织
- 清晰的层次结构 - 使用合理的目录结构
- 一致的命名 - 统一的文件和路径命名规范
- 适当的分页 - 避免单页内容过长
- 交叉引用 - 合理使用内部链接
2. 写作规范
- 用户导向 - 从用户角度编写内容
- 简洁明了 - 使用简单直接的语言
- 示例丰富 - 提供充足的代码示例
- 及时更新 - 保持内容的时效性
3. 技术优化
- 性能优化 - 优化图片和资源加载
- SEO 优化 - 合理的元数据和结构化数据
- 可访问性 - 遵循无障碍设计原则
- 移动友好 - 确保移动设备体验
部署选项
1. Vercel 部署
json
{
"buildCommand": "npm run docs:build",
"outputDirectory": "docs/.vitepress/dist",
"installCommand": "npm install"
}
2. Netlify 部署
toml
# netlify.toml
[build]
publish = "docs/.vitepress/dist"
command = "npm run docs:build"
[build.environment]
NODE_VERSION = "18"
3. GitHub Pages
yaml
# 在 .github/workflows/deploy.yml 中配置
# (参见上面的部署配置示例)
总结
通过本指南,你可以构建一个功能完整的技术文档网站,包含:
- 现代化的用户界面和体验
- 强大的搜索和导航功能
- 灵活的内容组织和管理
- 自动化的构建和部署流程
这个文档系统可以满足各种技术文档的需求,从 API 文档到用户手册,从教程到参考资料。