Revert "feat: add sidebarGenerator function to auto generate sidebar based file structure"

This reverts commit ebb4d0a711.
This commit is contained in:
kiameow
2024-07-29 14:05:31 +08:00
parent ebb4d0a711
commit c9127a7bc5
2 changed files with 6 additions and 67 deletions

View File

@@ -1,6 +1,3 @@
import fs from 'fs';
import path from 'path';
export function main_sidebar() {
return [
{
@@ -521,56 +518,3 @@ export function chapter9_old() {
}
]
}
// Function to extract numeric prefix as an array of numbers
function getNumericPrefix(fileName) {
const match = fileName.match(/^(\d+(\.\d+)?(?:\.\d+)*)/);
if (match) {
return match[0].split('.').map(Number); // Convert to array of numbers
}
return [];
}
// Function to compare two numeric prefixes
function compareNumericPrefixes(a, b) {
const prefixA = getNumericPrefix(a);
const prefixB = getNumericPrefix(b);
for (let i = 0; i < Math.max(prefixA.length, prefixB.length); i++) {
const numA = prefixA[i] || 0;
const numB = prefixB[i] || 0;
if (numA !== numB) {
return numA - numB;
}
}
return 0;
}
// Function to generate sidebar items
/**
*
* @param {String} dir the start folder to scan
* @param {String[]} excludeDir exclude unwanted folder
* @returns
*/
export function generateSidebar(dir, excludeDir = []) {
const files = fs.readdirSync(dir);
const sortedFiles = files.sort(compareNumericPrefixes);
return sortedFiles.map((file) => {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
if (excludeDir.includes(file)) {
return null; // Skip excluded directories
}
return {
text: file,
collapsed: true,
items: generateSidebar(fullPath, excludeDir),
};
} else if (file.endsWith('.md')) {
return { text: file.replace('.md', ''), link: `/${fullPath.replace('.md', '')}` };
}
}).filter(Boolean);
}