forked from buildbricks/student-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab-block.js
76 lines (73 loc) · 2.6 KB
/
tab-block.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
/**
* Extends the AsciiDoc syntax to support a tabset element. The tabset is
* created from a dlist that is enclosed in an example block marked with the
* tabs style.
*
* Usage:
*
* [tabs]
* ====
* Tab A::
* +
* --
* Contents of tab A.
* --
* Tab B::
* +
* --
* Contents of tab B.
* --
* ====
*
* To use this extension, register the extension.js file with Antora (i.e.,
* list it as an AsciiDoc extension in the Antora playbook file), combine
* styles.css with the styles for the site, and combine behavior.js with the
* JavaScript loaded by the page.
*
* @author Dan Allen <[email protected]>
*/
const IdSeparatorChar = '-'
const InvalidIdCharsRx = /[^a-zA-Z0-9_]/g
const List = Opal.const_get_local(Opal.module(null, 'Asciidoctor'), 'List')
const ListItem = Opal.const_get_local(Opal.module(null, 'Asciidoctor'), 'ListItem')
const generateId = (str, idx) => `tabset${idx}_${str.toLowerCase().replace(InvalidIdCharsRx, IdSeparatorChar)}`
function tabsBlock () {
this.onContext('example')
this.process((parent, reader, attrs) => {
const createHtmlFragment = (html) => this.createBlock(parent, 'pass', html)
const tabsetIdx = parent.getDocument().counter('idx-tabset')
const nodes = []
nodes.push(createHtmlFragment('<div class="tabset is-loading">'))
const container = this.parseContent(this.createBlock(parent, 'open'), reader)
const sourceTabs = container.getBlocks()[0]
if (!(sourceTabs && sourceTabs.getContext() === 'dlist' && sourceTabs.getItems().length)) return
const tabs = List.$new(parent, 'ulist')
tabs.addRole('tabs')
const panes = {}
sourceTabs.getItems().forEach(([[title], details]) => {
const tab = ListItem.$new(tabs)
tabs.$append(tab)
const id = generateId(title.getText(), tabsetIdx)
tab.text = `[[${id}]]${title.text}`
let blocks = details.getBlocks()
const numBlocks = blocks.length
if (numBlocks) {
if (blocks[0].context === 'open' && numBlocks === 1) blocks = blocks[0].getBlocks()
panes[id] = blocks.map((block) => (block.parent = parent) && block)
}
})
nodes.push(tabs)
nodes.push(createHtmlFragment('<div class="content">'))
Object.entries(panes).forEach(([id, blocks]) => {
nodes.push(createHtmlFragment(`<div class="tab-pane" aria-labelledby="${id}">`))
nodes.push(...blocks)
nodes.push(createHtmlFragment('</div>'))
})
nodes.push(createHtmlFragment('</div>'))
nodes.push(createHtmlFragment('</div>'))
parent.blocks.push(...nodes)
})
}
module.exports.register = (registry, context) => {
registry.block('tabs', tabsBlock)
}