Skip to content

Instantly share code, notes, and snippets.

@huehnerhose
Created January 9, 2014 18:29
Show Gist options
  • Save huehnerhose/8339335 to your computer and use it in GitHub Desktop.
Save huehnerhose/8339335 to your computer and use it in GitHub Desktop.
Table of contents script for multimarkdown
(function(){
var toc = $("<div class='toc'>");
toc.append('<ul class="level-1">');
var last = 0;
$(":header").each(function(){
var tagName = $(this).prop("tagName");
tagName = parseInt(tagName.split("H")[1]);
if(tagName < last){
toc.find("ul.level-"+tagName).after($("<ul class='level-"+tagName+"'>"));
}else if(tagName > last){
toc.find(".level-"+last).last().append($("<ul class='level-"+tagName+"'>"));
}
toc.find(".level-"+tagName).last().append("<li class='level-"+tagName+"'><a href='#"+$(this).prop("id")+"'>" + $(this).text() + "</a></li>");
last = tagName;
})
toc.find("ul.level-1").first().before("<h1>Inhaltsverzeichnis</h1>");
$("body").first().prepend(toc);
})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment