From 70f8a6f1fb9ba49d7891f5f603cd350344366859 Mon Sep 17 00:00:00 2001 From: yeheshuah Date: Wed, 30 Apr 2025 12:47:24 +0900 Subject: [PATCH] Collapse/Expand section with paragraphs --- docs/_assets/add_paragraphs.js | 34 +++++++++++++++++++++++++++++++ docs/_assets/scroll_to_summary.js | 32 +++++++++++++++++++++++++++++ docs/_assets/user.css | 10 +++++++++ mkdocs.yml | 4 ++++ 4 files changed, 80 insertions(+) create mode 100644 docs/_assets/add_paragraphs.js create mode 100644 docs/_assets/scroll_to_summary.js diff --git a/docs/_assets/add_paragraphs.js b/docs/_assets/add_paragraphs.js new file mode 100644 index 00000000..b31b634d --- /dev/null +++ b/docs/_assets/add_paragraphs.js @@ -0,0 +1,34 @@ +document.addEventListener('DOMContentLoaded', function () { + document.querySelectorAll('details').forEach(details => { + const summary = details.querySelector('summary'); + + if (!summary) return; + + let linkEl; + + details.addEventListener('mouseenter', () => { + if (details.open) return; + + const summaryText = summary.textContent.trim(); + console.log(`${summaryText}`); + const hash = '#' + encodeURIComponent(summaryText); + + if (linkEl) return; + + linkEl = document.createElement('a'); + linkEl.href = hash; + linkEl.textContent = 'ΒΆ'; + linkEl.title = 'Permanent link'; + linkEl.className = 'hoverlink'; + + summary.appendChild(linkEl); + }); + + details.addEventListener('mouseleave', () => { + if (linkEl) { + linkEl.remove(); + linkEl = null; + } + }); + }); +}); \ No newline at end of file diff --git a/docs/_assets/scroll_to_summary.js b/docs/_assets/scroll_to_summary.js new file mode 100644 index 00000000..f71cc3c8 --- /dev/null +++ b/docs/_assets/scroll_to_summary.js @@ -0,0 +1,32 @@ +function scroll_to_summary() { + const hash = decodeURIComponent(window.location.hash.slice(1)).trim(); + if (!hash) return; + + const summaries = document.querySelectorAll('details > summary'); + + for (const summary of summaries) { + const text = summary.textContent.trim(); + + if (text.toLowerCase().includes(hash.toLowerCase())) { + const details = summary.closest('details'); + if (details) { + details.open = true; + + const yOffset = -60 + const y = details.getBoundingClientRect().top + window.pageYOffset + yOffset; + + window.scrollTo({ top: y, behavior: 'smooth' }); + + break; + } + } + } +} + +document.addEventListener('DOMContentLoaded', function () { + scroll_to_summary(); + + window.addEventListener("hashchange", () => { + scroll_to_summary(); + }); +}); \ No newline at end of file diff --git a/docs/_assets/user.css b/docs/_assets/user.css index e173373a..9d1e1c4b 100644 --- a/docs/_assets/user.css +++ b/docs/_assets/user.css @@ -183,3 +183,13 @@ ol li::marker { .md-button--stretch { width: 100%; } + +.hoverlink { + font-size: 1.0em; + margin-left: 8px; + text-decoration: none; +} + +.hoverlink:hover { + text-decoration: none; +} \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index b162f796..a9021c21 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -40,6 +40,10 @@ theme: extra_css: - _assets/user.css +extra_javascript: + - _assets/add_paragraphs.js + - _assets/scroll_to_summary.js + markdown_extensions: - admonition - def_list