forked from linwu-hi/coding-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateMarkdownToc.ts
38 lines (30 loc) · 986 Bytes
/
generateMarkdownToc.ts
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
37
38
// @ts-nocheck
import fs from 'fs';
import { dirName } from './docs/.vuepress/sidebar/dirName';
function generateMarkdownToc(data, prefix = '') {
let toc = '';
function generateTocItem(item, prefix) {
const link = item.link ? item.link : prefix + item + '.md';
toc += `- [${item.text}](docs/${link})\n`;
if (item.children && item.children.length > 0) {
toc += item.children.map(child => {
return ` - [${child}](docs/${prefix + child}.md)`;
}).join('\n') + '\n';
}
}
data.forEach(item => {
if (typeof item === 'string') {
generateTocItem(item, prefix);
} else {
for (const key in item) {
const subItem = item[key];
const subPrefix = subItem.prefix ? subItem.prefix : '';
generateTocItem(subItem, prefix + subPrefix);
}
}
});
return toc;
}
const markdownToc = generateMarkdownToc(dirName['/lc/']);
console.log(markdownToc);
fs.writeFileSync('dirname.md', markdownToc, 'utf8');