-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebarsWorkshops.js
More file actions
73 lines (59 loc) · 2.07 KB
/
sidebarsWorkshops.js
File metadata and controls
73 lines (59 loc) · 2.07 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
const fs = require("fs");
const path = require("path");
/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
function generateSidebar() {
const baseDir = path.join(__dirname, "workshops"); // Adjust this path if your docs directory is located elsewhere
const categories = fs.readdirSync(baseDir).filter((item) => {
const itemPath = path.join(baseDir, item);
return fs.statSync(itemPath).isDirectory() && !item.startsWith(".");
});
const sidebarItems = [];
// sidebarItems.push("intro");
let all_count = 0;
categories.forEach((category) => {
const categoryPath = path.join(baseDir, category);
const count = countItemsInDirectory(categoryPath);
all_count += count;
// Read the '_category_.json' to get the original label and link
const categoryJsonPath = path.join(categoryPath, "_category_.json");
let label = category;
let link = undefined;
if (fs.existsSync(categoryJsonPath)) {
const categoryJson = JSON.parse(
fs.readFileSync(categoryJsonPath, "utf8"),
);
label = categoryJson.label || category;
link = categoryJson.link;
}
// Append the count to the label
label = `${label} (${count} نوشته)`;
const sidebarItem = {
type: "category",
label: label,
items: [{ type: "autogenerated", dirName: category }],
};
if (link) {
sidebarItem.link = link;
}
sidebarItems.push(sidebarItem);
});
// sidebarItems.push({
// type: 'link',
// label: `All Notes: ${all_count} entries`,
// href: '/workshops/intro'
// })
return {
workshopsSidebar: sidebarItems,
};
}
function countItemsInDirectory(dirPath) {
const items = fs.readdirSync(dirPath).filter((item) => {
const itemPath = path.join(dirPath, item);
if (item === "_category_.json" || item.startsWith(".")) return false;
const stat = fs.statSync(itemPath);
// Include only directories and markdown files
return stat.isDirectory() || (stat.isFile() && item.endsWith(".md"));
});
return items.length;
}
module.exports = generateSidebar();