Skip to content

Commit e371d23

Browse files
committed
add next/prev section links in the book -- using js
1 parent 6869645 commit e371d23

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/rustbook/css.rs

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ body {
4646
margin-left: auto;
4747
margin-right:auto;
4848
max-width: 750px;
49+
padding-bottom: 50px;
4950
}
5051
5152
.chapter {
@@ -123,4 +124,12 @@ body {
123124
padding: 0;
124125
}
125126
127+
.left {
128+
float: left;
129+
}
130+
131+
.right {
132+
float: right;
133+
}
134+
126135
"#;

src/rustbook/javascript.rs

+31
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)