Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function addTableOfContents(){
- var content = document.getElementById("contentsjs");
- if(content === null){
- console.error("No #content block found");
- }
- var headings = document.querySelectorAll("h1,h2,h3,h4,h5,h6,h7")
- var currentDepth = 0;
- var stackTags = new Array();
- for(var i = 0; i < headings.length; i++){
- // We then need to find out the depth.
- var currentHeader = headings[i];
- // Get the string of the header
- var tagDepth = currentHeader.tagName.substring(1);
- var intDepth = parseInt(tagDepth);
- if( intDepth < currentDepth ){
- // This means we've dropped back down. Close tags
- // Number of tags is abs(intDepth - currentDepth)
- var closeNum = Math.abs(intDepth - currentDepth);
- currentDepth -= closeNum;
- for(var x = 0; x < closeNum; x++){
- // Pop the top. No other things must happen
- stackTags.pop();
- }
- }
- else if( intDepth > currentDepth){
- // This means we need to add a level tag
- // We need the number
- var openNum = Math.abs(intDepth - currentDepth);
- currentDepth += openNum;
- for(var x = 0; x < openNum; x++){
- var openNew = document.createElement("ul")
- stackTags.put(openNew);
- }
- }
- else{
- // Do not close or open any new tags
- }
- var newLI = document.createElement("li")
- newLI.innerHTML = "New Thing";
- stackTags.slice(-1)[0].appendChild(newLI);
- }
- }
- /**
- * This will back link all <span class='backlink'>
- */
- function backLink(){
- }
- function onLoad(){
- addTableOfContents();
- backLink();
- }
- document.addEventListener('DOMContentLoaded', onLoad, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement