diff --git a/crates/mdbook-html/front-end/css/chrome.css b/crates/mdbook-html/front-end/css/chrome.css
index ce7fe04820..fcd9f76c85 100644
--- a/crates/mdbook-html/front-end/css/chrome.css
+++ b/crates/mdbook-html/front-end/css/chrome.css
@@ -186,10 +186,6 @@ html:not(.js) .left-buttons button {
left: var(--page-padding);
}
-/* Use the correct buttons for RTL layouts*/
-[dir=rtl] .previous i.fa-angle-left:before {content:"\f105";}
-[dir=rtl] .next i.fa-angle-right:before { content:"\f104"; }
-
@media only screen and (max-width: 1080px) {
.nav-wide-wrapper { display: none; }
.nav-wrapper { display: block; }
diff --git a/crates/mdbook-html/front-end/templates/index.hbs b/crates/mdbook-html/front-end/templates/index.hbs
index 5482448913..7a55e709cf 100644
--- a/crates/mdbook-html/front-end/templates/index.hbs
+++ b/crates/mdbook-html/front-end/templates/index.hbs
@@ -221,17 +221,25 @@
@@ -239,17 +247,25 @@
diff --git a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs
index b2c0ecaf0b..1c7f01001b 100644
--- a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs
+++ b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs
@@ -4,7 +4,7 @@ use crate::theme::Theme;
use anyhow::{Context, Result, bail};
use handlebars::Handlebars;
use log::{debug, info, trace, warn};
-use mdbook_core::book::{Book, BookItem};
+use mdbook_core::book::{Book, BookItem, Chapter};
use mdbook_core::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition};
use mdbook_core::utils;
use mdbook_core::utils::fs::get_404_output_file;
@@ -30,18 +30,17 @@ impl HtmlHandlebars {
HtmlHandlebars
}
- fn render_item(
+ fn render_chapter(
&self,
- item: &BookItem,
- mut ctx: RenderItemContext<'_>,
+ ch: &Chapter,
+ prev_ch: Option<&Chapter>,
+ next_ch: Option<&Chapter>,
+ mut ctx: RenderChapterContext<'_>,
print_content: &mut String,
) -> Result<()> {
// FIXME: This should be made DRY-er and rely less on mutable state
- let (ch, path) = match item {
- BookItem::Chapter(ch) if !ch.is_draft_chapter() => (ch, ch.path.as_ref().unwrap()),
- _ => return Ok(()),
- };
+ let path = ch.path.as_ref().unwrap();
if let Some(ref edit_url_template) = ctx.html_config.edit_url_template {
let full_path = ctx.book_config.src.to_str().unwrap_or_default().to_owned()
@@ -61,7 +60,7 @@ impl HtmlHandlebars {
let fixed_content =
render_markdown_with_path(&ch.content, ctx.html_config.smart_punctuation, Some(path));
- if !ctx.is_index && ctx.html_config.print.page_break {
+ if prev_ch.is_some() && ctx.html_config.print.page_break {
// Add page break between chapters
// See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before
// Add both two CSS properties because of the compatibility issue
@@ -116,6 +115,25 @@ impl HtmlHandlebars {
);
}
+ let mut nav = |name: &str, ch: Option<&Chapter>| {
+ let Some(ch) = ch else { return };
+ let path = ch
+ .path
+ .as_ref()
+ .unwrap()
+ .with_extension("html")
+ .to_str()
+ .unwrap()
+ .replace('\\', "//");
+ let obj = json!( {
+ "title": ch.name,
+ "link": path,
+ });
+ ctx.data.insert(name.to_string(), obj);
+ };
+ nav("previous", prev_ch);
+ nav("next", next_ch);
+
// Render the handlebars template with the data
debug!("Render template");
let rendered = ctx.handlebars.render("index", &ctx.data)?;
@@ -131,7 +149,7 @@ impl HtmlHandlebars {
debug!("Creating {}", filepath.display());
utils::fs::write_file(&ctx.destination, &filepath, rendered.as_bytes())?;
- if ctx.is_index {
+ if prev_ch.is_none() {
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));
@@ -253,8 +271,6 @@ impl HtmlHandlebars {
no_section_label: html_config.no_section_label,
}),
);
- handlebars.register_helper("previous", Box::new(helpers::navigation::previous));
- handlebars.register_helper("next", Box::new(helpers::navigation::next));
// TODO: remove theme_option in 0.5, it is not needed.
handlebars.register_helper("theme_option", Box::new(helpers::theme::theme_option));
}
@@ -442,21 +458,26 @@ impl Renderer for HtmlHandlebars {
utils::fs::write_file(destination, "CNAME", format!("{cname}\n").as_bytes())?;
}
- let mut is_index = true;
- for item in book.iter() {
- let ctx = RenderItemContext {
+ let chapters: Vec<_> = book
+ .iter()
+ .filter_map(|item| match item {
+ BookItem::Chapter(ch) if !ch.is_draft_chapter() => Some(ch),
+ _ => None,
+ })
+ .collect();
+ for (i, ch) in chapters.iter().enumerate() {
+ let previous = (i != 0).then(|| chapters[i - 1]);
+ let next = (i != chapters.len() - 1).then(|| chapters[i + 1]);
+ let ctx = RenderChapterContext {
handlebars: &handlebars,
destination: destination.to_path_buf(),
data: data.clone(),
- is_index,
book_config: book_config.clone(),
html_config: html_config.clone(),
edition: ctx.config.rust.edition,
chapter_titles: &ctx.chapter_titles,
};
- self.render_item(item, ctx, &mut print_content)?;
- // Only the first non-draft chapter item should be treated as the "index"
- is_index &= !matches!(item, BookItem::Chapter(ch) if !ch.is_draft_chapter());
+ self.render_chapter(ch, previous, next, ctx, &mut print_content)?;
}
// Render 404 page
@@ -927,11 +948,10 @@ fn partition_source(s: &str) -> (String, String) {
(before, after)
}
-struct RenderItemContext<'a> {
+struct RenderChapterContext<'a> {
handlebars: &'a Handlebars<'a>,
destination: PathBuf,
data: serde_json::Map,
- is_index: bool,
book_config: BookConfig,
html_config: HtmlConfig,
edition: Option,
diff --git a/crates/mdbook-html/src/html_handlebars/helpers/mod.rs b/crates/mdbook-html/src/html_handlebars/helpers/mod.rs
index c2a52a8414..720704f3e1 100644
--- a/crates/mdbook-html/src/html_handlebars/helpers/mod.rs
+++ b/crates/mdbook-html/src/html_handlebars/helpers/mod.rs
@@ -1,4 +1,3 @@
-pub(crate) mod navigation;
pub(crate) mod resources;
pub(crate) mod theme;
pub(crate) mod toc;
diff --git a/crates/mdbook-html/src/html_handlebars/helpers/navigation.rs b/crates/mdbook-html/src/html_handlebars/helpers/navigation.rs
deleted file mode 100644
index 38a3a08c11..0000000000
--- a/crates/mdbook-html/src/html_handlebars/helpers/navigation.rs
+++ /dev/null
@@ -1,302 +0,0 @@
-use std::collections::BTreeMap;
-use std::path::Path;
-
-use handlebars::{
- Context, Handlebars, Helper, Output, RenderContext, RenderError, RenderErrorReason, Renderable,
-};
-
-use log::{debug, trace};
-use mdbook_core::utils;
-use serde_json::json;
-
-type StringMap = BTreeMap;
-
-/// Target for `find_chapter`.
-enum Target {
- Previous,
- Next,
-}
-
-impl Target {
- /// Returns target if found.
- fn find(
- &self,
- base_path: &str,
- current_path: &str,
- current_item: &StringMap,
- previous_item: &StringMap,
- ) -> Result