about summary refs log tree commit diff
path: root/src/rustbook/javascript.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-01-30 12:02:48 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-01-30 12:02:48 -0800
commit9ff540ba3771a1c0a396ca3f7a685a94b4e0f622 (patch)
tree37e9f5baa41270232e785ddb11c125cb57be3484 /src/rustbook/javascript.rs
parent15dd0a5acba78e7d086f0b9fe610e66f3369ed63 (diff)
parente371d23486a5b42f41cd220ebef1689d49e3fe94 (diff)
downloadrust-9ff540ba3771a1c0a396ca3f7a685a94b4e0f622.tar.gz
rust-9ff540ba3771a1c0a396ca3f7a685a94b4e0f622.zip
rollup merge of #21494: jatinn/jsnav
Added javascript code to insert next/prev links in the rust book.
Related Issue - https://github.com/rust-lang/rust/issues/20835
Diffstat (limited to 'src/rustbook/javascript.rs')
-rw-r--r--src/rustbook/javascript.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/rustbook/javascript.rs b/src/rustbook/javascript.rs
index eb4401e1835..d34887d2b08 100644
--- a/src/rustbook/javascript.rs
+++ b/src/rustbook/javascript.rs
@@ -38,6 +38,37 @@ document.addEventListener("DOMContentLoaded", function(event) {
        el.className = classes.join(' ');
      }
   }
+
+  // The below code is used to add prev and next navigation links to the bottom
+  // of each of the sections.
+  // It works by extracting the current page based on the url and iterates over
+  // the menu links until it finds the menu item for the current page. We then
+  // create a copy of the preceeding and following menu links and add the
+  // correct css class and insert them into the bottom of the page.
+  var toc = document.getElementById('toc').getElementsByTagName('a');
+  var href = document.location.pathname.split('/').pop();
+  if (href === 'index.html' || href === '') {
+    href = 'README.html';
+  }
+
+  for (var i = 0; i < toc.length; i++) {
+    if (toc[i].attributes['href'].value === href) {
+      var nav = document.createElement('p');
+      if (i > 0) {
+        var prevNode = toc[i-1].cloneNode(true);
+        prevNode.className = 'left';
+        nav.appendChild(prevNode);
+      }
+      if (i < toc.length - 1) {
+        var nextNode = toc[i+1].cloneNode(true);
+        nextNode.className = 'right';
+        nav.appendChild(nextNode);
+      }
+      document.getElementById('page').appendChild(nav);
+      break;
+    }
+  }
+
 });
 </script>
 "#;