feat: add sidebarGenerator function to auto generate sidebar based file structure
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
// import { defineConfig } from 'vitepress'
|
// import { defineConfig } from 'vitepress'
|
||||||
import { withMermaid } from "vitepress-plugin-mermaid-xyxsw";
|
import { transformerTwoslash } from '@shikijs/vitepress-twoslash';
|
||||||
|
import PanguPlugin from 'markdown-it-pangu';
|
||||||
|
import { fileURLToPath, URL } from 'node:url';
|
||||||
|
import VueMacros from 'unplugin-vue-macros/vite';
|
||||||
import { VitePWA } from 'vite-plugin-pwa';
|
import { VitePWA } from 'vite-plugin-pwa';
|
||||||
import { main_sidebar, main_sidebar_old, chapter2_old, chapter3_old, chapter4_old, chapter5_old, chapter6_old, chapter7_old, chapter8_old, chapter9_old } from './sidebar.js';
|
import { withMermaid } from "vitepress-plugin-mermaid-xyxsw";
|
||||||
import { nav } from './nav.js';
|
import { nav } from './nav.js';
|
||||||
import PanguPlugin from 'markdown-it-pangu'
|
import { chapter2_old, chapter3_old, chapter4_old, chapter5_old, chapter6_old, chapter7_old, chapter8_old, chapter9_old, generateSidebar, main_sidebar, main_sidebar_old } from './sidebar.js';
|
||||||
import { fileURLToPath, URL } from 'node:url'
|
|
||||||
import VueMacros from 'unplugin-vue-macros/vite'
|
|
||||||
import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
|
|
||||||
|
|
||||||
// https://vitepress.dev/reference/site-config
|
// https://vitepress.dev/reference/site-config
|
||||||
export default withMermaid({
|
export default withMermaid({
|
||||||
@@ -51,6 +51,11 @@ export default withMermaid({
|
|||||||
|
|
||||||
sidebar: {
|
sidebar: {
|
||||||
'/': main_sidebar(),
|
'/': main_sidebar(),
|
||||||
|
'/1.杭电生存指南/': generateSidebar('1.杭电生存指南', ['static']),
|
||||||
|
'/2.编程模块/': generateSidebar('2.编程模块', ['static']),
|
||||||
|
'/3.AI模块/': generateSidebar('3.AI模块', ['static']),
|
||||||
|
'/4.WEB模块/': generateSidebar('4.WEB模块', ['static']),
|
||||||
|
'/5.安全模块/': generateSidebar('5.安全模块', ['static']),
|
||||||
'/2023旧版内容/': main_sidebar_old(),
|
'/2023旧版内容/': main_sidebar_old(),
|
||||||
'/2023旧版内容/2.高效学习/': chapter2_old(),
|
'/2023旧版内容/2.高效学习/': chapter2_old(),
|
||||||
'/2023旧版内容/3.编程思维体系构建/': chapter3_old(),
|
'/2023旧版内容/3.编程思维体系构建/': chapter3_old(),
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
export function main_sidebar() {
|
export function main_sidebar() {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@@ -518,3 +521,56 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user