-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
md2html.js
48 lines (44 loc) · 1.75 KB
/
md2html.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
const {readdir, readFile, realpathSync, stat, writeFile} = require('fs');
const showdown = require('showdown');
const converter = new showdown.Converter();
converter.setFlavor('github');
const basePath = realpathSync('./');
console.log('--- md2html.js ---', basePath);
function md2Html(inFile, outFile) {
if (!inFile.endsWith('.md')) return;
readFile(inFile, 'utf8', (fsReadError, fileContent) => {
if (!fsReadError) {
console.log(inFile);
const htmlContent = converter.makeHtml(fileContent);
writeFile(outFile, htmlContent, (err) => err && console.error(err));
} else {
return console.error(inFile, fsReadError);
}
})
}
readdir(basePath, 'utf8', (fsDirError, fileNameList) => {
if(!fsDirError) {
fileNameList.forEach((file) => {
const fileName = `${basePath}/${file}`;
stat(fileName, (err, stats) => {
if (stats.isDirectory()) {
readdir(fileName, 'utf8', (fsDirError, subFileNameList) => {
if(!fsDirError) {
subFileNameList.forEach((subFile) => {
const subFileName = `${fileName}/${subFile}`;
const outFile = `${basePath}/${subFile.replace('.md', '')}.${file}.html`;
md2Html(subFileName, outFile);
})
} else {
return console.error(fsDirError);
}
})
} else {
md2Html(fileName, fileName.replace('.md', '.html'));
}
})
})
} else {
return console.error(fsDirError);
}
});