From e94787a4f2633aee6578d54ad1bcbd97384634e4 Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Fri, 12 Jun 2026 20:14:10 +0800 Subject: Add prev/next page navigation plugin and styles --- docs/scripts/docsify-plugin-prevnext.js | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 docs/scripts/docsify-plugin-prevnext.js (limited to 'docs/scripts') 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 += + '' + + '"; + } + if (nextLink) { + html += + '' + + '"; + } + + 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); +})(); -- cgit