vuepress 动态读取目录

import fs from 'fs';
import path from 'path';

// 是否折叠侧边栏目录
const collapsible = false;

/**
 * @description: 遍历文件夹下的子文件夹,处理侧边栏目录
 * @param {*} pathStr
 * @return {*}
 */
function mapDirToSidebar(pathStr) {
  const sidebarArr = [];
  const fullPath = path.resolve(__dirname, `..${pathStr}`);
  const files = fs.readdirSync(fullPath);
  files.forEach((fileName) => {
    const childFileName = path.join(fullPath, fileName);
    const stats = fs.statSync(childFileName);
    if (stats.isDirectory()) {
      const textName = fileName.split('-')[1];
      sidebarArr.push({
        text: textName || fileName,
        children: mapDirToSidebar(`${pathStr}/${fileName}`),
        collapsible,
      })
    }
    if (stats.isFile()) {
      sidebarArr.push(`${pathStr}/${fileName}`);
    }
  });
  console.log(`${pathStr}: `, sidebarArr);
  return sidebarArr;
}

export default {
  '/life/': mapDirToSidebar('/life'),
  '/work/': mapDirToSidebar('/work'),
  '/': ["/"],
}