-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuTree.mjs
65 lines (61 loc) · 1.81 KB
/
menuTree.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import fs from "node:fs";
import path from "node:path";
import util from "util";
import { read } from "to-vfile";
import { matter } from "vfile-matter";
const isFile = (fileName) => {
return fs.lstatSync(fileName).isFile();
};
const isDir = (dirName) => {
return fs.lstatSync(dirName).isDirectory();
};
const folderPath = "./src/content/";
try {
if (!fs.existsSync(folderPath)) console.error("目录不存在");
} catch (err) {
console.error(err);
}
const filterPath = (filePath) => {
return filePath
.replace("src/content/", "/")
.replace(".mdx", "")
.replace(/(\d{1,2}-)/g, ""); //.replace(/(\d{1,2}-)/g, "");
};
const readDocsDir = async (folderPath, menuMap) => {
let files = fs.readdirSync(folderPath);
for (let fileName of files) {
let filePath = path.join(folderPath, fileName);
if (isFile(filePath)) {
if (filePath.includes("index.mdx")) continue;
const file = await read(filePath);
matter(file, { strip: true });
let { title = "未知" } = file.data.matter || {};
let menu = {
label: title,
href: filterPath(filePath),
};
menuMap.push(menu);
}
if (isDir(filePath)) {
// const file = await read(filePath + "/index.mdx");
// matter(file, { strip: true });
// let { title = "未知" } = file.data.matter || {};
let menu = {
label: "title",
href: filterPath(filePath),
subMenus: [],
};
menuMap.push(menu);
readDocsDir(filePath, menu.subMenus);
}
}
return menuMap;
};
let menuMap = [];
let menus = await readDocsDir(folderPath, menuMap);
console.log(util.inspect(menus, false, null, true /* enable colors */));
// try {
// fs.writeFileSync("./src/nav/link.map.json", JSON.stringify(mapPath, null, 2), "utf8");
// } catch (err) {
// console.error(err);
// }