최근 검색
최근 검색 없음

Christian Espinosa
가입한 날짜: 2024년 8월 20일
·
마지막 활동: 2024년 8월 20일
팔로잉
0
팔로워
0
총 활동 수
2
투표 수
0
가입 플랜
1
활동 개요
배지
문서
게시물
커뮤니티 댓글
문서 댓글
활동 개요
님의 최근 활동 Christian Espinosa
Christian Espinosa님이 에 댓글을 입력함
커뮤니티 댓글 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
댓글 보기 · 2024년 8월 20일에 게시됨 · Christian Espinosa
0
팔로워
0
투표 수
0
댓글