feat: 优化内容展示
This commit is contained in:
@@ -1,136 +0,0 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function generateSidebarConfig(dirPath) {
|
||||
const absolutePath = path.resolve(process.cwd(), dirPath);
|
||||
const sidebarItems = [];
|
||||
|
||||
// 如果根目录有 README.md,添加空字符串
|
||||
if (fs.existsSync(path.join(absolutePath, "README.md"))) {
|
||||
sidebarItems.push("");
|
||||
}
|
||||
|
||||
// 读取目录内容
|
||||
function scanDirectory(currentPath, relativeDirPath = "") {
|
||||
const items = [];
|
||||
const files = fs.readdirSync(currentPath);
|
||||
|
||||
files.forEach((file) => {
|
||||
const fullPath = path.join(currentPath, file);
|
||||
const stat = fs.statSync(fullPath);
|
||||
|
||||
if (stat.isFile() && file.endsWith(".md") && file !== "README.md") {
|
||||
// 如果有相对路径,添加相对路径
|
||||
const relativePath = relativeDirPath
|
||||
? `${relativeDirPath}/${file.replace(".md", "")}`
|
||||
: file.replace(".md", "");
|
||||
items.push(relativePath);
|
||||
}
|
||||
});
|
||||
|
||||
return items.sort();
|
||||
}
|
||||
|
||||
// 检查目录是否包含子目录
|
||||
function hasSubDirectories(dirPath) {
|
||||
const items = fs.readdirSync(dirPath);
|
||||
return items.some((item) => {
|
||||
const fullPath = path.join(dirPath, item);
|
||||
return fs.statSync(fullPath).isDirectory();
|
||||
});
|
||||
}
|
||||
|
||||
// 处理目录
|
||||
function processDirectory(currentPath, isRoot = true) {
|
||||
const dirs = fs.readdirSync(currentPath, { withFileTypes: true });
|
||||
|
||||
// 如果当前目录只包含文件(没有子目录)
|
||||
if (!hasSubDirectories(currentPath)) {
|
||||
const files = scanDirectory(currentPath);
|
||||
if (files.length > 0) {
|
||||
sidebarItems.push({
|
||||
title: path.basename(currentPath),
|
||||
collapsable: false,
|
||||
children: files,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理包含子目录的情况
|
||||
dirs.forEach((dir) => {
|
||||
if (dir.isDirectory() && !dir.name.startsWith(".")) {
|
||||
const fullPath = path.join(currentPath, dir.name);
|
||||
const children = [];
|
||||
|
||||
// 扫描子目录中的文件
|
||||
function scanSubDirectory(subPath, relPath) {
|
||||
const subItems = fs.readdirSync(subPath);
|
||||
subItems.forEach((item) => {
|
||||
const itemPath = path.join(subPath, item);
|
||||
const itemStat = fs.statSync(itemPath);
|
||||
|
||||
if (itemStat.isFile() && item.endsWith(".md")) {
|
||||
if (item === "README.md") {
|
||||
// children.push(`${relPath}/`);
|
||||
} else {
|
||||
// 如果包含特殊字符
|
||||
if (item.includes("!")) {
|
||||
children.push(`${relPath}/${encodeURI(item.replace(".md", ""))}`);
|
||||
} else {
|
||||
children.push(`${relPath}/${item.replace(".md", "")}`);
|
||||
}
|
||||
}
|
||||
} else if (itemStat.isDirectory()) {
|
||||
scanSubDirectory(itemPath, `${relPath}/${item}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
scanSubDirectory(fullPath, `${dir.name}`);
|
||||
|
||||
if (children.length > 0) {
|
||||
sidebarItems.push({
|
||||
title: dir.name,
|
||||
collapsable: true,
|
||||
children: children.sort(),
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
processDirectory(absolutePath);
|
||||
return sidebarItems;
|
||||
}
|
||||
|
||||
// 接收命令行参数
|
||||
const targetDir = process.argv[2] || ".";
|
||||
|
||||
try {
|
||||
// 检查目录是否存在
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
throw new Error(`目录 "${targetDir}" 不存在`);
|
||||
}
|
||||
|
||||
// 生成配置
|
||||
const sidebarConfig = generateSidebarConfig(targetDir);
|
||||
|
||||
// 生成 TypeScript 代码
|
||||
const tsContent = `// 自动生成的侧边栏配置
|
||||
|
||||
export default ${JSON.stringify(sidebarConfig, null, 2)};
|
||||
|
||||
`;
|
||||
|
||||
// 写入文件
|
||||
fs.writeFileSync("temp.ts", tsContent, "utf-8");
|
||||
console.log("侧边栏配置已生成到 temp.ts 文件中");
|
||||
|
||||
// 输出生成的配置预览
|
||||
console.log("\n生成的配置预览:");
|
||||
console.log(JSON.stringify(sidebarConfig, null, 2));
|
||||
} catch (error) {
|
||||
console.error("错误:", error instanceof Error ? error.message : "未知错误");
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function generateSidebarConfig(dirPath) {
|
||||
// 将相对路径转为绝对路径
|
||||
const absolutePath = path.resolve(process.cwd(), dirPath);
|
||||
// 创建一个数组,存储 侧边栏目录列表
|
||||
let sidebarItems = [];
|
||||
// 如果根目录存在 README.md ,添加空字符串
|
||||
if (fs.existsSync(path.join(absolutePath, "README.md"))) {
|
||||
sidebarItems.push("");
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归的处理目录
|
||||
* @param {*} currentPath 当前的目录
|
||||
* @param {*} relativePath 当前目录的相对路径
|
||||
* @param {*} config 当前的配置
|
||||
*/
|
||||
function processDirectory(currentPath, relativePath = "", config = sidebarItems) {
|
||||
// 获取目录下的所有文件
|
||||
const items = fs.readdirSync(currentPath, { withFileTypes: true });
|
||||
// 文件列表
|
||||
const files = [];
|
||||
// 目录列表
|
||||
const directories = [];
|
||||
// 遍历 items 得到所有的文件和目录
|
||||
items.forEach((item) => {
|
||||
if (item.isDirectory() && !item.name.startsWith(".")) {
|
||||
directories.push(item);
|
||||
} else if (item.isFile() && item.name.endsWith(".md") && item.name !== "README.md") {
|
||||
files.push(item);
|
||||
}
|
||||
});
|
||||
// 处理文件
|
||||
if (files.length > 0) {
|
||||
const filePaths = files.map((file) => {
|
||||
const filePath = relativePath
|
||||
? `${relativePath}/${file.name.replace(".md", "")}`
|
||||
: file.name.replace(".md", "");
|
||||
return filePath;
|
||||
});
|
||||
// 将文件路径添加到 config 中
|
||||
filePaths.sort().forEach((filePath) => {
|
||||
config.push(filePath);
|
||||
});
|
||||
}
|
||||
if (directories.length > 0) {
|
||||
directories.forEach((dir) => {
|
||||
const subDirectoryPath = path.join(currentPath, dir.name);
|
||||
const newRelativePath = relativePath ? `${relativePath}/${dir.name}` : dir.name;
|
||||
|
||||
// 查找当前目录是否已经存在于配置中
|
||||
let existingDir = config.find((item) => item.title === dir.name);
|
||||
if (!existingDir) {
|
||||
existingDir = {
|
||||
title: dir.name,
|
||||
collapsable: true,
|
||||
children: [],
|
||||
};
|
||||
config.push(existingDir);
|
||||
}
|
||||
|
||||
processDirectory(subDirectoryPath, newRelativePath, existingDir.children); // 传递 existingDir.children 作为 config
|
||||
});
|
||||
}
|
||||
}
|
||||
processDirectory(absolutePath);
|
||||
// 返回计算得到的 sidebar 数组
|
||||
return sidebarItems;
|
||||
}
|
||||
|
||||
// 接收命令行参数:相对路径;默认使用当前目录作为兜底
|
||||
|
||||
const targetDir = process.argv[2] || ".";
|
||||
|
||||
try {
|
||||
// 检查目录是否存在
|
||||
const isExisting = fs.existsSync(targetDir);
|
||||
if (!isExisting) {
|
||||
throw new Error(`目录 “${targetDir}” 不存在`);
|
||||
}
|
||||
// 目录存在,生成 sidebar 配置数组
|
||||
const sidebarConfig = generateSidebarConfig(targetDir);
|
||||
// 输出内容到 temp.ts 中
|
||||
const content = `
|
||||
export default ${JSON.stringify(sidebarConfig, null, 2)}
|
||||
`;
|
||||
const fileName = "temp.ts";
|
||||
fs.writeFileSync(fileName, content, "utf-8");
|
||||
// 提示生成成功
|
||||
console.log(`侧边栏配置已经生成到 ${fileName} 文件中`);
|
||||
} catch (error) {
|
||||
console.error("错误:", error instanceof Error ? error.message : "未知错误");
|
||||
process.exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user