wwwwwllllk 发表于 2024-4-17 18:05:30

node遍历生成md文件展示层级目录结构

!(data/attachment/forum/202404/17/180443rjbg0hkzsb8jv0y1.png)


只需要复制我下面的代码,更改你需要的目录地址就可以生成

```js
const fs = require('fs');
const path = require('path');

function generateMarkdownForDirectory(dirPath, depth = 0, markdown = '') {
const files = fs.readdirSync(dirPath);

files.forEach((file) => {
    const filePath = path.join(dirPath, file);
    const stats = fs.statSync(filePath);

    if (stats.isDirectory()) {
      markdown += `${''.repeat(depth)}- ${file}\n`;
      markdown = generateMarkdownForDirectory(filePath, depth + 1, markdown);
    } else {
      markdown += `${''.repeat(depth)}- [${file}](${filePath})\n`;
    }
});

return markdown;
}

const directoryPath = 'D:\\frontedProject\\scriptcatList';
const markdownContent = generateMarkdownForDirectory(directoryPath);

// fs.writeFileSync('directoryStructure.md', markdownContent);

fs.writeFileSync('scriptList.md', markdownContent);

```

王一之 发表于 2024-4-17 19:53:03

很多markdown编辑器输入 都可以支持生成目录

wwwwwllllk 发表于 2024-4-18 08:31:02

王一之 发表于 2024-4-17 19:53
很多markdown编辑器输入 都可以支持生成目录

vscode支持吗

王一之 发表于 2024-4-18 09:35:54

wwwwwllllk 发表于 2024-4-18 08:31
vscode支持吗

这个就和具体的插件有关系了

wwwwwllllk 发表于 2024-4-18 10:35:07

王一之 发表于 2024-4-18 09:35
这个就和具体的插件有关系了

gg一般用啥编辑器

王一之 发表于 2024-4-18 10:37:43

wwwwwllllk 发表于 2024-4-18 10:35
gg一般用啥编辑器

vscode、idea、typora 混着用,主要vscode和idea自带github copilot提示
页: [1]
查看完整版本: node遍历生成md文件展示层级目录结构