-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathextra.js
60 lines (48 loc) · 1.78 KB
/
extra.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
// add classes for code-block-filename styling
$('.rst-content pre')
.prev('blockquote')
.addClass('code-block-filename');
var tabConversionIterations = 0
// convert tab admonition to tabs
while (true) {
const firstTab = $('.rst-content .admonition.tab').first()
if (firstTab.length == 0) break;
const otherTabs = firstTab.nextUntil(':not(.admonition.tab)');
const allTabs = $($.merge($.merge([], firstTab), otherTabs));
const tabContainer = $('<div>').addClass('tabs');
const headerContainer = $('<div>').addClass('tabs-header');
const contentContainer = $('<div>').addClass('tabs-content');
tabContainer.insertBefore(firstTab);
tabContainer.append(headerContainer, contentContainer)
// add extra classes from the first tab to the container
const classes = Array.from(firstTab[0].classList)
for (let i = 0; i < classes.length; i++) {
const element = classes[i];
if (element == 'tab' || element == 'admonition') {
continue
}
tabContainer.addClass(element)
}
const selectTab = function (index) {
headerContainer.children().removeClass('active')
headerContainer.children().eq(index).addClass('active')
contentContainer.children().hide()
contentContainer.children().eq(index).show()
}
allTabs.each(function (tabI, el) {
const $el = $(el)
const titleElement = $el.children('.admonition-title')
const title = titleElement.text()
const button = $('<button>').text(title)
button.click(function () {
selectTab(tabI)
})
headerContainer.append(button)
titleElement.remove()
$el.removeClass('admonition')
contentContainer.append($el)
})
selectTab(0)
// this will catch infinite loops which can occur when editing the above
if (tabConversionIterations++ > 1000) throw 'too many iterations'
}