From 324e8301a875b2535b684a111bcd75a0e1d4b766 Mon Sep 17 00:00:00 2001 From: MeiK Date: Thu, 27 Jan 2022 15:31:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20toc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- layouts/partials/post.html | 81 ++++++-------------------------------- static/css/toc.css | 4 ++ static/js/toc.js | 43 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 69 deletions(-) create mode 100644 static/css/toc.css diff --git a/layouts/partials/post.html b/layouts/partials/post.html index e8c522f..8279de0 100644 --- a/layouts/partials/post.html +++ b/layouts/partials/post.html @@ -1,5 +1,3 @@ - -
@@ -44,16 +42,22 @@
-
+
- + - {{ countwords .Content }} Words + + {{ countwords .Content }} Words @@ -76,7 +80,7 @@
-
+
{{- .Content -}}
@@ -88,66 +92,5 @@
- + + \ No newline at end of file diff --git a/static/css/toc.css b/static/css/toc.css new file mode 100644 index 0000000..780117d --- /dev/null +++ b/static/css/toc.css @@ -0,0 +1,4 @@ +.toc-item { + font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"; + line-height: 1.5; +} \ No newline at end of file diff --git a/static/js/toc.js b/static/js/toc.js index e69de29..7ffb525 100644 --- a/static/js/toc.js +++ b/static/js/toc.js @@ -0,0 +1,43 @@ +const details = document.querySelector("#toc-details"); + +function clickToc() { + switch (details.style.display) { + case 'none': + details.style.display = 'block'; + setTimeout(() => + document.body.addEventListener('click', closeToc), 1); + break; + case 'block': + details.style.display = 'none'; + break; + } +} + +function openToc() { + details.style.display = 'block'; +} + +function closeToc() { + details.style.display = 'none'; + document.body.removeEventListener('click', closeToc); +} + +function getToc() { + const hs = document.querySelector('.markdown-body').querySelectorAll('h1, h2, h3, h4, h5, h6'); + const toc_list = document.querySelector("#toc-list"); + for (const h of hs) { + const size = Number(h.tagName.toLowerCase().replace('h', '')); + const a = document.createElement('a'); + a.classList.add("filter-item", "SelectMenu-item", "ws-normal", "wb-break-word", "line-clamp-2", "py-1", "toc-item"); + a.href = `#${h.id}`; + a.innerText = h.innerHTML; + a.style.paddingLeft = `${size * 12}px`; + toc_list.appendChild(a); + } +} + + +// TODO: highlight on scroll +(() => { + getToc(); +})();