This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
65 lines (54 loc) · 1.75 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
58
59
60
61
62
63
64
65
var escape = require('escape-html');
/*
Generate HTML for the tab in the header
@param {Block}
@param {Boolean}
@return {String}
*/
function createTab(block, i, isActive) {
return '<div class="tab' + (isActive? ' active' : '') + '" data-codetab="' + i + '">' + block.kwargs.name + '</div>';
}
/*
Generate HTML for the tab's content
@param {Block}
@param {Boolean}
@return {String}
*/
function createTabBody(block, i, isActive) {
return '<div class="tab' + (isActive? ' active' : '') + '" data-codetab="' + i + '"><pre><code class="lang-' + (block.kwargs.type || block.kwargs.name) + '">'
+ escape(block.body) +
'</code></pre></div>';
}
module.exports = {
book: {
assets: './assets',
css: [
'codetabs.css'
],
js: [
'codetabs.js'
]
},
blocks: {
codetabs: {
blocks: ['language'],
process: function(parentBlock) {
var blocks = [parentBlock].concat(parentBlock.blocks);
var tabsContent = '';
var tabsHeader = '';
blocks.forEach(function(block, i) {
var isActive = (i == 0);
if (!block.kwargs.name) {
throw new Error('Code tab requires a "name" property');
}
tabsHeader += createTab(block, i, isActive);
tabsContent += createTabBody(block, i, isActive);
});
return '<div class="codetabs">' +
'<div class="codetabs-header">' + tabsHeader + '</div>' +
'<div class="codetabs-body">' + tabsContent + '</div>' +
'</div>';
}
}
}
};