Skip to content

Commit abfc773

Browse files
committed
[toc2] correct toc tree construction
previously, ul could end up inside another ul without intervening li. It's also now slightly clearer what's going on
1 parent 2091dc8 commit abfc773

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
@@ -599,7 +599,6 @@
599599

600600
var cell_toc_text = " # Table of Contents\n";
601601
var depth = 1;
602-
var li = ul; //yes, initialize li with ul!
603602
all_headers = $("#notebook").find(":header"); // update all_headers
604603
var min_lvl = 1 + Number(Boolean(cfg.skip_h1_title)),
605604
lbl_ary = [];
@@ -644,18 +643,16 @@
644643
}
645644

646645
// walk down levels
647-
for (var elm = li; depth < level; depth++) {
648-
var new_ul = $("<ul/>").addClass("toc-item");
649-
elm.append(new_ul);
650-
elm = ul = new_ul;
646+
for (; depth < level; depth++) {
647+
var li = ul.children('li:last-child');
648+
if (li.length < 1) {
649+
li = $('<li>').appendTo(ul);
650+
}
651+
ul = $('<ul class="toc-item">').appendTo(li);
651652
}
652653
// walk up levels
653654
for (; depth > level; depth--) {
654-
// up twice: the enclosing <ol> and <li> it was inserted in
655-
ul = ul.parent();
656-
while (!ul.is('ul')) {
657-
ul = ul.parent();
658-
}
655+
ul = ul.parent().closest('.toc-item');
659656
}
660657

661658
var toc_mod_id = h.attr('id') + '-' + num_str;
@@ -665,8 +662,10 @@
665662
$('<a>').addClass('toc-mod-link').attr('id', toc_mod_id).prependTo(h);
666663

667664
// Create toc entry, append <li> tag to the current <ol>.
668-
li = $('<li>').append($('<span/>').append(make_link(h, toc_mod_id)));
669-
ul.append(li);
665+
ul.append(
666+
$('<li>').append(
667+
$('<span>').append(
668+
make_link(h, toc_mod_id))));
670669
});
671670

672671
// update navigation menu

0 commit comments

Comments
 (0)