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
|
var MarkdownIt = require('markdown-it');
const fs = require("fs");
const path = require("path");
md = new MarkdownIt();
const html_header = `
<!doctype HTML>
<html><head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@exampledev/new.css@1/new.min.css">
<link rel="stylesheet" href="https://fonts.xz.style/serve/inter.css">
</head><body>
`
const html_footer = `
</body></html>
`
const walkSync = (inputdir, outputdir, callback) => {
const files = fs.readdirSync(inputdir);
files.forEach((file) => {
var filepath = path.join(inputdir, file);
const stats = fs.statSync(filepath);
if (stats.isDirectory()) {
walkSync(filepath, outputdir, callback);
} else if (stats.isFile()) {
callback(filepath, outputdir, stats);
}
});
};
function fileHandler (filepath, outputdir, stats) {
fs.readFile(filepath, function(err, buf) {
let outputpath = filepath.replace(/^.*?\//i, outputdir);
fs.mkdir(path.dirname(outputpath), { recursive: true }, (err) => {
if (err) throw err;
});
outputpath = outputpath.replace(/\.md$/i, '.html');
const html = html_header + md.render(buf.toString()) + html_footer;
fs.writeFile(outputpath, html, (err) => {
if (err) console.log(err);
console.log("Successfully Written to: " + outputpath);
});
});
}
fs.rmSync('build/', { recursive: true, force: true });
walkSync('src/', '/home/menix/public_html/', fileHandler);
|