From dfa38414dfb82fa965e48d37d1191a35bda04030 Mon Sep 17 00:00:00 2001 From: Ruabbit Date: Fri, 6 Dec 2024 13:14:44 +0800 Subject: [PATCH] vitepress: generate sidebar by md files automatically --- .vitepress/config.mts | 289 +++++++++++++++-------- package-lock.json | 18 ++ package.json | 3 +- src/wiki/life/{reflections => }/index.md | 0 src/wiki/news/events/001.md | 0 src/wiki/news/events/002.md | 0 6 files changed, 213 insertions(+), 97 deletions(-) rename src/wiki/life/{reflections => }/index.md (100%) create mode 100644 src/wiki/news/events/001.md create mode 100644 src/wiki/news/events/002.md diff --git a/.vitepress/config.mts b/.vitepress/config.mts index 67996ef..7015ede 100644 --- a/.vitepress/config.mts +++ b/.vitepress/config.mts @@ -1,96 +1,193 @@ -import { defineConfig } from "vitepress"; - -// https://vitepress.dev/reference/site-config -export default defineConfig({ - title: "爱特工作室(CCF海大学生分会)WIKI", - description: "Wiki for ITStudio.", - head: [["link", { rel: "icon", href: "/favicon.ico" }]], - lang: "zh-CN", - srcDir: "src", - themeConfig: { - logo: "/logo.png", - siteTitle: "ITSTUDIO WIKI", - search: { - provider: "local", - }, - nav: [ - { text: "Home", link: "/" }, - { text: "Wiki", link: "/wiki/about/intro" } - ], - - sidebar: { - "/wiki": [ - // { - // text: "ITStudio Wiki", - // link: "/wiki", - // }, - { - text: "团队介绍", - // link: "/wiki/about", - items: [{ text: "爱特工作室简介", link: "/wiki/about/intro" }], - }, - { - text: "新闻公告", - // link: "/wiki/news", - items: [ - { - text: "近期新闻", - link: "/wiki/news/recent-news", - collapsed: false, - items: [], - }, - { - text: "活动公告", - link: "/wiki/news/events", - collapsed: false, - items: [], - }, - ], - }, - { - text: "技术与专业领域", - // link: "/wiki/tech", - items: [ - { - text: "技术博客", - link: "/wiki/tech/blog", - collapsed: false, - items: [], - }, - { - text: "成员项目", - link: "/wiki/tech/projects", - collapsed: false, - items: [], - }, - ], - }, - { - text: "生活感悟", - link: "/wiki/life", - }, - { - text: "联系方式与友情链接", - items: [ - { - text: "联系方式", - link: "/wiki/contact/contatusus", - collapsed: false, - items: [], - }, - { - text: "友情链接", - link: "/wiki/contact/link", - collapsed: false, - items: [], - }, - ], - }, - ], - }, - socialLinks: [ - { icon: "googlehome", link: "https://www.itstudio.club/" }, - { icon: "github", link: "https://github.com/ITStudioOUC" }, - ], - }, -}); +import { defineConfig } from "vitepress"; +import fs from "fs"; +import path from "path"; + +function getMarkdownFiles(dirPath: string): { text: string; link: string }[] { + if (!fs.existsSync(dirPath)) return []; + return fs + .readdirSync(dirPath) + .filter( + (file) => + file.endsWith(".md") && + !file.startsWith(".") && + !file.startsWith("_") && + file !== "index.md" + ) + .sort() + .map((file) => ({ + text: + file.replace(/\.md$/, "").charAt(0).toUpperCase() + + file.replace(/\.md$/, "").slice(1), + link: + "/" + + path + .relative(path.join(__dirname, "../src"), path.join(dirPath, file)) + .replace(/\.md$/, "") + .replace(/\\/g, "/"), + })); +} + +// https://vitepress.dev/reference/site-config +export default defineConfig({ + title: "爱特工作室(CCF海大学生分会)WIKI", + description: "Wiki for ITStudio.", + head: [ + ["link", { rel: "icon", href: "/favicon.ico" }], + ["meta", { name: "theme-color", content: "#3eaf7c" }], + ["meta", { name: "apple-mobile-web-app-capable", content: "yes" }], + [ + "meta", + { name: "apple-mobile-web-app-status-bar-style", content: "black" }, + ], + ], + lang: "zh-CN", + srcDir: "src", + lastUpdated: true, + + themeConfig: { + logo: "/logo.png", + siteTitle: "ITSTUDIO WIKI", + + // Enable search + search: { + provider: "local", + options: { + translations: { + button: { + buttonText: "搜索文档", + buttonAriaLabel: "搜索文档", + }, + modal: { + noResultsText: "无法找到相关结果", + resetButtonTitle: "清除查询条件", + footer: { + selectText: "选择", + navigateText: "切换", + closeText: "关闭", + }, + }, + }, + }, + }, + + // Navigation + nav: [ + { text: "Home", link: "/" }, + { text: "Wiki", link: "/wiki/about/intro" }, + { + text: "相关链接", + items: [ + { text: "官方网站", link: "https://www.itstudio.club/" }, + { text: "GitHub", link: "https://github.com/ITStudioOUC" }, + ], + }, + ], + + sidebar: { + "/wiki": [ + { + text: "团队介绍", + items: [{ text: "爱特工作室简介", link: "/wiki/about/intro" }], + }, + { + text: "新闻公告", + items: [ + { + text: "近期新闻", + link: "/wiki/news/recent-news", + collapsed: false, + items: getMarkdownFiles( + path.join(__dirname, "../src/wiki/news/recent-news") + ), + }, + { + text: "活动公告", + link: "/wiki/news/events", + collapsed: false, + items: getMarkdownFiles( + path.join(__dirname, "../src/wiki/news/events") + ), + }, + ], + }, + { + text: "技术与专业领域", + items: [ + { + text: "技术博客", + link: "/wiki/tech/blog", + collapsed: false, + items: getMarkdownFiles( + path.join(__dirname, "../src/wiki/tech/blog") + ), + }, + { + text: "成员项目", + link: "/wiki/tech/projects", + collapsed: false, + items: getMarkdownFiles( + path.join(__dirname, "../src/wiki/tech/projects") + ), + }, + ], + }, + { + text: "生活感悟", + link: "/wiki/life", + items: getMarkdownFiles(path.join(__dirname, "../src/wiki/life")), + }, + { + text: "联系方式与友情链接", + items: [ + { + text: "联系方式", + link: "/wiki/contact/contatusus", + collapsed: false, + items: getMarkdownFiles( + path.join(__dirname, "../src/wiki/contact/contatusus") + ), + }, + { + text: "友情链接", + link: "/wiki/contact/link", + collapsed: false, + items: getMarkdownFiles( + path.join(__dirname, "../src/wiki/contact/link") + ), + }, + ], + }, + ], + }, + + // Social links + socialLinks: [ + { icon: "github", link: "https://github.com/ITStudioOUC/ITStudio-Wiki" }, + ], + + // Footer + footer: { + message: "Released under the Apache License 2.0", + copyright: "Copyright 2002-present ITStudio", + }, + + // Edit link + editLink: { + pattern: + "https://github.com/ITStudioOUC/ITStudio-Wiki/edit/main/src/:path", + text: "在 GitHub 上编辑此页", + }, + + // Doc footer + docFooter: { + prev: "上一页", + next: "下一页", + }, + + // Outline + outline: { + level: [2, 3], + label: "目录", + }, + }, +}); diff --git a/package-lock.json b/package-lock.json index 3d6850c..a0d7ad0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "packages": { "": { "devDependencies": { + "@types/node": "^18.0.0", "vitepress": "^1.5.0" } }, @@ -1136,6 +1137,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/node": { + "version": "18.19.67", + "resolved": "https://registry.npmmirror.com/@types/node/-/node-18.19.67.tgz", + "integrity": "sha512-wI8uHusga+0ZugNp0Ol/3BqQfEcCCNfojtO6Oou9iVNGPTL6QNSdnUdqq85fRgIorLhLMuPIKpsN98QE9Nh+KQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~5.26.4" + } + }, "node_modules/@types/unist": { "version": "3.0.3", "resolved": "https://registry.npmmirror.com/@types/unist/-/unist-3.0.3.tgz", @@ -2204,6 +2215,13 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmmirror.com/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true, + "license": "MIT" + }, "node_modules/unist-util-is": { "version": "6.0.0", "resolved": "https://registry.npmmirror.com/unist-util-is/-/unist-util-is-6.0.0.tgz", diff --git a/package.json b/package.json index e3378b7..34245a9 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "devDependencies": { + "@types/node": "^18.0.0", "vitepress": "^1.5.0" }, "scripts": { @@ -7,4 +8,4 @@ "docs:build": "vitepress build", "docs:preview": "vitepress preview" } -} \ No newline at end of file +} diff --git a/src/wiki/life/reflections/index.md b/src/wiki/life/index.md similarity index 100% rename from src/wiki/life/reflections/index.md rename to src/wiki/life/index.md diff --git a/src/wiki/news/events/001.md b/src/wiki/news/events/001.md new file mode 100644 index 0000000..e69de29 diff --git a/src/wiki/news/events/002.md b/src/wiki/news/events/002.md new file mode 100644 index 0000000..e69de29