Recherches récentes
Pas de recherche récente

Christian Espinosa
Adhésion le 20 août 2024
·
Dernière activité le 20 août 2024
Suivis
0
Abonnés
0
Activité totale
2
Votes
0
Abonnement
1
APERÇU DES ACTIVITÉS
BADGES
ARTICLES
PUBLICATIONS
COMMENTAIRES DE LA COMMUNAUTÉ
COMMENTAIRES SUR L’ARTICLE
APERÇU DES ACTIVITÉS
Dernière activité effectuée par Christian Espinosa
Christian Espinosa a ajouté un commentaire,
Commentaire de la communauté Discussion - Tips and best practices from the community
I wanted to contribute to this and provide an up to date version of the original js code without the fragility of regex, onload and var.
document.addEventListener('DOMContentLoaded', function() {
const tocContainer = document.getElementById('toc');
const contentContainer = document.getElementById('contents');
function generateTableOfContents() {
const headings = contentContainer.querySelectorAll('h1, h2, h3, h4, h5, h6');
let currentLevel = 0;
let tableOfContents = '';
headings.forEach((heading) => {
const headingLevel = parseInt(heading.tagName.charAt(1));
const headingText = heading.textContent.trim();
const headingId = headingText.replace(/\s/g, '_');
heading.id = headingId;
if (headingLevel > currentLevel) {
tableOfContents += '';
} else if (headingLevel < currentLevel) {
tableOfContents += '
'.repeat(currentLevel - headingLevel);
}
tableOfContents += `${headingText} `;
currentLevel = headingLevel;
});
if (currentLevel > 0) {
tableOfContents += ''.repeat(currentLevel);
}
tocContainer.innerHTML = tableOfContents;
}
generateTableOfContents();
});
Best,
Christian
Afficher le commentaire · Publication le 20 août 2024 · Christian Espinosa
0
Abonnés
0
Votes
0
Commentaire