-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
142 lines (114 loc) · 4.59 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var fs = require('fs');
var marked = require('marked');
var hljs = require('highlight.js');
// Various transformation utils
var transform = {
commentLineFeeds: function (data) {
return data
.replace(/(<!-- (?:slide|\.).*? -->)\n([^\n])/g, '$1\n\n$2');
},
renderIframe: function (data) {
return data
.replace(/<!-- render iframe(?: : ([0-9]+))? -->(\n\n```.*?\n([\s\S.]*?)\n```)/g, function (all, height, markedCode, innerCode) {
height = height || 300;
return '<!-- .rendered-iframe -->' + markedCode + '\n\n' +
'<iframe class="rendered-iframe" src="data:text/html,' +
encodeURIComponent(innerCode) +
'" height="' + height + 'px"></iframe>';
});
},
highlight: function (data) {
return data
.replace(/```(.*?)\n([\s\S.]*?)\n```/g, function (all, lang, code) {
if (lang != null && lang !== '') {
// highlight.js doesn't recognize html but xml
var hljsLang = /^x?html\d*$/.test(lang) ? 'xml' : lang;
code = '<pre data-language="' + lang + '"><code class="language-' + lang + '">' + hljs.highlight(hljsLang, code).value + '</code></pre>';
} else {
code = '<pre><code class="language-unknown">' + hljs.highlightAuto(code).value + '</code></pre>';
}
return code;
});
},
tocTiles: function (data, toc) {
return data
.replace(/<h2(.*?)>(.*?)<\/h2>/g, function (all, attributes, title) {
var slug = title
.toLowerCase()
.trim()
.replace(/[^a-z\d]/g, '-')
.replace(/-+/g, '-')
.replace(/-$/, '');
toc.push({title: title, slug: slug});
return '<h2' + attributes + ' id="' + slug + '">' + title + '</h2>';
});
},
insertToc: function (data, toc) {
var starToc = '<div class="toc well">\n<ul class="nav nav-list">\n<li class="nav-header">Table of contents</li>\n',
htmlToc,
endToc = '\n</ul>\n</div>';
htmlToc = starToc + toc.map(this.mapToc).join('\n') + endToc;
return data.replace(/<!-- toc -->/, htmlToc);
},
mapToc: function (title) {
return '<li><a href="#' + title.slug + '">' + title.title + '</a></li>';
},
headAndMetas: function (data) {
return data
.replace(/<!-- title : (.*?) -->/, '<title>$1</title>')
.replace(/<!-- author : (.*?) -->/, '<meta name="author" content="$1">')
.replace(/<!-- description : (.*?) -->/, '<meta name="description" content="$1">')
.replace(/<!-- keywords : (.*?) -->/, '<meta name="keywords" content="$1">');
},
dZSections: function (data) {
return data
.replace(/<!-- slide(?: : (.+))? -->\n/, '<section class="$1">')
.replace(/<!-- slide(?: : (.+))? -->\n/g, '</section>\n\n<section class="$1">')
.replace(/<section class="">/g, '<section>');
},
blockClasses: function (data) {
return data
.replace(/<!-- \.(.+?) -->(\n)*<(.+?)>/g, function (all, classes, lineBreaks, tagAndAttr) {
classes = classes.replace(/\./g, ' ');
return lineBreaks + '<' + tagAndAttr + ' class="' + classes + '">'
});
},
outputLineFeeds: function (data) {
return data
.replace(/(\n)*<\/p>/g, '</p>')
.replace(/<p>(<.*>)<\/p>/g, '$1');
}
};
// Main transformation
var markleft = function (data, fileName) {
var start = '<!DOCTYPE html>\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width">\n',
toc = [],
end = data.indexOf('<!-- slide') ? '\n</section>' : '\n';
data = transform.commentLineFeeds(data);
data = transform.renderIframe(data);
data = transform.highlight(data);
// Standard markdown transformation
data = marked(data);
data = transform.tocTiles(data, toc);
data = transform.insertToc(data, toc);
data = transform.headAndMetas(data);
data = transform.dZSections(data);
data = transform.blockClasses(data);
data = transform.outputLineFeeds(data);
return start + data + end;
};
// Get things done NOW!
['prezas', 'labs']
.forEach(function (outputDir) {
var inputDir = outputDir + '-md';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir)
}
fs.readdirSync(inputDir)
.forEach(function (fileName) {
var mdContents = fs.readFileSync(inputDir + '/' + fileName, 'utf-8'),
htmlContents = markleft(mdContents, fileName);
console.log(inputDir + '/' + fileName + ' updated ;-)');
fs.writeFileSync(outputDir + '/' + fileName.replace(/\.md$/, ".html"), htmlContents);
});
});