-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
52 lines (42 loc) · 1.18 KB
/
worker.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
46
47
48
49
50
51
52
// cp -rf ./* /home/wwwroot/articles.jerryshi.com
const ejs = require('ejs')
const glob = require('glob')
const fs = require('fs')
const pathList = ['./reading', './learning/fe', './learning/node', './learning/python', './learning/other']
const reg = /# \S+\n$/
const tpl =
`---
sidebar: false
---
# CONTENTS
<% for(let i=0; i < data.length; i++) { %>
* [<%= data[i].name %>](<%= data[i].path %>)
<% } %>`
function getContents(p) {
let list = []
~(async () => {
let ll = glob.sync(p + '/**/*.md').filter(i => i.indexOf('index') < 0)
await Promise.all(
ll.map(async path => {
const data = await fs.readFileSync(path, 'utf8')
const firstLine = data.split('\n')[0]
if (firstLine[0] !== '#') {
console.log('文件:' + path, '存在格式问题')
} else {
list.push({
name: firstLine.replace('# ', ''),
path: path.replace('.', '')
})
}
})
)
const rr = ejs.render(tpl, {
data: list
})
fs.writeFile(p + '/index.md', rr, err => {
if (err) console.error(err)
else console.info('写入 ' + p + '/index.md 成功')
})
})()
}
pathList.forEach(getContents)