Skip to content

Commit

Permalink
[toc2] correct toc tree construction
Browse files Browse the repository at this point in the history
previously, ul could end up inside another ul without intervening li. It's also now slightly clearer what's going on
  • Loading branch information
jcb91 committed Oct 6, 2017
1 parent 2091dc8 commit abfc773
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@

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

// walk down levels
for (var elm = li; depth < level; depth++) {
var new_ul = $("<ul/>").addClass("toc-item");
elm.append(new_ul);
elm = ul = new_ul;
for (; depth < level; depth++) {
var li = ul.children('li:last-child');
if (li.length < 1) {
li = $('<li>').appendTo(ul);
}
ul = $('<ul class="toc-item">').appendTo(li);
}
// walk up levels
for (; depth > level; depth--) {
// up twice: the enclosing <ol> and <li> it was inserted in
ul = ul.parent();
while (!ul.is('ul')) {
ul = ul.parent();
}
ul = ul.parent().closest('.toc-item');
}

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

// Create toc entry, append <li> tag to the current <ol>.
li = $('<li>').append($('<span/>').append(make_link(h, toc_mod_id)));
ul.append(li);
ul.append(
$('<li>').append(
$('<span>').append(
make_link(h, toc_mod_id))));
});

// update navigation menu
Expand Down

0 comments on commit abfc773

Please # to comment.