Skip to content

Commit 9ff540b

Browse files
committed
rollup merge of #21494: jatinn/jsnav
Added javascript code to insert next/prev links in the rust book. Related Issue - #20835
2 parents 15dd0a5 + e371d23 commit 9ff540b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/rustbook/css.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ body {
4848
margin-left: auto;
4949
margin-right:auto;
5050
max-width: 750px;
51+
padding-bottom: 50px;
5152
}
5253
5354
.chapter {
@@ -125,4 +126,12 @@ body {
125126
padding: 0;
126127
}
127128
129+
.left {
130+
float: left;
131+
}
132+
133+
.right {
134+
float: right;
135+
}
136+
128137
"#;

src/rustbook/javascript.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,37 @@ document.addEventListener("DOMContentLoaded", function(event) {
3838
el.className = classes.join(' ');
3939
}
4040
}
41+
42+
// The below code is used to add prev and next navigation links to the bottom
43+
// of each of the sections.
44+
// It works by extracting the current page based on the url and iterates over
45+
// the menu links until it finds the menu item for the current page. We then
46+
// create a copy of the preceeding and following menu links and add the
47+
// correct css class and insert them into the bottom of the page.
48+
var toc = document.getElementById('toc').getElementsByTagName('a');
49+
var href = document.location.pathname.split('/').pop();
50+
if (href === 'index.html' || href === '') {
51+
href = 'README.html';
52+
}
53+
54+
for (var i = 0; i < toc.length; i++) {
55+
if (toc[i].attributes['href'].value === href) {
56+
var nav = document.createElement('p');
57+
if (i > 0) {
58+
var prevNode = toc[i-1].cloneNode(true);
59+
prevNode.className = 'left';
60+
nav.appendChild(prevNode);
61+
}
62+
if (i < toc.length - 1) {
63+
var nextNode = toc[i+1].cloneNode(true);
64+
nextNode.className = 'right';
65+
nav.appendChild(nextNode);
66+
}
67+
document.getElementById('page').appendChild(nav);
68+
break;
69+
}
70+
}
71+
4172
});
4273
</script>
4374
"#;

0 commit comments

Comments
 (0)