Búsquedas recientes
No hay búsquedas recientes

Christian Espinosa
Incorporación 20 ago 2024
·
Última actividad 20 ago 2024
Seguimientos
0
Seguidores
0
Actividad total
2
Votos
0
Suscripción
1
RESUMEN DE LA ACTIVIDAD
INSIGNIAS
ARTÍCULOS
PUBLICACIONES
COMENTARIOS DE LA COMUNIDAD
COMENTARIOS DE ARTÍCULOS
RESUMEN DE LA ACTIVIDAD
Última actividad de Christian Espinosa
Christian Espinosa hizo un comentario,
Comentario de la comunidad 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
Ver comentario · Publicado 20 ago 2024 · Christian Espinosa
0
Seguidores
0
Votos
0
Comentarios