You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Create a table of contents (TOC) based on the title elements within a specified content. * * @param {Object} config - Configuration object for the function. * @param {Element} config.content - The root element where the TOC will be generated from. * @param {string} config.tocElement - A CSS selector that specifies the element where the TOC will be appended. * @param {Array} config.titleElements - An array of CSS selectors indicating title elements. */functioncreateToc(config){// Destructure the configuration object for easier accessconst{ content, tocElement, titleElements }=config;// Locate the container where the TOC will be appendedlettocElementDiv=content.querySelector(tocElement);// Create a new unordered list to house the TOClettocUl=document.createElement("ul");tocUl.id="list-toc-generated";// Give it an ID for potential styling or scriptingtocElementDiv.appendChild(tocUl);// Append the list to the container// Counter to generate unique IDs for title elements (if needed)lettocElementNbr=0;// Loop over each title element selectorfor(vari=0;i<titleElements.length;i++){// Calculate the title hierarchy based on its position in the titleElements arraylettitleHierarchy=i+1;// Select all elements in the content that match the current title selectorlettitleElement=content.querySelectorAll(titleElements[i]);titleElement.forEach(function(element){// Add a generic class and data attribute to indicate its hierarchyelement.classList.add("title-element");element.setAttribute("data-title-level",titleHierarchy);// If the element doesn't have an ID, create onetocElementNbr++;idElement=element.id;if(idElement==""){element.id="title-element-"+tocElementNbr;}});}// Select all title elements we previously modifiedlettocElements=content.querySelectorAll(".title-element");// Loop over each title element to create corresponding TOC list itemsfor(vari=0;i<tocElements.length;i++){lettocElement=tocElements[i];// Create a new list item for the TOClettocNewLi=document.createElement("li");// Add generic and hierarchy-specific classestocNewLi.classList.add("toc-element");tocNewLi.classList.add("toc-element-level-"+tocElement.dataset.titleLevel,);// Copy over other classes from the original title element (excluding the generic title-element class)letclassTocElement=tocElement.classList;for(varn=0;n<classTocElement.length;n++){if(classTocElement[n]!="title-element"){tocNewLi.classList.add(classTocElement[n]);}}// Set the inner content of the list item to be a link to the original title elementtocNewLi.innerHTML='<a href="#'+tocElement.id+'">'+tocElement.innerHTML+"</a>";// Append the new list item to the TOC listtocUl.appendChild(tocNewLi);}}
Everything went well if I generate a table of contents worth 1 page:
But if there is more than 1 page, I got two blank pages then the full TOC:
As you can see, the 3.15 is missing. It is on page 64. You can see that there are number 64 in page 4, which means it renders the page number but not the link.
Any ideas what went wrong?
The text was updated successfully, but these errors were encountered:
Introduction
Hi, I hope you are doing well. I tried to use your code using MacOS Sonoma
I have this HTML:
HTML
The script for generating TOC:
Script
The CSS:
CSS
The Problem
Everything went well if I generate a table of contents worth 1 page:
But if there is more than 1 page, I got two blank pages then the full TOC:
As you can see, the 3.15 is missing. It is on page 64. You can see that there are number 64 in page 4, which means it renders the page number but not the link.
Any ideas what went wrong?
The text was updated successfully, but these errors were encountered: