Skip to content

Commit 39e7db7

Browse files
committed
feat: render of sidebar
1 parent 9fb33d4 commit 39e7db7

File tree

1 file changed

+79
-43
lines changed

1 file changed

+79
-43
lines changed

.vitepress/config.mts

+79-43
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,83 @@
1-
import { defineConfig } from 'vitepress'
1+
import fs from 'node:fs/promises';
2+
import { resolve } from 'node:path';
3+
4+
import type { DefaultTheme } from 'vitepress';
5+
import { defineConfig } from 'vitepress';
6+
7+
/**
8+
* 判断路径目标是否为文件夹
9+
* @param {string} path
10+
* @returns
11+
*/
12+
const isDir = async (path: string) => {
13+
try {
14+
const stat = await fs.stat(path);
15+
return stat.isDirectory();
16+
} catch (error) {
17+
return false;
18+
}
19+
};
20+
21+
// 获取根目录
22+
const rootPath = resolve(import.meta.dirname, '../');
23+
24+
// TODO 对于 solution,需要递归读取
25+
// const questions = ['lcci', 'lcof', 'lcof2', 'lcp', 'solution'];
26+
// FIXME lcof 较为特殊,似乎中文名加上空格,导致服务崩溃
27+
// const questions = ['lcci', 'lcof', 'lcof2', 'lcp'];
28+
const questions = ['lcci', 'lcof2', 'lcp'];
29+
const items: Record<string, DefaultTheme.Sidebar> = {};
30+
const rewrites: Record<string, string> = {};
31+
32+
for (const question of questions) {
33+
const files = await fs.readdir(resolve(rootPath, question));
34+
items[question] = [];
35+
for (const file of files) {
36+
if (!(await isDir(resolve(rootPath, question, file)))) {
37+
continue;
38+
}
39+
items[question].push({ text: file, link: `${question}/${file}/README.md` });
40+
rewrites[`${question}/${file}/README.md`] = `${question}/${file}`;
41+
}
42+
}
243

344
// https://vitepress.dev/reference/site-config
445
export default defineConfig({
5-
title: "LeetCode Wiki",
6-
description: "LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解",
7-
locales: {
8-
root: {
9-
label: '中文',
10-
lang: 'cn'
46+
title: 'LeetCode Wiki',
47+
description: 'LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解',
48+
locales: {
49+
root: {
50+
label: '中文',
51+
lang: 'cn',
52+
},
53+
en: {
54+
label: 'English',
55+
lang: 'en-US',
56+
link: '/en',
57+
},
1158
},
12-
en: {
13-
label: 'English',
14-
lang: 'en-US',
15-
link: '/en'
16-
}
17-
},
18-
markdown: {
19-
math: true
20-
},
21-
themeConfig: {
22-
// https://vitepress.dev/reference/default-theme-config
23-
nav: [
24-
{ text: 'Home', link: '/' },
25-
{ text: 'LeetCode', link: '/lc/index' }
26-
],
27-
socialLinks: [
28-
{ icon: 'github', link: 'https://github.com/doocs/leetcode' }
29-
],
30-
sidebar: [
31-
{
32-
text: 'LeetCode',
33-
items: [
34-
{ text: '0001.Two Sum', link: '/lc/1.md' },
35-
{ text: '0002.Add Two Numbers', link: '/lc/2.md'},
36-
{ text: '0003.Longest Substring Without Repeating Characters', link: '/lc/3.md'},
37-
]
38-
}
39-
]
40-
},
41-
rewrites: {
42-
'solution/README.md': 'lc/index.md',
43-
'solution/README_EN.md': 'en/lc/index.md',
44-
'solution/0000-0099/0001.Two Sum/README.md': 'lc/1.md',
45-
'solution/0000-0099/0001.Two Sum/README_EN.md': 'en/lc/1.md',
46-
}
47-
})
59+
markdown: {
60+
math: true,
61+
},
62+
themeConfig: {
63+
// https://vitepress.dev/reference/default-theme-config
64+
nav: [
65+
{ text: 'Home', link: '/' },
66+
{ text: 'LeetCode', link: '/lc/index' },
67+
...questions.map(question => ({ text: question, link: `/${question}/README.md` })),
68+
],
69+
socialLinks: [{ icon: 'github', link: 'https://github.com/doocs/leetcode' }],
70+
sidebar: questions.reduce((r, question) => {
71+
return {
72+
...r,
73+
[`/${question}/`]: [
74+
{
75+
text: question,
76+
items: items[question],
77+
},
78+
],
79+
};
80+
}, {}),
81+
},
82+
rewrites,
83+
});

0 commit comments

Comments
 (0)