From dedb66bd80a207b735b56c976882e6e142775148 Mon Sep 17 00:00:00 2001 From: YangFong Date: Thu, 5 Sep 2024 09:27:33 +0800 Subject: [PATCH] feat: render of sidebar --- .vitepress/config.mts | 122 +++++++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 43 deletions(-) diff --git a/.vitepress/config.mts b/.vitepress/config.mts index e9115d9aca681..92121627d785a 100644 --- a/.vitepress/config.mts +++ b/.vitepress/config.mts @@ -1,47 +1,83 @@ -import { defineConfig } from 'vitepress' +import fs from 'node:fs/promises'; +import { resolve } from 'node:path'; + +import type { DefaultTheme } from 'vitepress'; +import { defineConfig } from 'vitepress'; + +/** + * 判断路径目标是否为文件夹 + * @param {string} path + * @returns + */ +const isDir = async (path: string) => { + try { + const stat = await fs.stat(path); + return stat.isDirectory(); + } catch (error) { + return false; + } +}; + +// 获取根目录 +const rootPath = resolve(import.meta.dirname, '../'); + +// TODO 对于 solution,需要递归读取 +// const questions = ['lcci', 'lcof', 'lcof2', 'lcp', 'solution']; +// FIXME lcof 较为特殊,似乎中文名加上空格,导致服务崩溃 +// const questions = ['lcci', 'lcof', 'lcof2', 'lcp']; +const questions = ['lcci', 'lcof2', 'lcp']; +const items: Record = {}; +const rewrites: Record = {}; + +for (const question of questions) { + const files = await fs.readdir(resolve(rootPath, question)); + items[question] = []; + for (const file of files) { + if (!(await isDir(resolve(rootPath, question, file)))) { + continue; + } + items[question].push({ text: file, link: `${question}/${file}/README.md` }); + rewrites[`${question}/${file}/README.md`] = `${question}/${file}`; + } +} // https://vitepress.dev/reference/site-config export default defineConfig({ - title: "LeetCode Wiki", - description: "LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解", - locales: { - root: { - label: '中文', - lang: 'cn' + title: 'LeetCode Wiki', + description: 'LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解', + locales: { + root: { + label: '中文', + lang: 'cn', + }, + en: { + label: 'English', + lang: 'en-US', + link: '/en', + }, }, - en: { - label: 'English', - lang: 'en-US', - link: '/en' - } - }, - markdown: { - math: true - }, - themeConfig: { - // https://vitepress.dev/reference/default-theme-config - nav: [ - { text: 'Home', link: '/' }, - { text: 'LeetCode', link: '/lc/index' } - ], - socialLinks: [ - { icon: 'github', link: 'https://github.com/doocs/leetcode' } - ], - sidebar: [ - { - text: 'LeetCode', - items: [ - { text: '0001.Two Sum', link: '/lc/1.md' }, - { text: '0002.Add Two Numbers', link: '/lc/2.md'}, - { text: '0003.Longest Substring Without Repeating Characters', link: '/lc/3.md'}, - ] - } - ] - }, - rewrites: { - 'solution/README.md': 'lc/index.md', - 'solution/README_EN.md': 'en/lc/index.md', - 'solution/0000-0099/0001.Two Sum/README.md': 'lc/1.md', - 'solution/0000-0099/0001.Two Sum/README_EN.md': 'en/lc/1.md', - } -}) + markdown: { + math: true, + }, + themeConfig: { + // https://vitepress.dev/reference/default-theme-config + nav: [ + { text: 'Home', link: '/' }, + { text: 'LeetCode', link: '/lc/index' }, + ...questions.map(question => ({ text: question, link: `/${question}/README.md` })), + ], + socialLinks: [{ icon: 'github', link: 'https://github.com/doocs/leetcode' }], + sidebar: questions.reduce((r, question) => { + return { + ...r, + [`/${question}/`]: [ + { + text: question, + items: items[question], + }, + ], + }; + }, {}), + }, + rewrites, +});