From c9127a7bc544cde2a0561a8b7b1893a5c5b829e2 Mon Sep 17 00:00:00 2001 From: kiameow Date: Mon, 29 Jul 2024 14:05:31 +0800 Subject: [PATCH] Revert "feat: add sidebarGenerator function to auto generate sidebar based file structure" This reverts commit ebb4d0a7116020c00fb8b332afd6274aaddb4ab5. --- .vitepress/config.js | 17 +++++-------- .vitepress/sidebar.js | 56 ------------------------------------------- 2 files changed, 6 insertions(+), 67 deletions(-) diff --git a/.vitepress/config.js b/.vitepress/config.js index 0c78055..1465c28 100644 --- a/.vitepress/config.js +++ b/.vitepress/config.js @@ -1,12 +1,12 @@ // import { defineConfig } from 'vitepress' -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 { withMermaid } from "vitepress-plugin-mermaid-xyxsw"; +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 { nav } from './nav.js'; -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 PanguPlugin from 'markdown-it-pangu' +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 export default withMermaid({ @@ -51,11 +51,6 @@ 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 1ba42dd..14e3053 100644 --- a/.vitepress/sidebar.js +++ b/.vitepress/sidebar.js @@ -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); -} -