forked from GitbookIO/plugin-codetabs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (56 loc) · 1.74 KB
/
index.js
File metadata and controls
65 lines (56 loc) · 1.74 KB
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
/*
Generate HTML for the tab in the header
@param {Block}
@param {Boolean}
@return {String}
*/
function createTab(block, i, isActive) {
return '<div class="mdtab' + (isActive? ' active' : '') + '" data-mdtab="' + i + '">' + block.kwargs.title + '</div>';
}
/*
Generate HTML for the tab's content
@param {Block}
@param {Boolean}
@return {String}
*/
async function createTabBody(book, block, i, isActive) {
let body = await book.renderBlock('markdown', block.body);
return '<div class="mdtab' + (isActive? ' active' : '') + '" data-mdtab="' + i + '">'
+ body
+ '</div>';
}
module.exports = {
book: {
assets: './assets',
css: [
'mdtabs.css'
],
js: [
'mdtabs.js'
]
},
blocks: {
mdtabs: {
blocks: ['mdtab'],
process: async function(parentBlock) {
var blocks = [parentBlock].concat(parentBlock.blocks);
var tabsContent = '';
var tabsHeader = '';
var i = 0;
for (const block of blocks) {
var isActive = (i == 0);
if (!block.kwargs.title) {
throw new Error('Tab requires a "title" property');
}
tabsHeader += createTab(block, i, isActive);
tabsContent += await createTabBody(this.book, block, i, isActive);
i++;
};
return '<div class="mdtabs">' +
'<div class="mdtabs-header">' + tabsHeader + '</div>' +
'<div class="mdtabs-body">' + tabsContent + '</div>' +
'</div>';
}
}
}
};