-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbocco.js
49 lines (39 loc) · 1.47 KB
/
bocco.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
var marked = require('marked');
var renderer = new marked.Renderer();
var format = function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] !== 'undefined' ? args[number] : match;
});
};
module.exports = function(md, template) {
var section, title, toc = [];
renderer.heading = function(text, level) {
var code = (text.match(/<code>.+<\/code>/g, '') || [''])[0];
if (code) text = text.replace(/<code>.+<\/code>/g, '');
var id = text.toLowerCase().trim().replace(/[^\w]+/g, '-');
if (level === 1) title = text;
else if (level === 2) section = id;
else if (level === 3 && section) id = section + '/' + id;
toc.push(format('<h{0}><a href="#{1}">{2}</a></h{0}>', level, id, level === 3 ? ' - ' + text : text));
return format('<h{0}><a name="{1}" class="anchor" href="#{1}">{2}</a>{3}<span class="ac">¶</span></h{0}>', level, id, text, code);
};
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false
});
// Synchronous highlighting with highlight.js
// marked.setOptions({
// highlight: function (code) {
// return require('highlight.js').highlightAuto(code).value;
// }
// });
var html = marked(md, { renderer: renderer });
return format(template, title, toc.join(''), html);
};