forked from derbyjs/derby-site
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown.js
222 lines (199 loc) · 6.08 KB
/
markdown.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var marked = require('marked');
var fs = require('fs');
var path = require('path');
// Synchronous highlighting with highlight.js
marked.setOptions({
breaks: true,
highlight: function (code, lang) {
if (lang) {
// highlight.js does not know html, but xml
if (lang === 'html') lang = 'xml';
code = require('highlight.js').highlight(lang, code).value;
} else {
code = require('highlight.js').highlightAuto(code).value;
}
// replace Derby template engine brackets in code examples
code = code.replace(/\{\{/g, '{'); //actually {{
code = code.replace(/\}\}/g, '}}');
// new lines
return code.replace(/\n/g, '<br>');
}
});
// Generate html from # Header levels
function getNavbar(levels) {
var html = '';
var smallest = 5;
for (var i = 0; i < levels.length; i++) {
var level = levels[i][0];
var link = levels[i][1];
var header = levels[i][2];
if (level < smallest) smallest = level;
html += '<li><a href="#' + link + '">' + header + '</a>';
if (i === levels.length - 1) {
html += '</li>';
for (var j = level; j > smallest; j--) {
html += '</ul></li>';
}
} else {
var nextLevel = levels[i + 1][0];
if (level < nextLevel) {
html += '<ul class="nav">';
} else {
html += '</li>';
}
if (level > nextLevel) {
html += '</ul></li>';
}
}
}
return html;
}
// Escape string
function toLink (text) {
return text.toLowerCase().replace(/[^\w]+/g, '-');
}
// Parse markdown file
function parseMarkdown(mdPath) {
var lines = fs.readFileSync(mdPath, {encoding: 'utf8'}).toString().split('\n');
if (lines[0].indexOf('# ') !== 0) {
throw new Error('No # Header in ' + mdPath);
}
var name = lines[0].substr(2);
var description = '';
var startLine = 0;
for (var i = 1; i < lines.length; i++) {
if (lines[i].indexOf('##') !== 0) {
description += lines[i];
} else {
startLine = i;
break;
}
}
var isCode = false;
var file = '';
var levels = [];
for (var i = startLine; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf('```') === 0) {
isCode = !isCode;
}
if (line[0] === '#' & !isCode) {
var level = line.indexOf(' ');
var header = line.substr(level + 1);
var link = toLink(header);
levels.push([level, link, header]);
}
file += line + '\n';
}
return {
file: file,
levels: levels,
link: toLink(name),
name: name,
description: description
}
}
var openTag = '{{markdown}}';
var closeTag = '{{/markdown}}';
// Replace Markdown tags with html
function processTags (from) {
var file = fs.readFileSync(from, {encoding: 'utf8'});
while(true) {
var startIndex = file.indexOf(openTag);
if (startIndex === -1) break;
var endIndex = file.indexOf(closeTag, startIndex);
var startText = startIndex + openTag.length;
var markdown = file.substring(startText, endIndex);
var html = marked(markdown);
file = file.replace(openTag + markdown + closeTag, html);
}
return file;
}
var mdDir = __dirname + '/md';
var appDir = __dirname + '/views/app';
var genDir = __dirname + '/views/gen';
if (!fs.existsSync(genDir)) {
fs.mkdirSync(genDir);
}
var githubPath = 'https://github.com/vmakhaev/derby-site/edit/master/md';
// Replace tags in template with data from Markdown file
function processTemplate (mdPath, content, navbar) {
var file = fs.readFileSync(appDir + '/template.html', {encoding: 'utf8'});
var meta = parseMarkdown(mdDir + '/' + mdPath);
file = file.replace('{{title}}', 'Derby | ' + meta.name);
file = file.replace('{{editLink}}', githubPath + '/' + mdPath);
file = file.replace('{{name}}', meta.name);
file = file.replace('{{description}}', marked(meta.description));
var navbar = navbar || getNavbar(meta.levels);
file = file.replace('{{navbar}}', navbar);
var content = content || marked(meta.file);
file = file.replace('{{content}}', content);
return file;
}
var importHtml = '';
function addToImport (name, ns) {
if (ns) {
importHtml += '<import: src="./' + name + '" ns="' + ns + '">\n';
} else {
importHtml += '<import: src="./' + name + '">\n';
}
}
function processMarkdownDir (from, to, dirName) {
var html = '<ul>';
var fileNames = fs.readdirSync(from);
for (var i = 0; i < fileNames.length; i++) {
var fileName = fileNames[i];
if (fileName === 'index.md') continue;
var filePath = from + '/' + fileName;
var meta = parseMarkdown(filePath);
html += '<li><a href="/' + dirName + '/' + meta.link + '">' + meta.name + '</a></li>';
var file = processTemplate(dirName + '/' + fileName);
var name = path.basename(filePath, '.md');
fs.writeFileSync(to + '/' + name + '.html', file);
addToImport(dirName + '/' + name, dirName + ':' + name);
}
html += '</ul>';
var file = processTemplate(dirName + '/index.md', html, ' ');
fs.writeFileSync(to + '/index.html', file);
}
// Process /md dir
var fileNames = fs.readdirSync(mdDir);
for (var i = 0; i < fileNames.length; i++) {
var fileName = fileNames[i];
var filePath = mdDir + '/' + fileName;
var stat = fs.statSync(filePath);
if (stat.isDirectory()) {
var to = genDir + '/' + fileName;
if (!fs.existsSync(to)) {
fs.mkdirSync(to);
}
processMarkdownDir(filePath, to, fileName);
addToImport(fileName);
} else {
var name = path.basename(filePath, '.md');
var file = processTemplate(fileName);
fs.writeFileSync(genDir + '/' + name + '.html', file);
addToImport(name);
}
}
function processViewsDir (from, to) {
var stat = fs.statSync(from);
if (stat.isDirectory()) {
if (!fs.existsSync(to)) {
fs.mkdirSync(to);
}
var fileNames = fs.readdirSync(from);
for (var i = 0; i < fileNames.length; i++) {
var fileName = fileNames[i];
processViewsDir(from + '/' + fileName, to + '/' + fileName);
}
} else {
var file = processTags(from);
if (from === appDir + '/index.html') {
file = importHtml + file;
}
fs.writeFileSync(to, file);
}
}
// Process /views dir
processViewsDir(appDir, genDir);