Last active
October 2, 2015 18:26
-
-
Save andypetrella/3e438402d9a030b116b2 to your computer and use it in GitHub Desktop.
Drop this cell into one of your Spark Notebook (https://github.com/andypetrella/spark-notebook/) declaring headings, execute, hide output, hide input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:javascript | |
require(["jquery", "underscore"], function(j, _) { | |
j(".toc").remove(); | |
var toc = j(document.createElement("div")); | |
toc.attr("id", "toc") | |
.addClass("toc") | |
.css("position", "fixed") | |
.css("top", "15%") | |
.css("width", "8%") | |
.addClass("panel").addClass("panel-info"); | |
var tocHeading = j(document.createElement("div")); | |
tocHeading.addClass("panel-heading") | |
.appendTo(toc); | |
var tocTitle = j(document.createElement("h5")); | |
tocTitle.text("Table of Content") | |
.appendTo(tocHeading); | |
var tocBody = j(document.createElement("div")); | |
tocBody.addClass("panel-body") | |
.appendTo(toc); | |
var bodyContent = j(document.createElement("div")); | |
bodyContent.css("max-height", "400px") | |
.css("overflow", "auto") | |
.css("max-width", "400px") | |
.appendTo(tocBody); | |
var notebook = j("#notebook"); | |
var current = 0; | |
var ul, li; | |
var container = bodyContent; | |
j(notebook).find("h1,h2,h3").each(function(i, e) { | |
var lvl = j(e).prop("tagName").substring(1)*1; | |
if (current != lvl) { | |
if (Math.min(current, lvl) == current) { | |
current = lvl; | |
var addUl = function(l) { | |
var c = container; | |
container = j(document.createElement("ul")).css("padding-left", "15px"); | |
container.addClass("lvl"+(current+l)); | |
c.append(container); | |
if (l != 0) addUl(l-1); | |
} | |
addUl(lvl-current); | |
} else { | |
container = container.parents(".lvl"+lvl); | |
current = lvl; | |
} | |
} | |
li = j(document.createElement("li")).html(j(e).html()); | |
var lk = li.find(".anchor-link"); | |
var href = lk.attr("href"); | |
lk.remove(); | |
var a = j(document.createElement("a")).html(li.html()).attr("href", href); | |
li.empty(); | |
li.append(a) | |
li.css("white-space", "nowrap") | |
container.append(li); | |
}); | |
toc.on( | |
{ | |
"mouseenter": function() {j(this).css("width", "20%");}, | |
"mouseleave": function() {j(this).css("width", "8%");}, | |
} | |
); | |
toc.appendTo(j("body")); | |
j('#toc').affix(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment