My static website generator using poole https://www.xythobuz.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auto_toc.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function generate_toc() {
  2. var output = '<div id="toc">';
  3. output += '<h3 class="toc">Table of Contents</h3>';
  4. output += '<ul>';
  5. var level = 0;
  6. var counters = [ 1, 1, 1 ];
  7. $("#content").children("h2, h3, h4").each(function() {
  8. var this_level = parseInt($(this).prop("tagName").slice(1));
  9. if (level <= 0) level = this_level;
  10. if (this_level > level) {
  11. output += '<ul>';
  12. } else if (this_level < level) {
  13. output += '</ul>';
  14. counters[level - 2] = 1;
  15. }
  16. level = this_level;
  17. var title = '';
  18. var link = '';
  19. for (var lvl = 2; lvl <= this_level; lvl++) {
  20. if (lvl > 2) {
  21. title += ".";
  22. link += ".";
  23. }
  24. title += counters[lvl - 2] - ((lvl < this_level) ? 1 : 0);
  25. link += counters[lvl - 2] - ((lvl < this_level) ? 1 : 0);
  26. }
  27. title += ') ';
  28. title += $(this).text();
  29. link += '_';
  30. link += $('<span>').text($(this).text().toLowerCase().split(' ').join('_')).html();
  31. output += '<li>';
  32. output += '<a href="#' + link + '">' + title + '</a>';
  33. output += '</li>';
  34. $(this).before('<a class="anchor al' + this_level + '" name="' + link + '" href="#' + link + '"></a>')
  35. counters[this_level - 2]++;
  36. });
  37. output += '</ul>';
  38. output += '</div>';
  39. $("#toc_wrap").html(output);
  40. }
  41. function register_toc_toggle() {
  42. $("<a>", {
  43. text: "toggle ToC visibility",
  44. href: "",
  45. id: "toc_toggle",
  46. click: function() {
  47. $("#toc_wrap").toggle("fast");
  48. return false;
  49. },
  50. }).appendTo('#toc_toggle_wrap');
  51. }
  52. generate_toc();
  53. register_toc_toggle();