vitepress: optimize path handling in vitepress config using BASE_PATH constant

This commit is contained in:
Ruabbit
2024-12-06 13:32:22 +08:00
parent dfa38414df
commit aaf07e2e1f
+10 -12
View File
@@ -2,6 +2,8 @@ import { defineConfig } from "vitepress";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
const BASE_PATH = path.join(__dirname, "../src");
function getMarkdownFiles(dirPath: string): { text: string; link: string }[] { function getMarkdownFiles(dirPath: string): { text: string; link: string }[] {
if (!fs.existsSync(dirPath)) return []; if (!fs.existsSync(dirPath)) return [];
return fs return fs
@@ -21,7 +23,7 @@ function getMarkdownFiles(dirPath: string): { text: string; link: string }[] {
link: link:
"/" + "/" +
path path
.relative(path.join(__dirname, "../src"), path.join(dirPath, file)) .relative(BASE_PATH, path.join(dirPath, file))
.replace(/\.md$/, "") .replace(/\.md$/, "")
.replace(/\\/g, "/"), .replace(/\\/g, "/"),
})); }));
@@ -97,16 +99,14 @@ export default defineConfig({
link: "/wiki/news/recent-news", link: "/wiki/news/recent-news",
collapsed: false, collapsed: false,
items: getMarkdownFiles( items: getMarkdownFiles(
path.join(__dirname, "../src/wiki/news/recent-news") path.join(BASE_PATH, "wiki/news/recent-news")
), ),
}, },
{ {
text: "活动公告", text: "活动公告",
link: "/wiki/news/events", link: "/wiki/news/events",
collapsed: false, collapsed: false,
items: getMarkdownFiles( items: getMarkdownFiles(path.join(BASE_PATH, "wiki/news/events")),
path.join(__dirname, "../src/wiki/news/events")
),
}, },
], ],
}, },
@@ -117,16 +117,14 @@ export default defineConfig({
text: "技术博客", text: "技术博客",
link: "/wiki/tech/blog", link: "/wiki/tech/blog",
collapsed: false, collapsed: false,
items: getMarkdownFiles( items: getMarkdownFiles(path.join(BASE_PATH, "wiki/tech/blog")),
path.join(__dirname, "../src/wiki/tech/blog")
),
}, },
{ {
text: "成员项目", text: "成员项目",
link: "/wiki/tech/projects", link: "/wiki/tech/projects",
collapsed: false, collapsed: false,
items: getMarkdownFiles( items: getMarkdownFiles(
path.join(__dirname, "../src/wiki/tech/projects") path.join(BASE_PATH, "wiki/tech/projects")
), ),
}, },
], ],
@@ -134,7 +132,7 @@ export default defineConfig({
{ {
text: "生活感悟", text: "生活感悟",
link: "/wiki/life", link: "/wiki/life",
items: getMarkdownFiles(path.join(__dirname, "../src/wiki/life")), items: getMarkdownFiles(path.join(BASE_PATH, "wiki/life")),
}, },
{ {
text: "联系方式与友情链接", text: "联系方式与友情链接",
@@ -144,7 +142,7 @@ export default defineConfig({
link: "/wiki/contact/contatusus", link: "/wiki/contact/contatusus",
collapsed: false, collapsed: false,
items: getMarkdownFiles( items: getMarkdownFiles(
path.join(__dirname, "../src/wiki/contact/contatusus") path.join(BASE_PATH, "wiki/contact/contatusus")
), ),
}, },
{ {
@@ -152,7 +150,7 @@ export default defineConfig({
link: "/wiki/contact/link", link: "/wiki/contact/link",
collapsed: false, collapsed: false,
items: getMarkdownFiles( items: getMarkdownFiles(
path.join(__dirname, "../src/wiki/contact/link") path.join(BASE_PATH, "wiki/contact/link")
), ),
}, },
], ],