-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTOC_links.js
118 lines (94 loc) · 3.83 KB
/
TOC_links.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var CONFIRMATION_MESSAGE = "This will edit the markdown cells with titles to remove the links." +
" If you wish to restore the changes, create a backup copy of the Jupyter notebook." +
" Do you want to continue?"
Jupyter.toolbar.add_buttons_group([
// Create table of contents (with links)
// =====================================
{
'label' : 'Create table of contents with links',
'icon' : 'fa-list',
'callback': function(){
if (!confirm(CONFIRMATION_MESSAGE)) return;
Jupyter.notebook.save_notebook(); // save the notebook before making changes
Jupyter.notebook.insert_cell_at_index('code', 0);
// Initalize variables
// -------------------
var toc = '# ' + window.document.getElementById("notebook_name").innerHTML +
'<a id="top"></a>\n---\n## Table of Contents\n',
level1, level2, level3, // counter for levels 1, 2 and 3
txt, title, tag,
edit_markdown = false;
level1 = level2 = level3 = 0;
txt = title = tag = "";
for (var cc = 0; cc < Jupyter.notebook.ncells(); cc++) {
var curr = Jupyter.notebook.get_cell(cc);
if (curr.cell_type != 'markdown') continue;
// Read text
// ---------
txt = curr.get_text();
// Loop over the titles
// --------------------
titles = findTitles(txt);
for (var tt = 0; tt < titles.length; tt++) {
title = titles[tt];
[[level1, level2, level3], tag, toc, edit_markdown] = addToTOC_links(titleToLevel(title),
[level1, level2, level3],
trimTitle(title),
toc);
if (edit_markdown) {
txt = replaceLine(txt, title, titleToLink(title, tag));
}
}
// Edit cell
// ---------
if (titles.length) {
curr.set_text(txt);
curr.execute();
}
}
// Insert TOC at the beginning
// ---------------------------
toc = toc.substring(0, toc.length - 1); // remove the last line break
Jupyter.notebook.get_cell(0).set_text(toc);
Jupyter.notebook.to_markdown(0);
Jupyter.notebook.get_cell(0).execute();
Jupyter.notebook.scroll_to_top();
}
},
// Remove the links created by the function above
// ==============================================
{
'label' : 'Unlink: remove links created with the TOC button',
'icon' : 'fa-unlink',
'callback': function(){
if (!confirm(CONFIRMATION_MESSAGE)) return;
Jupyter.notebook.save_notebook(); // save the notebook before making changes
// Initalize variables
// -------------------
var txt, title, no_link;
txt = title = no_link = '';
for (var i = 0; i < Jupyter.notebook.ncells(); i++) {
var curr = Jupyter.notebook.get_cell(i);
if (curr.cell_type != 'markdown') continue;
// Read text
// ---------
txt = curr.get_text();
// Loop over the titles
// --------------------
var titles = findTitles(txt);
for (var tt = 0; tt < titles.length; tt++) {
title = titles[tt];
no_link = removeLink(title);
if (title != no_link) {
txt = replaceLine(txt, title, no_link);
}
// Edit cell
if (titles.length) {
curr.set_text(txt);
curr.execute();
}}
}
}
}
])
console.log("Custom TOC (with links) button loaded successfully!");