toc.generator.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*jslint
  2. white: true,
  3. browser: true,
  4. vars: true
  5. */
  6. /**
  7. * Generates a table of contents for your document based on the headings
  8. * present. Anchors are injected into the document and the
  9. * entries in the table of contents are linked to them. The table of
  10. * contents will be generated inside of the first element with the id `toc`.
  11. * @param {HTMLDOMDocument} documentRef Optional A reference to the document
  12. * object. Defaults to `document`.
  13. * @author Matthew Christopher Kastor-Inare III
  14. * @version 20130726
  15. * @example
  16. * // call this after the page has loaded
  17. * generateTOC();
  18. */
  19. /**
  20. * Modified by @danpros
  21. * select only in selector ID
  22. * using the heading title as the slug IDs
  23. * insert the anchor inside the heading
  24. * fix browser not scrolling to the hash
  25. */
  26. function generateTOC (id) {
  27. var documentRef = document;
  28. var selector = id + ' h1,' + id + ' h2,' + id + ' h3,' + id + ' h4,' + id + ' h5,' + id + ' h6';
  29. var toc = documentRef.getElementById('toc' + id);
  30. var headings = [].slice.call(documentRef.body.querySelectorAll(selector));
  31. if (headings && headings.length) {
  32. headings.forEach(function (heading, index) {
  33. heading.setAttribute('id', heading.textContent.replace(/\s+/g, '-').toLowerCase() + id);
  34. heading.setAttribute('class', 'toc-link');
  35. var anchor = documentRef.createElement('a');
  36. anchor.setAttribute('href', '#' + heading.textContent.replace(/\s+/g, '-').toLowerCase() + id);
  37. anchor.setAttribute('class', 'anchor');
  38. anchor.setAttribute('aria-label', heading.textContent);
  39. anchor.innerHTML = '<svg fill="currentColor" class="toc-icon" viewBox="0 0 24 24" height="18" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z" fill="none"></path><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76.0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71.0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71.0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76.0 5-2.24 5-5s-2.24-5-5-5z"></path></svg>';
  40. var link = documentRef.createElement('a');
  41. link.setAttribute('href', '#' + heading.textContent.replace(/\s+/g, '-').toLowerCase() + id);
  42. link.textContent = heading.textContent;
  43. var div = documentRef.createElement('div');
  44. div.setAttribute('class', heading.tagName.toLowerCase() + '-toc');
  45. heading.appendChild(anchor);
  46. div.appendChild(link);
  47. toc.appendChild(div);
  48. });
  49. documentRef.getElementById('toc-wrapper' + id).classList.add('visible');
  50. documentRef.getElementById('toc-wrapper' + id).style.display = 'inline-block';
  51. }
  52. if (window.location.hash) {
  53. var hash = window.location.hash;
  54. scrollToHash(hash);
  55. }
  56. }
  57. // fix browser not scrolling to the hash
  58. function scrollToHash (hash) {
  59. setTimeout(function() {
  60. hashtag = hash;
  61. location.hash = '';
  62. location.hash = hashtag;
  63. }, 300);
  64. }
  65. try {
  66. module.exports = generateTOC;
  67. } catch (e) {
  68. // module.exports is not defined
  69. }