-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
84 lines (77 loc) · 2.59 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var fs = require('fs');
var 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
});
var templateHtml = '';
fs.readFile('template.html', function (err, data) {
if (err)
throw err;
if (data)
templateHtml = data.toString('utf8');
});
fs.readdir(__dirname + '/posts/', function(err, files) {
if (err) return;
files.forEach(function(f) {
if (f.indexOf('.md') != -1 && f != 'home.md') {
//- [Markdown Test Page](#lorem-ipsum)
fs.readFile(__dirname + '/posts/' + f, 'utf8', (err, data) => {
if (err) throw err;
var firstLine = data.split("\n")[0]; // READ FIRST LINE
var title = firstLine.slice(2, firstLine.length-1); // REMOVE "# " and "\n"
var markAnchor = '- [' + title + '](#' + f + ')';
fs.appendFile('routes.md', markAnchor + "\n", (err) => {
if (err) throw err;
console.log(markAnchor + " is saved in routes.md");
});
});
}
})
});
fs.readdir(__dirname + '/posts/', function(err, files) {
if (err) return;
files.forEach(function(f) {
if (f.indexOf('.md') != -1) {
var htmlOutput = __dirname + '/posts/' + f.replace('.md', '.html');
var postContent = '';
var htmlContent = '';
var metaData = '';
fs.readFile(__dirname + '/posts/' + f, function (err, data) {
if (err)
throw err;
if (data) {
var markdownPost = data.toString('utf8');
var lines = markdownPost.split('\n');
var title = '';
if (lines.length > 0) {
title = lines[0].replace(/#/g, '').replace("\r\n", '').replace("\n", '');
if (lines.length > 6 && lines[lines.length - 6].indexOf('<meta') == 0) {
metaData = lines.slice(lines.length - 6).join('\n');
markdownPost = markdownPost.split('\n');
markdownPost.splice(markdownPost.length - 6);
markdownPost = markdownPost.join('\n');
}
}
postContent = marked(markdownPost);
htmlContent = templateHtml.replace('{%content%}', postContent);
htmlContent = htmlContent.replace('{%title%}', title);
htmlContent = htmlContent.replace('{%meta%}', metaData);
fs.writeFile(htmlOutput, htmlContent, function (err) {
if (err)
throw err;
else
console.log('>>', htmlOutput);
});
}
});
}
});
});