aboutsummaryrefslogtreecommitdiff
path: root/docs/scripts/docsify-plugin-prevnext.js
blob: c80f17c286f4fb42720107231af2222c7a0236da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
})();