-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssg.js
executable file
·58 lines (46 loc) · 1.51 KB
/
ssg.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const marked = require('marked');
const hljs = require('highlight.js/lib/common');
hljs.registerLanguage('json', require('highlight.js/lib/languages/json'));
marked.setOptions({
highlight: function(code, lang) {
const highlighted = hljs.highlight(code, {
language: lang,
});
return highlighted.value;
},
});
const inDir = './';
const outDir = (process.argv.length !== 3) ? './' : `./${process.argv[2]}/`;
fs.mkdirSync(outDir, { recursive: true });
const partialsDir = path.join(inDir, 'partials');
const headerHtml = fs.readFileSync(path.join(partialsDir, 'header.html'), 'utf-8');
const footerHtml = fs.readFileSync(path.join(partialsDir, 'footer.html'), 'utf-8');
const pagesDir = path.join(inDir, 'pages');
const pagesFiles = fs.readdirSync(pagesDir, {
withFileTypes: true,
});
for (const pageMd of pagesFiles) {
if (!pageMd.isFile()) {
continue;
}
const mdText = fs.readFileSync(path.join(pagesDir, pageMd.name), 'utf-8');
const htmlText = headerHtml + marked(mdText) + footerHtml;
const pageName = path.parse(pageMd.name).name;
let htmlDir;
if (pageName === 'index') {
htmlDir = outDir;
}
else {
htmlDir = path.join(outDir, pageName);
fs.mkdirSync(htmlDir, { recursive: true });
}
fs.writeFileSync(path.join(htmlDir, 'index.html'), htmlText);
}
if (outDir != './') {
for (file of ["styles.css", "logo.svg", "screenshot.png"]) {
fs.copyFileSync(`${file}`, path.join(outDir, file))
}
}