Skip to content

Commit

Permalink
Make link to first chapter active in index page
Browse files Browse the repository at this point in the history
Makes both pages more consistent, and also the previous test pass
  • Loading branch information
ISSOtm committed Jun 22, 2022
1 parent 72bcca9 commit aa83d7e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl HtmlHandlebars {
if ctx.is_index {
ctx.data.insert("path".to_owned(), json!("index.md"));
ctx.data.insert("path_to_root".to_owned(), json!(""));
ctx.data.insert("is_index".to_owned(), json!("true"));
ctx.data.insert("is_index".to_owned(), json!(true));
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
let rendered_index =
self.post_process(rendered_index, &ctx.html_config.playground, ctx.edition);
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ impl HelperDef for RenderToc {
out.write("<ol class=\"chapter\">")?;

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

for item in chapters {
// Spacer
Expand Down Expand Up @@ -126,7 +131,8 @@ impl HelperDef for RenderToc {
out.write(&tmp)?;
out.write("\"")?;

if path == &current_path {
if path == &current_path || is_first_chapter {
is_first_chapter = false;
out.write(" class=\"active\"")?;
}

Expand Down

0 comments on commit aa83d7e

Please sign in to comment.