-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnameMap.mjs
More file actions
96 lines (88 loc) · 2.65 KB
/
nameMap.mjs
File metadata and controls
96 lines (88 loc) · 2.65 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import fs from "node:fs";
import path from "node:path";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import { unified } from "unified";
import { read } from "to-vfile";
import { compile } from "@mdx-js/mdx";
import remarkFrontmatter from "remark-frontmatter";
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
import { readFile } from "node:fs/promises";
const linkMap = JSON.parse(await readFile(new URL("./src/nav/link.map.json", import.meta.url)));
const folderPath = "./src/content/docs/";
async function mdxCompile(file) {
const { value } = await compile(await readFile(file), {
jsx: true,
remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter],
});
// const data = await eval(`(async () => { ${value} })()`);
console.log(value);
}
// Object.keys(linkMap).forEach(async function (key, index) {
// const file = folderPath + linkMap[key];
// await mdxCompile(file);
// });
await mdxCompile("./src/content/docs/02-app/01-building-your-application/02-data-fetching/index.mdx");
const isFile = (fileName) => {
return fs.lstatSync(fileName).isFile();
};
const isDir = (dirName) => {
return fs.lstatSync(dirName).isDirectory();
};
try {
if (!fs.existsSync(folderPath)) {
// fs.mkdirSync(folderName);
console.error("目录不存在");
}
} catch (err) {
console.error(err);
}
// let files = fs.readdirSync(folderPath);
// files = files
// .map((fileName) => {
// return path.join(folderPath, fileName);
// })
// .filter(isFile);
const filterPath = (filePath) => {
// console.log("filterPath:", filePath);
return filePath.replace("src/content/docs/", ""); //.replace(/(\d{1,2}-)/g, "");
};
const readMdx = async (file) => {
const mdx = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter, ["yaml", "toml"])
.use(remarkMdxFrontmatter)
.use(function () {
return function (tree) {
console.dir(tree);
};
})
.process(await read(file));
console.log(String(mdx));
};
// const nameMap = async () => {
// linkMap.map((dir) => {
// console.log(dir);
// });
// };
// try {
// nameMap();
// } catch (err) {
// console.error("[error]:", err);
// }
// let mapPath = {};
// readDocsDir(folderPath).map((file) => {
// let key = file
// .replace(".mdx", "")
// .replace("/index", "")
// .replace(/(\d{1,2}-)/g, "");
// mapPath[key] = file;
// });
// try {
// fs.writeFileSync("./src/nav/link.map.json", JSON.stringify(mapPath, null, 2), "utf8");
// } catch (err) {
// console.error(err);
// }
// console.log(files);
// console.log(util.inspect(mapPath, { showHidden: false, depth: null, colors: true }));