From 9a89b69d98a659d55b0adcc20f93fe6b056d1285 Mon Sep 17 00:00:00 2001 From: dschick Date: Fri, 21 Aug 2015 11:35:35 +0200 Subject: [PATCH] allow dublicate headlines in toc extension Adds a random hash to the id and link of each heading in the toc extension. --- nbextensions/toc.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/nbextensions/toc.js b/nbextensions/toc.js index 35eb7e3..ddf2d96 100644 --- a/nbextensions/toc.js +++ b/nbextensions/toc.js @@ -17,13 +17,26 @@ toc.load_ipython_extension(); define(["require", "jquery", "base/js/namespace"], function (require, $, IPython) { "use strict"; + var rand = function() { + return Math.random().toString(36).substr(2); // remove `0.` + }; + var make_link = function (h) { + var a = $(""); - a.attr("href", '#' + h.attr('id')); + + // get the text *excluding* the link text, whatever it may be var hclone = h.clone(); hclone.children().remove(); a.text(hclone.text()); + + var newID = hclone.text().toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-') + '_' + rand(); + + + h.attr('id', newID); + a.attr("href", '#' + newID); + return a; };