Skip to content

[Rustbook] add next/prev section links in the book -- using js #21494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/rustbook/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ body {
margin-left: auto;
margin-right:auto;
max-width: 750px;
padding-bottom: 50px;
}

.chapter {
Expand Down Expand Up @@ -123,4 +124,12 @@ body {
padding: 0;
}

.left {
float: left;
}

.right {
float: right;
}

"#;
31 changes: 31 additions & 0 deletions src/rustbook/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
"#;