diff --git a/crates/mdbook-core/src/utils/fs.rs b/crates/mdbook-core/src/utils/fs.rs index d32f5705..ebc7dc63 100644 --- a/crates/mdbook-core/src/utils/fs.rs +++ b/crates/mdbook-core/src/utils/fs.rs @@ -6,14 +6,6 @@ use std::io::Write; use std::path::{Component, Path, PathBuf}; use tracing::{debug, trace}; -/// Naively replaces any path separator with a forward-slash '/' -pub fn normalize_path(path: &str) -> String { - use std::path::is_separator; - path.chars() - .map(|ch| if is_separator(ch) { '/' } else { ch }) - .collect::() -} - /// Write the given data to a file, creating it first if necessary pub fn write_file>(build_dir: &Path, filename: P, content: &[u8]) -> Result<()> { let path = build_dir.join(filename); diff --git a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs index a0f01c7a..373b5e1e 100644 --- a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs +++ b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs @@ -1,6 +1,7 @@ use super::helpers; use super::static_files::StaticFiles; use crate::theme::Theme; +use crate::utils::ToUrlPath; use anyhow::{Context, Result, bail}; use handlebars::Handlebars; use mdbook_core::book::{Book, BookItem, Chapter}; @@ -122,9 +123,7 @@ impl HtmlHandlebars { .as_ref() .unwrap() .with_extension("html") - .to_str() - .unwrap() - .replace('\\', "//"); + .to_url_path(); let obj = json!( { "title": ch.name, "link": path, @@ -1054,7 +1053,7 @@ fn collect_redirects_for_path( path: &Path, redirects: &HashMap, ) -> Result> { - let path = format!("/{}", path.display().to_string().replace('\\', "/")); + let path = format!("/{}", path.to_url_path()); if redirects.contains_key(&path) { bail!( "redirect found for existing chapter at `{path}`\n\ diff --git a/crates/mdbook-html/src/html_handlebars/helpers/toc.rs b/crates/mdbook-html/src/html_handlebars/helpers/toc.rs index cd876e08..d299803d 100644 --- a/crates/mdbook-html/src/html_handlebars/helpers/toc.rs +++ b/crates/mdbook-html/src/html_handlebars/helpers/toc.rs @@ -1,3 +1,4 @@ +use crate::utils::ToUrlPath; use std::path::Path; use std::{cmp::Ordering, collections::BTreeMap}; @@ -109,12 +110,7 @@ impl HelperDef for RenderToc { let path_exists = match item.get("path") { Some(path) if !path.is_empty() => { out.write(" String; +} + +impl ToUrlPath for Path { + fn to_url_path(&self) -> String { + // We're generally assuming that all paths we deal with are utf-8. + // The replace here is to handle Windows paths. + self.to_str().unwrap().replace('\\', "/") + } +}