-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.js
67 lines (58 loc) · 1.93 KB
/
generator.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
62
63
64
65
66
67
const fs = require('fs');
const path = require('path');
const marked = require('./js/marked.js');
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false, // IMPORTANT, because we do MathJax before markdown,
// however we do escaping in 'CreatePreview'.
smartLists: true,
smartypants: false
});
let template = '';
console.log('Loading template...');
template = fs.readFileSync('template.html', { encoding: 'utf8' });
console.log('Generating static files...');
let pages = [
{ name: 'About Us', slug: 'index'},
{ name: 'What we do?', slug: 'what-we-do'},
{ name: 'How we do it?', slug: 'how-we-do'},
{ name: 'Get involved', slug: 'get-involved'},
{ name: 'Wanna hack?', slug: 'legacy'}
];
let menuHTML = pages.reduce((html, item) => {
html += `<li><a href="${item.slug}.html">${item.name}</a></li>`;
return html;
}, "");
for (let i = 0; i < pages.length; i++) {
let page = pages[i];
let htmlOutput = __dirname + '/' + page.slug + '.html';
fs.readFile(__dirname + '/src/' + page.slug + '.md', function (err, data) {
if (err)
throw err;
if (data) {
let postContent = '';
let htmlContent = '';
let markdownPost = data.toString('utf8');
let lines = markdownPost.split('\n');
let title = '';
if (lines.length > 0) {
title = lines[0].replace(/#/g, '').replace("\r\n", '').replace("\n", '');
}
// Custom components
postContent = marked(markdownPost);
htmlContent = template.replace('{%content%}', postContent);
htmlContent = htmlContent.replace('{%title%}', title);
htmlContent = htmlContent.replace('{%menu%}', menuHTML);
fs.writeFile(htmlOutput, htmlContent, function (err) {
if (err)
throw err;
else
console.log('>>', htmlOutput);
});
}
});
}