-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-directory-links.js
More file actions
36 lines (30 loc) · 1.14 KB
/
Copy pathgenerate-directory-links.js
File metadata and controls
36 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require('fs');
const path = require('path');
const generateMarkdown = (dir, nestedLevel = 0) => {
let result = '';
const files = fs.readdirSync(dir);
files.forEach(file => {
// 排除.隐藏文件和node_modules目录
if (file.startsWith('.') || file === 'node_modules'|| file === 'assets') return;
// 文件路径
const filePath = path.join(dir, file);
// 获取文件信息
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// 如果是首层目录,使用标题格式;否则使用列表格式
if (nestedLevel === 0) {
result += `### ${file}\n`;
} else {
result += `${' '.repeat(nestedLevel - 1)}- [${file}](${encodeURIComponent(filePath.replace(/\\/g, '/'))}/)\n`;
}
// 递归调用生成子目录的Markdown
result += generateMarkdown(filePath, nestedLevel + 1);
} else if (nestedLevel > 0) {
// 文件使用列表格式
result += `${' '.repeat(nestedLevel - 1)}- [${file}](${encodeURIComponent(filePath.replace(/\\/g, '/'))})\n`;
}
});
return result;
};
let result = `${generateMarkdown('.')}`;
console.log(result);