.article-toc.card { background: #ffffff; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 10px; transition: box-shadow 0.3s ease, transform 0.3s ease; margin: 1.5rem 0; width: 100%; max-width: 100%; height: fit-content; border: 1px solid #e5e7eb; } .article-toc h2 { margin: 0; font-size: 20px; background: none; border: none; font-weight: bold; cursor: pointer; display: flex; align-items: center; width: 100%; justify-content: space-between; padding: 5px 0 5px 20px; position: relative; user-select: none; word-spacing: 2px !important; letter-spacing: normal !important; } .toc-toggle { font-size: 20px; background: none; border: none; font-weight: bold; cursor: pointer; display: flex; align-items: center; justify-content: center; padding: 0.5rem; position: absolute; right: 10px; top: 50%; transform: translateY(-50%); color: inherit !important; outline: none !important; box-shadow: none !important; } .toc-toggle::after { content: "+"; } .toc-toggle.expanded::after { content: "−"; } .article-toc ul { list-style: none; padding-left: 15px; margin: 0; overflow: hidden; height: 0; transition: max-height 0.3s ease; } .article-toc ul.expanded { height: auto; max-height: 2000px; } .article-toc li { margin-bottom: 5px; padding: 5px; } .article-toc .toc-h2 { font-weight: 600; padding-left: 1rem; text-decoration: none; display: block; } .article-toc .toc-h3 { font-weight: 500; padding-left: 2.5rem !important; text-decoration: none; display: block; } .article-toc .toc-h2:hover, .article-toc .toc-h3:hover { transform: translateY(-2px); transition: .3s all; } .article-toc a { color: inherit; text-decoration: none; transition: color 0.2s; border-bottom: none !important; padding: 5px 10px !important; display: inline-block; width: 100%; } .article-toc a.active { color: #009ca6 !important; font-weight: bold; } @media (min-width: 1024px) { .article-toc .toc-h2:hover, .article-toc .toc-h3:hover { transform: translateY(-2px); transition: .3s all; } .article-toc a:hover { color: #009ca6 !important; } } Table of Contents document.addEventListener("DOMContentLoaded", function () { const articleContent = document.querySelector(".article-template__content"); const tocList = document.getElementById("toc-list"); const tocContainer = document.querySelector(".article-toc"); const tocHeader = document.querySelector(".article-toc h2"); const tocToggleButton = document.querySelector(".toc-toggle"); if (!articleContent || !tocList || !tocContainer) { if(tocContainer) tocContainer.style.display = 'none'; return; } if (tocHeader) { tocHeader.addEventListener('click', function() { const expanded = tocList.classList.contains('expanded'); if (expanded) { tocList.style.maxHeight = '0px'; tocList.classList.remove('expanded'); if(tocToggleButton) tocToggleButton.classList.remove('expanded'); } else { tocList.style.maxHeight = tocList.scrollHeight + 'px'; tocList.classList.add('expanded'); if(tocToggleButton) tocToggleButton.classList.add('expanded'); } }); tocList.addEventListener('transitionend', () => { if (tocList.classList.contains('expanded')) { tocList.style.maxHeight = 'none'; } }); } const headings = articleContent.querySelectorAll("h2:not(.toc-header), h3"); const hasH2 = Array.from(headings).some(heading => heading.tagName === 'H2' && heading.textContent.trim().length > 0 ); if (!hasH2) { tocContainer.style.display = 'none'; return; } const validHeadings = Array.from(headings).filter(heading => { return heading.textContent.trim().length > 0; }); if (validHeadings.length === 0) { tocContainer.style.display = 'none'; return; } validHeadings.forEach((heading, index) => { const text = heading.textContent.trim(); const tagName = heading.tagName.toLowerCase(); let id = heading.id; if (!id) { id = "toc-" + index; heading.setAttribute("id", id); } const li = document.createElement("li"); const a = document.createElement("a"); a.href = "#" + id; a.className = tagName === "h2" ? "toc-h2" : "toc-h3"; a.textContent = text; li.appendChild(a); tocList.appendChild(li); a.addEventListener('click', function (e) { e.preventDefault(); document.querySelectorAll('.article-toc a').forEach(link => link.classList.remove('active')); this.classList.add('active'); const targetID = this.getAttribute('href').substring(1); const targetEl = document.getElementById(targetID); if (targetEl) { const headerOffset = 20; const elementPosition = targetEl.getBoundingClientRect().top; const offsetPosition = elementPosition + window.scrollY - headerOffset; window.scrollTo({ top: offsetPosition, behavior: "smooth" }); } }); });});