-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsidebar.config.mjs
128 lines (107 loc) · 2.83 KB
/
sidebar.config.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import fs from "node:fs/promises";
import path from "node:path";
async function getCategoryLabel(dirPath) {
try {
const categoryPath = path.join(dirPath, "_category_.json");
const content = await fs.readFile(categoryPath, "utf-8");
const { label } = JSON.parse(content);
return label;
} catch {
return path.basename(dirPath);
}
}
async function getMarkdownFileInfo(filePath) {
try {
const content = await fs.readFile(filePath, "utf-8");
// Get title from frontmatter
const titleMatch = content.match(/title:\s*["'](.+)["']/);
const title = titleMatch ? titleMatch[1] : path.basename(filePath, ".md");
// Get slug from frontmatter
const slugMatch = content.match(/slug:\s*([^\n]+)/);
if (!slugMatch) {
return null;
}
// Just use the slug as-is, ensuring it starts with /
const slug = slugMatch[1].trim();
const link = slug.startsWith("/") ? slug : `/${slug}`;
return {
label: title,
link: link,
};
} catch {
return null;
}
}
async function buildSidebarFromDirectory(basePath, currentPath = "") {
const fullPath = path.join(basePath, currentPath);
const entries = await fs.readdir(fullPath, { withFileTypes: true });
const items = [];
// Handle markdown files first
const mdFiles = entries.filter(
(entry) =>
entry.isFile() && entry.name.endsWith(".md") && entry.name !== "index.md",
);
for (const file of mdFiles) {
const filePath = path.join(basePath, currentPath, file.name);
const fileInfo = await getMarkdownFileInfo(filePath);
if (fileInfo) {
items.push(fileInfo);
}
}
// Then handle directories
const directories = entries.filter((entry) => entry.isDirectory());
for (const dir of directories) {
const dirPath = path.join(currentPath, dir.name);
const fullDirPath = path.join(basePath, dirPath);
const label = await getCategoryLabel(fullDirPath);
const subItems = await buildSidebarFromDirectory(basePath, dirPath);
if (subItems.length > 0) {
items.push({
label,
collapsed: true,
items: subItems,
});
} else {
items.push({
label,
collapsed: true,
autogenerate: { directory: dirPath },
});
}
}
return items;
}
export async function generateSidebar() {
const docsPath = path.join(process.cwd(), "src/content/docs");
const navigation = {
label: "Navigation",
items: [
{ label: "Docs", link: "/docs/" },
{ label: "Download", link: "/download/" },
],
};
const supportSection = await buildSidebarFromDirectory(
docsPath,
"docs/support",
);
const policiesSection = {
label: "Policies",
collapsed: true,
autogenerate: { directory: "docs/policies" },
};
const releasesSection = {
label: "Releases",
collapsed: true,
autogenerate: { directory: "docs/releases" },
};
return [
navigation,
policiesSection,
releasesSection,
{
label: "Support",
collapsed: true,
items: supportSection,
},
];
}