From ebb4d0a7116020c00fb8b332afd6274aaddb4ab5 Mon Sep 17 00:00:00 2001 From: kiameow Date: Mon, 29 Jul 2024 13:34:38 +0800 Subject: [PATCH] feat: add sidebarGenerator function to auto generate sidebar based file structure --- .vitepress/config.js | 17 ++++++++----- .vitepress/sidebar.js | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/.vitepress/config.js b/.vitepress/config.js index 1465c28..0c78055 100644 --- a/.vitepress/config.js +++ b/.vitepress/config.js @@ -1,12 +1,12 @@ // 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 { 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 PanguPlugin from 'markdown-it-pangu' -import { fileURLToPath, URL } from 'node:url' -import VueMacros from 'unplugin-vue-macros/vite' -import { transformerTwoslash } from '@shikijs/vitepress-twoslash' +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'; // https://vitepress.dev/reference/site-config export default withMermaid({ @@ -51,6 +51,11 @@ export default withMermaid({ 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旧版内容/2.高效学习/': chapter2_old(), '/2023旧版内容/3.编程思维体系构建/': chapter3_old(), diff --git a/.vitepress/sidebar.js b/.vitepress/sidebar.js index 14e3053..1ba42dd 100644 --- a/.vitepress/sidebar.js +++ b/.vitepress/sidebar.js @@ -1,3 +1,6 @@ +import fs from 'fs'; +import path from 'path'; + export function main_sidebar() { 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); +} +