This repository has been archived by the owner on Apr 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (51 loc) · 1.44 KB
/
index.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
53
54
55
56
57
58
59
60
61
var fs = require('fs')
var path = require('path')
var remark = require('remark')
var recommended = require('remark-preset-lint-recommended')
var toc = require('remark-toc')
var slug = require('remark-slug')
var html = require('remark-html')
var pipeline = remark().use(recommended).use(toc).use(slug).use(html)
var rootdir = __dirname
var outdir = path.join(__dirname, 'dist')
try {
fs.mkdirSync(outdir)
} catch (e) {
// console.log(e)
}
var dirs = fs.readdirSync(rootdir)
.map(fn => path.join(rootdir, fn))
.map(fp => {
return {
filepath: fp,
stat: fs.statSync(fp)
}
})
.filter(f =>
f.stat.isDirectory()
&& !(f.filepath.endsWith('.git'))
&& !(f.filepath.endsWith('node_modules'))
&& !(f.filepath.endsWith('images'))
&& !(f.filepath === outdir)
)
.map(d => d.filepath)
console.log(dirs)
dirs.map(processDirectory)
function processDirectory (dirpath) {
console.log('Processing files from', dirpath)
var markdown = catMdFiles(dirpath)
var pathparts = dirpath.split('/')
var outfile = path.join(outdir, pathparts[pathparts.length - 1]) + '.html'
pipeline.process(markdown, function(err, file) {
if (err) throw err
console.log(`Writing HTML to ${outfile}`)
fs.writeFileSync(outfile, String(file))
})
}
function catMdFiles (dir) {
return fs.readdirSync(dir)
.filter(fn => fn.endsWith('.md'))
.map(fn => path.join(dir, fn))
.map(fp => fs.readFileSync(fp))
.join("\n\n")
}