|
| 1 | +/** |
| 2 | + * Originally it's |
| 3 | + * |
| 4 | + * reveal.js table of contents plugin |
| 5 | + * |
| 6 | + * A plugin which generates automatically a table of contents slide. |
| 7 | + * |
| 8 | + * Demo https://naamor.github.io/reveal.js-tableofcontents/ |
| 9 | + * |
| 10 | + * MIT License |
| 11 | + * Copyright (c) 2018 Roman Stocker |
| 12 | + * |
| 13 | + * Modified in 2023 by @vvscod as it doesn't support markdown |
| 14 | + * https://github.com/naamor/reveal.js-tableofcontents/issues/2 |
| 15 | + */ |
| 16 | + |
| 17 | +var RevealTableOfContents = |
| 18 | + window.RevealTableOfContents || |
| 19 | + (() => { |
| 20 | + // Set all option defaults |
| 21 | + const options = Reveal.getConfig().tableofcontents || {}; |
| 22 | + const titleTag = options.titleTag || "h1"; |
| 23 | + let titleTagSelector = ["h1", "h2", "h3", "h4", "h5", "h6"]; |
| 24 | + let title = options.title || "Table of Contents"; |
| 25 | + const position = options.position || 2; |
| 26 | + const fadeInElements = options.fadeInElements || false; |
| 27 | + |
| 28 | + let ignoreFirstSlide = options.ignoreFirstSlide; |
| 29 | + if (typeof ignoreFirstSlide === "undefined") ignoreFirstSlide = true; |
| 30 | + |
| 31 | + initialize(); |
| 32 | + |
| 33 | + function initialize() { |
| 34 | + if (typeof options.titleTagSelector === "string") { |
| 35 | + titleTagSelector = options.titleTagSelector |
| 36 | + .split(",") |
| 37 | + .map((item) => item.trim()); |
| 38 | + } |
| 39 | + |
| 40 | + generateTableOfContentsSlide(); |
| 41 | + } |
| 42 | + |
| 43 | + function generateTableOfContentsSlide() { |
| 44 | + const slides = document.getElementsByClassName("slides")[0]; |
| 45 | + |
| 46 | + const section = document.createElement("section"); |
| 47 | + section.className = "table-of-contents"; |
| 48 | + |
| 49 | + const heading = document.createElement(titleTag); |
| 50 | + heading.innerText = title; |
| 51 | + section.appendChild(heading); |
| 52 | + |
| 53 | + const list = generateList(); |
| 54 | + section.appendChild(list); |
| 55 | + |
| 56 | + // Subtract by one because index starts with zero |
| 57 | + const slideAfter = slides.children[position - 1]; |
| 58 | + |
| 59 | + // Check if there are enough slides for the configured table of contents slide position |
| 60 | + // or set the table of contents slide automatically after the last slide |
| 61 | + if (slideAfter !== undefined) { |
| 62 | + slides.insertBefore(section, slideAfter); |
| 63 | + } else { |
| 64 | + slides.appendChild(section); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Generate list with the title of each slide |
| 69 | + function generateList() { |
| 70 | + const slides = Reveal.getSlides(); |
| 71 | + |
| 72 | + const ul = document.createElement("ul"); |
| 73 | + |
| 74 | + let counter = 0; |
| 75 | + |
| 76 | + // Ignore first slide with counter 0 |
| 77 | + if (ignoreFirstSlide) { |
| 78 | + counter++; |
| 79 | + } |
| 80 | + |
| 81 | + for (counter; counter < slides.length; counter++) { |
| 82 | + const title = getTitle(slides[counter]); |
| 83 | + |
| 84 | + if (title !== undefined) { |
| 85 | + const li = document.createElement("li"); |
| 86 | + |
| 87 | + // Add attributes for use reveal.js fragment functionality |
| 88 | + if (fadeInElements) { |
| 89 | + li.className = "fragment"; |
| 90 | + li.setAttribute("data-fragment-index", counter); |
| 91 | + } |
| 92 | + |
| 93 | + li.innerHTML = title; |
| 94 | + |
| 95 | + ul.appendChild(li); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return ul; |
| 100 | + } |
| 101 | + |
| 102 | + // Select the text of the most important heading tag of every slide |
| 103 | + function getTitle({ childNodes }) { |
| 104 | + return (title = Array.from(childNodes) |
| 105 | + .filter(getSlideTitle) |
| 106 | + .map(getSlideTitle)[0]); |
| 107 | + } |
| 108 | + |
| 109 | + // Filter tags based on options |
| 110 | + function getSlideTitle(element) { |
| 111 | + const { tagName, textContent } = element; |
| 112 | + if (tagName === undefined) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + const originalTitle = |
| 117 | + titleTagSelector.includes(tagName.toLowerCase()) && textContent; |
| 118 | + |
| 119 | + if (originalTitle) { |
| 120 | + return originalTitle; |
| 121 | + } |
| 122 | + if (element.matches('script[type="text/template"]')) { |
| 123 | + const markdownLinksRegexp = /\[(.*?)\]\((.*?)\)/g; |
| 124 | + return ( |
| 125 | + element.innerHTML |
| 126 | + .trim() |
| 127 | + .split("\n") |
| 128 | + .filter((line) => line.trim().startsWith("#")) |
| 129 | + .map((line) => line.replaceAll("#", "").trim()) |
| 130 | + .map((line) => line.replace(markdownLinksRegexp, "")) |
| 131 | + .join(". ") || undefined |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | + })(); |
0 commit comments