From 60f738427565ee3ae46af1e09b9971b990009518 Mon Sep 17 00:00:00 2001 From: Max Goldman Date: Mon, 23 Jul 2018 13:49:49 -0400 Subject: [PATCH] Nest jump links in children for elements that shouldn't contain them directly Also generate links for definition lists and exercise part labels. Closes #19, fixes #20, and closes #21. --- web/handout/handout-render.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/web/handout/handout-render.js b/web/handout/handout-render.js index c156190..03d030e 100644 --- a/web/handout/handout-render.js +++ b/web/handout/handout-render.js @@ -469,8 +469,13 @@ function convertExercise(container, category, heading, content) { } function identifyChunks(dense) { - var jumpable = 'h1, h2, h3, h4, h5, h6, .panel-heading' + (dense ? ', p, pre, ol>li:first-child, ul>li:first-child, table' : ''); + var jumpable = 'h1, h2, h3, h4, h5, h6, .panel-heading' + (dense ? ', p, pre, ol:not(li ol), ul:not(li ul), dl, table, .exercise-part-heading' : ''); var exclude = '.exercise-explain *, .faq h3 + div > p:first-child'; + var nest = { + 'ol, ul': 'li', + 'dl': 'dt', + 'table': 'th, td', + }; var elements = $(jumpable).not(':has(' + jumpable + ')').not(exclude); var chunks = {}; var stopwords = [ @@ -515,8 +520,15 @@ function identifyChunks(dense) { }); } })([], chunks); - elements.filter('[id]').prepend(function() { - return $('').addClass('jump').attr('href', '#' + this.id); + elements.filter('[id]').each(function(idx, elt) { + var parent = $(elt); + $.each(nest, function(outer, inner) { + if (parent.is(outer)) { + parent = $(inner, parent).first(); + return false; + } + }); + parent.prepend($('').addClass('jump').attr('href', '#' + elt.id)); }); }