Skip to content

Commit fbe3027

Browse files
committed
others:🤗Initially adopt the ViteNotes framework to present the tutorial content.
1 parent 0827747 commit fbe3027

171 files changed

Lines changed: 10085 additions & 1121 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,20 @@ Thumbs.db
1313
[._]*.sw[a-p]
1414

1515
.vscode/*
16+
17+
# vitepress specific
18+
19+
# dependencies
20+
node_modules/
21+
22+
# VitePress cache + build output
23+
docs/.vitepress/cache/
24+
docs/.vitepress/dist/
25+
26+
# logs
27+
*.log
28+
29+
# OS / editor
30+
.DS_Store
31+
.vscode/
32+
.idea/

docs/.vitepress/config.ts

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
import { defineConfig, type HeadConfig } from 'vitepress'
2+
import { generateSidebar } from 'vitepress-sidebar'
3+
import { withMermaid } from 'vitepress-plugin-mermaid'
4+
import timeline from "vitepress-markdown-timeline"
5+
import { writeFile } from 'fs/promises'
6+
import { join } from 'path'
7+
8+
// ========== 一、站点配置(统一管理 SEO 和资源配置)==========
9+
const SITE_CONFIG = {
10+
// 站点标题配置
11+
title: "A tutorial of git", // 浏览器标签页标题、SEO 标题
12+
siteTitle: "Faster-Git", // 左上角导航栏标题(非SEO)
13+
description: "A tutorial of git",
14+
15+
// SEO 配置
16+
url: '', // 网站域名,示例: 'https://yourdomain.com'(留空则不生成 sitemap)
17+
keywords: 'git,版本控制,教程,中文,学习笔记',
18+
author: '',
19+
20+
// 资源配置
21+
favicon: {
22+
href: '/favicon/datawhale.png',// 网站图标
23+
type: 'image/png' // 支持: image/png, image/svg+xml, image/x-icon 等(tips:手动修改匹配)
24+
},
25+
logo: '/favicon/datawhale.png',// 左上角图标
26+
27+
// robots.txt 排除目录(根据项目实际情况调整)
28+
robotsDisallow: [
29+
'/*assets/', // 任意层级名为 assets 的文件夹(兼容主流爬虫的通配符)
30+
'/.vitepress/', // VitePress 配置
31+
],
32+
}
33+
34+
/**
35+
* 将 Markdown 文件路径转换为网站 URL 路径
36+
* 兼容 rewrites 和国际化路由配置
37+
*/
38+
function getPageUrl(relativePath: string): string {
39+
return relativePath
40+
.replace(/\\/g, '/') // Windows 路径 -> Unix 路径
41+
.replace(/\.md$/, '.html') // .md -> .html
42+
.replace(/\/index\.html$/, '/') // /index.html -> /
43+
.replace(/^index\.html$/, '') // 根 index.html -> ''
44+
.replace(/^/, '/') // 确保以 / 开头
45+
.replace(/\/\/+/g, '/') // 去除多余斜杠
46+
}
47+
48+
// ========== 二、侧边栏自动化生成 ==========
49+
const commonSidebarConfig = {
50+
useTitleFromFileHeading: true,
51+
useFolderTitleFromIndexFile: true,
52+
useFolderLinkFromIndexFile: true,
53+
hyphenToSpace: true,
54+
collapsed: true,
55+
excludePattern: ['public', 'assets', 'docs'],
56+
manualSortFileNameByPriority: [ // 手动排序文件名优先级
57+
'guide',
58+
'Appendix',
59+
'80-MachineLearning'
60+
],
61+
}
62+
63+
// 为侧边栏所有链接添加国际化路径前缀
64+
const addPrefix = (items: any, prefix: string): any => {
65+
if (Array.isArray(items)) {
66+
return items.map(item => ({
67+
...item,
68+
link: item.link ? prefix + item.link.replace(/^\//, '') : undefined,
69+
items: item.items ? addPrefix(item.items, prefix) : undefined
70+
}))
71+
}
72+
return items
73+
}
74+
75+
// 生成侧边栏(支持国际化前缀)
76+
const createSidebar = (root: string, prefix = '/') => {
77+
const sidebar = generateSidebar({ documentRootPath: root, ...commonSidebarConfig })
78+
return prefix === '/' ? sidebar : addPrefix(sidebar, prefix)
79+
}
80+
81+
// ========== 三、VitePress 配置 ==========
82+
83+
export default withMermaid(defineConfig({
84+
85+
// 路由重写:将 en 目录映射到根路径,作为默认语言内容
86+
rewrites: {
87+
'en/index.md': 'index.md', // 英文首页映射到根路径
88+
'en/:dir/:rest*': ':dir/:rest*', // 英文内容映射到根路径
89+
// zh 目录保持 /zh/ 前缀(无需路由重写,直接跳转到/zh/)
90+
},
91+
92+
// 排除目录
93+
srcExclude: ['**/docs/**'],
94+
95+
//主题配置(全局配置,会被 locales 中的配置继承)
96+
themeConfig: {
97+
logo: SITE_CONFIG.logo,//左上角logo
98+
siteTitle: SITE_CONFIG.siteTitle,//左上角标题
99+
100+
socialLinks: [//外部链接图标配置
101+
{ icon: 'github', link: 'https://github.com/datawhalechina/faster-git' },
102+
],
103+
104+
footer: { //底部版权信息配置
105+
message: '© 2025 Datawhale. All Rights Reserved.',
106+
},
107+
108+
// 全局搜索配置&UI语言设置(英文en无需再次配置)
109+
search: {
110+
provider: 'local',
111+
options: {
112+
locales: {
113+
zh: {
114+
translations: {
115+
button: {
116+
buttonText: '搜索文档',
117+
buttonAriaLabel: '搜索文档'
118+
},
119+
modal: {
120+
noResultsText: '无法找到相关结果',
121+
resetButtonTitle: '清除查询条件',
122+
footer: {
123+
selectText: '选择',
124+
navigateText: '切换',
125+
closeText: '关闭'
126+
}
127+
}
128+
}
129+
}
130+
// 添加其他语言示例:
131+
// fr: { translations: { /* 法语翻译 */ } }
132+
}
133+
}
134+
}
135+
},
136+
137+
138+
// ========== 国际化页面配置 ==========
139+
locales: {
140+
// ---------- 英文配置 ----------拥有默认英文UI文本,无需再次配置
141+
root: {
142+
label: 'English',
143+
lang: 'en',
144+
title: SITE_CONFIG.title,
145+
description: SITE_CONFIG.description,
146+
themeConfig: {
147+
nav: [
148+
{ text: 'Home', link: '/' },
149+
{
150+
text: 'Class Content',
151+
items: [
152+
{ text: 'Chapter 1 Git Introduction', link: '/lecture01/' },
153+
{ text: 'Chapter 2 Git Basic Commands', link: '/lecture02/' },
154+
{ text: 'Chapter 3 Git Branch Management', link: '/lecture03/' },
155+
{ text: 'Chapter 4 Git Tools', link: '/lecture04/' },
156+
{ text: 'Chapter 5 Git Internal Principles', link: '/lecture05/' },
157+
{ text: 'Chapter 6 GitFlow Workflow Practice', link: '/lecture06/' },
158+
{ text: 'Chapter 7 Git Commit Specification', link: '/lecture07/' },
159+
{ text: 'Chapter 8 Github/Gitee Usage', link: '/lecture08/' },
160+
{ text: 'Chapter 9 Git Visual Tools Download', link: '/lecture09/' },
161+
{ text: 'Chapter 10 Git Team Collaboration', link: '/lecture10/' }
162+
]
163+
},
164+
],
165+
// 由于 rewrites 已将 en 映射到根路径,侧边栏也使用根路径
166+
sidebar: createSidebar('docs/en', '/'),// '/'决定“链接挂到哪里”(这些文档对应的网站路径以哪个前缀开头)
167+
}
168+
},
169+
170+
// ---------- 中文配置----------
171+
zh: {
172+
label: '中文',
173+
lang: 'zh-CN',
174+
title: SITE_CONFIG.title,
175+
description: SITE_CONFIG.description,
176+
themeConfig: {
177+
nav: [
178+
{ text: '主页', link: '/zh/' },
179+
{
180+
text: '课程内容',
181+
items: [
182+
{ text: '第一章 Git简介', link: '/zh/lecture01/' },
183+
{ text: '第二章 Git基础命令', link: '/zh/lecture02/' },
184+
{ text: '第三章 Git分支管理', link: '/zh/lecture03/' },
185+
{ text: '第四章 Git工具', link: '/zh/lecture04/' },
186+
{ text: '第五章 Git内部原理', link: '/zh/lecture05/' },
187+
{ text: '第六章 GitFlow工作流实战', link: '/zh/lecture06/' },
188+
{ text: '第七章 Git提交规范', link: '/zh/lecture07/' },
189+
{ text: '第八章 Github/Gitee使用说明', link: '/zh/lecture08/' },
190+
{ text: '第九章 Git可视化工具下载', link: '/zh/lecture09/' },
191+
{ text: '第十章 Git团队协作以及合并时的diff工具', link: '/zh/lecture10/' }
192+
]
193+
},
194+
],
195+
sidebar: createSidebar('docs/zh', '/zh/'),
196+
197+
// 中文 UI 文本配置
198+
outlineTitle: '页面导航',
199+
lastUpdatedText: '最后更新于',
200+
darkModeSwitchLabel: '主题',
201+
sidebarMenuLabel: '菜单',
202+
returnToTopLabel: '回到顶部',
203+
langMenuLabel: '多语言',
204+
205+
docFooter: {
206+
prev: '上一页',
207+
next: '下一页'
208+
}
209+
}
210+
}
211+
},
212+
213+
// ========== 四、默认设置 ==========
214+
215+
// 1.显示最后更新时间
216+
lastUpdated: true,
217+
218+
// 2. Markdown 增强配置
219+
markdown: {
220+
math: true,// 开启数学公式 ($$ E=mc^2 $$)
221+
lineNumbers: true, // 开启行号
222+
languageAlias: { // 语言别名,消除 gitignore/env 警告
223+
'gitignore': 'ini',
224+
'env': 'properties'
225+
},
226+
config: (md) => {// 注册时间线插件
227+
md.use(timeline);
228+
},
229+
},
230+
231+
// 3. Mermaid 配置
232+
mermaid: {// refer to mermaid config options
233+
},
234+
mermaidPlugin: {
235+
class: "mermaid my-class", // set additional css classes for parent container
236+
},
237+
238+
// 4. Vite 构建配置
239+
vite: {
240+
// SSR(服务端渲染)配置
241+
ssr: {
242+
noExternal: ['vitepress-plugin-mermaid', 'mermaid'],// 将这些包打包到输出中,解决 Mermaid 图表在 SSR 时的兼容性问题
243+
},
244+
// 优化mermaid配置
245+
optimizeDeps: {
246+
include: ['mermaid'],// 提前预构建 mermaid,提升开发环境性能
247+
},
248+
// 开发服务器配置
249+
server: {
250+
host: '0.0.0.0', // 监听所有网络接口(可从局域网访问)
251+
port: 5173, // 默认端口
252+
strictPort: false, // 端口被占用时自动尝试下一个端口
253+
allowedHosts: true // 允许所有主机访问(跳过 host 检查)
254+
}
255+
},
256+
257+
// ========== 五、SEO 优化配置 ==========
258+
259+
// 基础 head 标签(全局生效)
260+
head: [
261+
['meta', { name: 'keywords', content: SITE_CONFIG.keywords }],
262+
['meta', { name: 'author', content: SITE_CONFIG.author }],
263+
['meta', { property: 'og:type', content: 'website' }],
264+
['link', { rel: 'icon', href: SITE_CONFIG.favicon.href, type: SITE_CONFIG.favicon.type }],
265+
],
266+
267+
// Sitemap 自动生成(需配置 SITE_CONFIG.url)
268+
...(SITE_CONFIG.url ? { sitemap: { hostname: SITE_CONFIG.url } } : {}),
269+
270+
// 动态生成每个页面的 SEO meta 标签(标题、描述、URL、社交分享图)
271+
transformHead: ({ pageData }) => {
272+
if (!SITE_CONFIG.url) return []
273+
274+
const pageUrl = `${SITE_CONFIG.url}${getPageUrl(pageData.relativePath)}`
275+
const title = pageData.frontmatter.title || pageData.title
276+
const description = pageData.frontmatter.description || pageData.description
277+
const image = `${SITE_CONFIG.url}${SITE_CONFIG.logo}`
278+
279+
return [
280+
// 规范链接(避免重复内容)
281+
['link', { rel: 'canonical', href: pageUrl }],
282+
// Open Graph(Facebook、微信等)
283+
['meta', { property: 'og:url', content: pageUrl }],
284+
['meta', { property: 'og:title', content: title }],
285+
['meta', { property: 'og:description', content: description }],
286+
['meta', { property: 'og:image', content: image }],
287+
// Twitter Card
288+
['meta', { name: 'twitter:card', content: 'summary_large_image' }],
289+
['meta', { name: 'twitter:title', content: title }],
290+
['meta', { name: 'twitter:description', content: description }],
291+
['meta', { name: 'twitter:image', content: image }],
292+
] as HeadConfig[]
293+
},
294+
295+
// 构建完成后自动生成 robots.txt
296+
buildEnd: async (siteConfig) => {
297+
const disallowRules = SITE_CONFIG.robotsDisallow
298+
.map(path => `Disallow: ${path}`)
299+
.join('\n')
300+
301+
const robotsContent = [
302+
'User-agent: *',
303+
'Allow: /',
304+
'',
305+
'# 排除资源文件',
306+
disallowRules,
307+
'',
308+
...(SITE_CONFIG.url ? [`Sitemap: ${SITE_CONFIG.url}/sitemap.xml`] : []),
309+
].join('\n')
310+
311+
await writeFile(join(siteConfig.outDir, 'robots.txt'), robotsContent, 'utf-8')
312+
},
313+
}))

0 commit comments

Comments
 (0)