Skip to content

Commit 59b98a4

Browse files
authored
Merge pull request #1121 from jcb91/t2_0
[toc2] correct toc tree construction
2 parents 08ab4cb + abfc773 commit 59b98a4

File tree

1 file changed

+11
-12
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/toc2

1 file changed

+11
-12
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,6 @@
618618
$("#toc").empty().append(ul);
619619

620620
var depth = 1;
621-
var li = ul; //yes, initialize li with ul!
622621
all_headers = $("#notebook").find(":header"); // update all_headers
623622
var min_lvl = 1 + Number(Boolean(cfg.skip_h1_title)),
624623
lbl_ary = [];
@@ -663,18 +662,16 @@
663662
}
664663

665664
// walk down levels
666-
for (var elm = li; depth < level; depth++) {
667-
var new_ul = $("<ul/>").addClass("toc-item");
668-
elm.append(new_ul);
669-
elm = ul = new_ul;
665+
for (; depth < level; depth++) {
666+
var li = ul.children('li:last-child');
667+
if (li.length < 1) {
668+
li = $('<li>').appendTo(ul);
669+
}
670+
ul = $('<ul class="toc-item">').appendTo(li);
670671
}
671672
// walk up levels
672673
for (; depth > level; depth--) {
673-
// up twice: the enclosing <ol> and <li> it was inserted in
674-
ul = ul.parent();
675-
while (!ul.is('ul')) {
676-
ul = ul.parent();
677-
}
674+
ul = ul.parent().closest('.toc-item');
678675
}
679676

680677
var toc_mod_id = h.attr('id') + '-' + num_str;
@@ -684,8 +681,10 @@
684681
$('<a>').addClass('toc-mod-link').attr('id', toc_mod_id).prependTo(h);
685682

686683
// Create toc entry, append <li> tag to the current <ol>.
687-
li = $('<li>').append($('<span/>').append(make_link(h, toc_mod_id)));
688-
ul.append(li);
684+
ul.append(
685+
$('<li>').append(
686+
$('<span>').append(
687+
make_link(h, toc_mod_id))));
689688
});
690689

691690
// update navigation menu

0 commit comments

Comments
 (0)