Skip to content

Commit

Permalink
feat: add sidebarGenerator function to auto generate sidebar based fi…
Browse files Browse the repository at this point in the history
…le structure
  • Loading branch information
Kiameow committed Jul 29, 2024
1 parent 6b5fd1d commit ebb4d0a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
17 changes: 11 additions & 6 deletions .vitepress/config.js
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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(),
Expand Down
56 changes: 56 additions & 0 deletions .vitepress/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs';
import path from 'path';

export function main_sidebar() {
return [
{
Expand Down Expand Up @@ -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);
}

0 comments on commit ebb4d0a

Please sign in to comment.