Advertisement
Guest User

Untitled

a guest
Dec 26th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. function addTableOfContents(){
  2. var content = document.getElementById("contentsjs");
  3.  
  4. if(content === null){
  5. console.error("No #content block found");
  6. }
  7.  
  8. var headings = document.querySelectorAll("h1,h2,h3,h4,h5,h6,h7")
  9.  
  10. var currentDepth = 0;
  11.  
  12. var stackTags = new Array();
  13.  
  14. for(var i = 0; i < headings.length; i++){
  15. // We then need to find out the depth.
  16. var currentHeader = headings[i];
  17.  
  18. // Get the string of the header
  19. var tagDepth = currentHeader.tagName.substring(1);
  20. var intDepth = parseInt(tagDepth);
  21.  
  22. if( intDepth < currentDepth ){
  23. // This means we've dropped back down. Close tags
  24. // Number of tags is abs(intDepth - currentDepth)
  25.  
  26. var closeNum = Math.abs(intDepth - currentDepth);
  27. currentDepth -= closeNum;
  28.  
  29. for(var x = 0; x < closeNum; x++){
  30. // Pop the top. No other things must happen
  31. stackTags.pop();
  32. }
  33. }
  34. else if( intDepth > currentDepth){
  35. // This means we need to add a level tag
  36. // We need the number
  37. var openNum = Math.abs(intDepth - currentDepth);
  38. currentDepth += openNum;
  39.  
  40. for(var x = 0; x < openNum; x++){
  41. var openNew = document.createElement("ul")
  42. stackTags.put(openNew);
  43. }
  44. }
  45. else{
  46. // Do not close or open any new tags
  47. }
  48.  
  49. var newLI = document.createElement("li")
  50. newLI.innerHTML = "New Thing";
  51. stackTags.slice(-1)[0].appendChild(newLI);
  52. }
  53. }
  54.  
  55. /**
  56. * This will back link all <span class='backlink'>
  57. */
  58. function backLink(){
  59.  
  60. }
  61.  
  62. function onLoad(){
  63. addTableOfContents();
  64. backLink();
  65. }
  66.  
  67. document.addEventListener('DOMContentLoaded', onLoad, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement