-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
57 lines (52 loc) · 1.66 KB
/
index.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
var swig = require('swig');
var _ = require('lodash');
var marked = require('marked');
var path = require('path');
var classHtmlAttribute = /class=['"](.+)['"]/;
var tpl = swig.compileFile(path.resolve(path.dirname(module.filename), 'reveal.html'));
function revealContent(_page) {
var nodes = marked.lexer(_page.content);
var page = _page;
var revealCount = 1;
var revealNodes = _.filter(nodes, function(n) {
if (n.type !== 'html') return false;
var text = n.text.substring(0, n.text.indexOf(">")),
classAttr = classHtmlAttribute.exec(text);
return classAttr && classAttr[1] === 'reveal';
});
_.each(revealNodes, function(n) {
var body = marked.lexer(n.text.substring(n.text.indexOf('>') + 1, n.text.lastIndexOf('<')));
var reveals = [], revealNodes = [], revealTitle = body[0].text;
for (var i = 0; i < body.length; i++) {
if (body[i].type !== 'hr') {
revealNodes.push(body[i]);
}
if (body[i].type === 'hr' || i === body.length - 1) {
revealNodes = _.toArray(revealNodes);
revealNodes.links = {};
reveals.push({
title: revealTitle,
id: _page.progress.current.level + "-" + revealCount + "-" + reveals.length,
content: marked.parser(revealNodes)
});
if (i !== body.length - 1) {
revealTitle = body[++i].text;
revealNodes = [body[i]];
}
}
}
page.content = page.content.replace(n.text, tpl({ reveals: reveals }));
revealCount++;
});
return page;
}
module.exports = {
book: {
assets: "./book",
js: [ "reveal.js" ],
css: [ "reveal.css" ]
},
hooks: {
'page:before': revealContent
}
};