diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-06-12 20:14:10 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-06-12 20:14:10 +0800 |
| commit | e94787a4f2633aee6578d54ad1bcbd97384634e4 (patch) | |
| tree | c2a0eb95a3b9a9195d30abb06e14ec7e77f2f787 /docs/scripts/docsify-plugin-prevnext.js | |
| parent | c51c592a9a977dfdcaa754214a4e1c2e7964d6e8 (diff) | |
Add prev/next page navigation plugin and styles
Diffstat (limited to 'docs/scripts/docsify-plugin-prevnext.js')
| -rw-r--r-- | docs/scripts/docsify-plugin-prevnext.js | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/docs/scripts/docsify-plugin-prevnext.js b/docs/scripts/docsify-plugin-prevnext.js new file mode 100644 index 0000000..c80f17c --- /dev/null +++ b/docs/scripts/docsify-plugin-prevnext.js @@ -0,0 +1,95 @@ +/*! + * docsify-plugin-prevnext + * Auto-generates previous/next page navigation based on sidebar order. + */ +(function () { + function install(hook, vm) { + hook.doneEach(function () { + var nav = document.querySelector(".page-nav"); + if (nav) nav.remove(); + + function navigate(path) { + window.location.hash = "#/" + (path || ""); + } + + var sidebarLinks = document.querySelectorAll(".sidebar-nav li a"); + if (!sidebarLinks.length) return; + + var currentPath = vm.route.path; + var links = []; + sidebarLinks.forEach(function (a) { + var href = a.getAttribute("href"); + if (!href) return; + if (href.startsWith("http")) return; + if (href.indexOf("?id=") !== -1) return; // skip sub-headings + var path = href.startsWith("#") ? href.slice(1) : href; + if (path.startsWith("/")) path = path.slice(1); + if (path === "" || path === "/") path = "README"; + links.push({ path: path, text: a.textContent.trim() }); + }); + + function normalize(p) { + return p.replace(/\.md$/g, "").replace(/^\/+|\/+$/g, ""); + } + + // Treat root path as README + var cur = normalize(currentPath) || "README"; + var idx = -1; + for (var i = 0; i < links.length; i++) { + if (normalize(links[i].path) === cur) { + idx = i; + break; + } + } + + if (idx === -1) return; + + var prevLink = idx > 0 ? links[idx - 1] : null; + var nextLink = idx < links.length - 1 ? links[idx + 1] : null; + + // Build nav HTML + var html = ""; + if (prevLink) { + html += + '<a class="nav-prev" href="javascript:;" data-nav="' + + encodeURIComponent(prevLink.path) + + '">' + + '<span><div class="nav-label">‹ Previous</div><div class="nav-title">' + + prevLink.text + + "</div></span></a>"; + } + if (nextLink) { + html += + '<a class="nav-next" href="javascript:;" data-nav="' + + encodeURIComponent(nextLink.path) + + '">' + + '<span><div class="nav-label">Next ›</div><div class="nav-title">' + + nextLink.text + + "</div></span></a>"; + } + + if (!html) return; + + nav = document.createElement("div"); + nav.className = "page-nav"; + nav.innerHTML = html; + + // Attach click handlers via event delegation + nav.addEventListener("click", function (e) { + var target = e.target.closest("a"); + if (!target) return; + var path = target.getAttribute("data-nav"); + if (path) { + e.preventDefault(); + navigate(decodeURIComponent(path)); + } + }); + + var section = document.querySelector(".markdown-section"); + if (section) section.appendChild(nav); + }); + } + + window.$docsify = window.$docsify || {}; + window.$docsify.plugins = (window.$docsify.plugins || []).concat(install); +})(); |
