From 15d5ea1e7d4d2df40df7b0cb3f7da1afb62fbe36 Mon Sep 17 00:00:00 2001 From: Meekdai Date: Mon, 29 Jul 2024 10:15:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=96=87=E7=AB=A0=E4=B8=AD?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E6=A0=87=E9=A2=98=E5=B1=9E=E6=80=A7=EF=BC=8C?= =?UTF-8?q?=E5=88=99=E4=B8=8D=E5=88=9B=E5=BB=BATOC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/GmeekTOC.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/plugins/GmeekTOC.js b/plugins/GmeekTOC.js index eb8a9d2..c52edff 100644 --- a/plugins/GmeekTOC.js +++ b/plugins/GmeekTOC.js @@ -2,9 +2,15 @@ function createTOC() { var tocElement = document.createElement('div'); tocElement.className = 'toc'; var contentContainer = document.getElementById('content'); - contentContainer.prepend(tocElement); + const headings = contentContainer.querySelectorAll('h1, h2, h3, h4, h5, h6'); + + if (headings.length === 0) { + return; // 如果没有标题元素,则不创建TOC + } + tocElement.insertAdjacentHTML('afterbegin', '
文章目录
'); + headings.forEach(heading => { if (!heading.id) { heading.id = heading.textContent.trim().replace(/\s+/g, '-').toLowerCase(); @@ -16,10 +22,11 @@ function createTOC() { link.style.paddingLeft = `${(parseInt(heading.tagName.charAt(1)) - 1) * 10}px`; tocElement.appendChild(link); }); + tocElement.insertAdjacentHTML('beforeend', 'Top'); + contentContainer.prepend(tocElement); } - document.addEventListener("DOMContentLoaded", function() { createTOC(); var css = ` @@ -89,4 +96,4 @@ document.addEventListener("DOMContentLoaded", function() { }; console.log("\n %c GmeekTOC Plugins https://github.com/Meekdai/Gmeek \n","padding:5px 0;background:#C333D0;color:#fff"); -}); \ No newline at end of file +});