-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
45 lines (38 loc) · 1.01 KB
/
build.js
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
39
40
41
42
43
44
45
const fs = require('fs')
const path = require('path')
const fg = require('fast-glob')
const linkPrefix = 'https://github.com/L-Chris/blog/blob/master/'
const template = `
# Blog
> 记录一些工作中总结的方案,以及一些问题的解决方法
目录
`
const stream = fg.stream('**/*.md', {
ignore: ['node_modules', 'README.md']
})
const entries = []
stream.on('data', entry => {
if (!entry) return
const parentName = entry.split('/')[0]
const fileName = path.basename(entry, '.md')
const index = entries.findIndex(_ => _.name === parentName)
if (index < 0) {
entries.push({
name: parentName,
children: [fileName]
})
return
}
entries[index].children.push(fileName)
})
stream.once('error', console.log)
stream.once('end', () => {
const content = entries.reduce((pre, {name, children}) => {
pre += `
- ${name}
${children.map(title => ` - [${title}](${linkPrefix}${name}/${title}.md)`).join('\n')}
`
return pre
}, template)
fs.writeFileSync('README.md', content)
})