Skip to content

Commit 1e53218

Browse files
committed
Rollup merge of #25551 - cllns:add-active-class-to-rustbook-toc, r=alexcrichton
Currently the table of contents for `rustbook` doesn't signify which page you are on. This PR adds an 'active' class to the link for the current page, and defines the CSS rule for that class to make the link underlined and bold. Not sure about two things: 1) Is `current_page` is a good name for the function parameter? At first I thought `current_item` would be good, but then in the `walk_item` function, you'd have `item` and `current_item`. 2) For the CSS, is both bold and underline too much? At first I had it just be underlined, but that's also how the links look when they're hovered over.
2 parents 392576b + 99868f6 commit 1e53218

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/rustbook/build.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,34 @@ pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
3737
}
3838
}
3939

40-
fn write_toc(book: &Book, path_to_root: &Path, out: &mut Write) -> io::Result<()> {
40+
fn write_toc(book: &Book, current_page: &BookItem, out: &mut Write) -> io::Result<()> {
4141
fn walk_items(items: &[BookItem],
4242
section: &str,
43-
path_to_root: &Path,
43+
current_page: &BookItem,
4444
out: &mut Write) -> io::Result<()> {
4545
for (i, item) in items.iter().enumerate() {
46-
try!(walk_item(item, &format!("{}{}.", section, i + 1)[..], path_to_root, out));
46+
try!(walk_item(item, &format!("{}{}.", section, i + 1)[..], current_page, out));
4747
}
4848
Ok(())
4949
}
5050
fn walk_item(item: &BookItem,
5151
section: &str,
52-
path_to_root: &Path,
52+
current_page: &BookItem,
5353
out: &mut Write) -> io::Result<()> {
54-
try!(writeln!(out, "<li><a href='{}'><b>{}</b> {}</a>",
55-
path_to_root.join(&item.path.with_extension("html")).display(),
54+
let class_string = if item.path == current_page.path {
55+
"class='active'"
56+
} else {
57+
""
58+
};
59+
60+
try!(writeln!(out, "<li><a {} href='{}'><b>{}</b> {}</a>",
61+
class_string,
62+
item.path_to_root.join(&item.path.with_extension("html")).display(),
5663
section,
5764
item.title));
5865
if !item.children.is_empty() {
5966
try!(writeln!(out, "<ul class='section'>"));
60-
let _ = walk_items(&item.children[..], section, path_to_root, out);
67+
let _ = walk_items(&item.children[..], section, current_page, out);
6168
try!(writeln!(out, "</ul>"));
6269
}
6370
try!(writeln!(out, "</li>"));
@@ -67,7 +74,7 @@ fn write_toc(book: &Book, path_to_root: &Path, out: &mut Write) -> io::Result<()
6774

6875
try!(writeln!(out, "<div id='toc' class='mobile-hidden'>"));
6976
try!(writeln!(out, "<ul class='chapter'>"));
70-
try!(walk_items(&book.chapters[..], "", path_to_root, out));
77+
try!(walk_items(&book.chapters[..], "", &current_page, out));
7178
try!(writeln!(out, "</ul>"));
7279
try!(writeln!(out, "</div>"));
7380

@@ -115,7 +122,7 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
115122
<span class="bar"></span>
116123
</button>
117124
</div>"#));
118-
let _ = write_toc(book, &item.path_to_root, &mut toc);
125+
let _ = write_toc(book, &item, &mut toc);
119126
try!(writeln!(&mut toc, "<div id='page-wrapper'>"));
120127
try!(writeln!(&mut toc, "<div id='page'>"));
121128
}

src/rustbook/css.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ body {
9797
color: #000000;
9898
}
9999
100+
.chapter li a.active {
101+
text-decoration: underline;
102+
font-weight: bold;
103+
}
104+
100105
#toggle-nav {
101106
height: 20px;
102107
width: 30px;

0 commit comments

Comments
 (0)