From a63ddb646cdc0f3e923c7daca5664c971d5612fb Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 5 Nov 2020 14:46:46 +0530 Subject: [PATCH] include anchors in rich text table of contents --- www/pad/inner.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/www/pad/inner.js b/www/pad/inner.js index eeabbb337..235b4fafa 100644 --- a/www/pad/inner.js +++ b/www/pad/inner.js @@ -650,9 +650,25 @@ define([ }, 500); // 500ms to make sure it is sent after chainpad sync }; + var isAnchor = function (el) { return el.nodeName === 'A'; }; + var getAnchorName = function (el) { + return el.getAttribute('id') || + el.getAttribute('data-cke-saved-name') || + el.getAttribute('name') || + Util.stripTags($(el).text()); + }; + var updateTOC = Util.throttle(function () { var toc = []; - $inner.find('h1, h2, h3').each(function (i, el) { + $inner.find('h1, h2, h3, a[id][data-cke-saved-name]').each(function (i, el) { + if (isAnchor(el)) { + return void toc.push({ + level: 2, + el: el, + title: getAnchorName(el), + }); + } + toc.push({ level: Number(el.tagName.slice(1)), el: el, @@ -661,6 +677,8 @@ define([ }); var content = [h('h2', Messages.markdown_toc)]; toc.forEach(function (obj) { + var title = (obj.title || "").trim(); + if (!title) { return; } // Only include level 2 headings var level = obj.level; var a = h('a.cp-pad-toc-link', { @@ -672,7 +690,7 @@ define([ if (!obj.el || UIElements.isVisible(obj.el, $inner)) { return; } obj.el.scrollIntoView(); }); - a.innerHTML = obj.title; + a.innerHTML = title; content.push(h('p.cp-pad-toc-'+level, a)); }); $toc.html('').append(content); @@ -1098,7 +1116,7 @@ define([ */ Ckeditor.dom.element.prototype.setHtml = function(a){ if (/callFunction/.test(a)) { - a = a.replace(/on(mousedown|blur|keydown|focus|click|dragstart)/g, function (value) { + a = a.replace(/on(mousedown|blur|keydown|focus|click|dragstart|mouseover|mouseout)/g, function (value) { return 'o' + value; }); }