Skip to content

Commit cf80e95

Browse files
committed
Make link to first chapter active in index page
Makes both pages more consistent, and also the previous test pass
1 parent a91e888 commit cf80e95

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl HtmlHandlebars {
116116
if ctx.is_index {
117117
ctx.data.insert("path".to_owned(), json!("index.md"));
118118
ctx.data.insert("path_to_root".to_owned(), json!(""));
119-
ctx.data.insert("is_index".to_owned(), json!("true"));
119+
ctx.data.insert("is_index".to_owned(), json!(true));
120120
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
121121
let rendered_index =
122122
self.post_process(rendered_index, &ctx.html_config.playground, ctx.edition);

src/renderer/html_handlebars/helpers/toc.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ impl HelperDef for RenderToc {
5757
out.write("<ol class=\"chapter\">")?;
5858

5959
let mut current_level = 1;
60+
// The "index" page, which has this attribute set, is supposed to alias the first chapter in
61+
// the book, i.e. the first link. There seems to be no easy way to determine which chapter
62+
// the "index" is aliasing from within the renderer, so this is used instead to force the
63+
// first link to be active. See further below.
64+
let mut is_first_chapter = ctx.data().get("is_index").is_some();
6065

6166
for item in chapters {
6267
// Spacer
@@ -130,7 +135,8 @@ impl HelperDef for RenderToc {
130135
out.write(&tmp)?;
131136
out.write("\"")?;
132137

133-
if path == &current_path {
138+
if path == &current_path || is_first_chapter {
139+
is_first_chapter = false;
134140
out.write(" class=\"active\"")?;
135141
}
136142

0 commit comments

Comments
 (0)