forked from mapschool/mapschool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
70 lines (59 loc) · 1.9 KB
/
build.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
var _ = require('underscore'),
marked = require('marked'),
s = require('underscore.string'),
fs = require('fs');
function rmcomments(_) {
return _.split('\n').filter(function(l) {
return (l.indexOf('%') !== 0);
}).join('\n');
}
var lexed = marked.lexer(rmcomments(fs.readFileSync('README.md', 'utf8')));
var chunks = [];
var chunk = { sections: [] };
chunk.sections.links = {};
lexed.forEach(function(l) {
if (l.type == 'heading') {
if (!_.isEmpty(chunk)) {
chunks.push(chunk);
chunk = {
sections: [
]
};
chunk.sections.links = {};
}
chunk.heading = l;
} else {
chunk.sections.push(l);
}
});
chunks.forEach(function(c) {
if (c.heading) {
fs.writeFileSync('section-' + s.slugify(c.heading.text) + '.html',
_.template(fs.readFileSync('template._', 'utf8'))({
content: marked.parser(c.sections),
title: 'map school: ' + c.heading.text
}));
}
});
var lexed = marked.lexer(rmcomments(fs.readFileSync('README.md', 'utf8')));
lexed.forEach(function(l) {
if (l.type == 'heading') {
l.text = '[' + l.text + '](section-' + s.slugify(l.text) + '.html)';
}
});
var renderer = new marked.Renderer();
renderer.heading = function(text, level) {
var escapedText;
escapedText = s.slugify(text);
return '<h' + level + ' class="h" id="' + escapedText + '">' +
text +
'<a class="permalink" href="#' + escapedText + '">#</a>' +
'<a class="permalink" href="section-' + escapedText + '.html">→</a>' +
'</h' + level + '>\n';
};
var content = marked(rmcomments(fs.readFileSync('README.md', 'utf8')), { renderer: renderer });
fs.writeFileSync('index.html',
_.template(fs.readFileSync('template._', 'utf8'))({
content: content,
title: 'map school'
}));