Repository navigation
Expand file tree
/
Copy pathsidebars.js
More file actions
65 lines (60 loc) · 1.7 KB
/
Copy pathsidebars.js
File metadata and controls
65 lines (60 loc) · 1.7 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
const { readdirSync, readFileSync } = require("fs");
const { resolve, relative, dirname } = require("path");
const DocUtils = require("@docusaurus/utils");
const docsPath = resolve(__dirname, "docs");
function getFiles(dir) {
const dirents = readdirSync(dir, { withFileTypes: true }).filter(
(dirent) => dirent.name !== "contribute.md"
);
const files = dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
return {
type: "category",
label: dirent.name,
items: getFiles(res),
};
} else {
const fileString = readFileSync(res, "utf-8");
const { frontMatter } = DocUtils.parse(fileString);
const relativePath = dirname(relative(docsPath, res));
return {
type: "doc",
id:
relativePath === "."
? frontMatter.id
: relativePath + "/" + frontMatter.id,
};
}
});
return Array.prototype.concat(...files).sort((a, b) => {
if (a.type === "category" && b.type === "doc") {
return 1;
} else if (a.type === "doc" && b.type === "category") {
return -1;
} else if (a.type === "doc" && b.type === "doc") {
const aId = a.id.toLowerCase();
const bId = b.id.toLowerCase();
if (aId < bId) {
return -1;
}
if (aId > bId) {
return 1;
}
return 0;
} else if (a.type === "category" && b.type === "category") {
const aLabel = a.label.toLowerCase();
const bLabel = b.label.toLowerCase();
if (aLabel < bLabel) {
return -1;
}
if (aLabel > bLabel) {
return 1;
}
return 0;
}
});
}
module.exports = {
docs: getFiles(docsPath),
};