最近の検索
最近の検索はありません

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
コメント