-
Notifications
You must be signed in to change notification settings - Fork 4
/
nbtoc.js
81 lines (66 loc) · 1.79 KB
/
nbtoc.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
77
78
79
// adapted from https://gist.github.com/magican/5574556
function clone_anchor(element) {
// clone link
var h = element.find("div.text_cell_render").find(':header').first();
var a = h.find('a').clone();
var new_a = $("<a>");
new_a.attr("href", a.attr("href"));
// get the text *excluding* the link text, whatever it may be
var hclone = h.clone();
hclone.children().remove();
new_a.text(hclone.text());
return new_a;
}
function ol_depth(element) {
// get depth of nested ol
var d = 0;
while (element.prop("tagName").toLowerCase() == 'ol') {
d += 1;
element = element.parent();
}
return d;
}
function table_of_contents(threshold) {
if (threshold === undefined) {
threshold = 4;
}
var cells = IPython.notebook.get_cells();
var ol = $("<ol/>");
$("#toc").empty().append(ol);
for (var i=0; i < cells.length; i++) {
var cell = cells[i];
if (cell.cell_type !== 'heading') continue;
var level = cell.level;
if (level > threshold) continue;
var depth = ol_depth(ol);
// walk down levels
for (; depth < level; depth++) {
var new_ol = $("<ol/>");
ol.append(new_ol);
ol = new_ol;
}
// walk up levels
for (; depth > level; depth--) {
ol = ol.parent();
}
//
ol.append(
$("<li/>").append(clone_anchor(cell.element))
);
}
$('#toc-wrapper .header').click(function(){
$('#toc').slideToggle();
$('#toc-wrapper').toggleClass('closed');
if ($('#toc-wrapper').hasClass('closed')){
$('#toc-wrapper .hide-btn').text('[show]');
} else {
$('#toc-wrapper .hide-btn').text('[hide]');
}
return false;
})
$(window).resize(function(){
$('#toc').css({maxHeight: $(window).height() - 200})
})
$(window).trigger('resize')
}
//table_of_contents();