Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions crates/mdbook-core/src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>()
}

/// Write the given data to a file, creating it first if necessary
pub fn write_file<P: AsRef<Path>>(build_dir: &Path, filename: P, content: &[u8]) -> Result<()> {
let path = build_dir.join(filename);
Expand Down
7 changes: 3 additions & 4 deletions crates/mdbook-html/src/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1054,7 +1053,7 @@ fn collect_redirects_for_path(
path: &Path,
redirects: &HashMap<String, String>,
) -> Result<BTreeMap<String, String>> {
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\
Expand Down
8 changes: 2 additions & 6 deletions crates/mdbook-html/src/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::ToUrlPath;
use std::path::Path;
use std::{cmp::Ordering, collections::BTreeMap};

Expand Down Expand Up @@ -109,12 +110,7 @@ impl HelperDef for RenderToc {
let path_exists = match item.get("path") {
Some(path) if !path.is_empty() => {
out.write("<a href=\"")?;
let tmp = Path::new(path)
.with_extension("html")
.to_str()
.unwrap()
// Hack for windows who tends to use `\` as separator instead of `/`
.replace('\\', "/");
let tmp = Path::new(path).with_extension("html").to_url_path();

// Add link
out.write(&tmp)?;
Expand Down
11 changes: 5 additions & 6 deletions crates/mdbook-html/src/html_handlebars/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::static_files::StaticFiles;
use crate::theme::searcher;
use anyhow::{Context, Result, bail};
use crate::utils::ToUrlPath;
use anyhow::{Result, bail};
use elasticlunr::{Index, IndexBuilder};
use mdbook_core::book::{Book, BookItem, Chapter};
use mdbook_core::config::{Search, SearchChapterSettings};
Expand Down Expand Up @@ -128,11 +129,9 @@ fn render_item(
.path
.as_ref()
.expect("Checked that path exists above");
let filepath = Path::new(&chapter_path).with_extension("html");
let filepath = filepath
.to_str()
.with_context(|| "Could not convert HTML path to str")?;
let anchor_base = utils::fs::normalize_path(filepath);
let anchor_base = Path::new(&chapter_path)
.with_extension("html")
.to_url_path();

let options = HtmlRenderOptions::new(&chapter_path);
let mut p = new_cmark_parser(&chapter.content, &options.markdown_options).peekable();
Expand Down
1 change: 1 addition & 0 deletions crates/mdbook-html/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod html_handlebars;
pub mod theme;
pub(crate) mod utils;

pub use html_handlebars::HtmlHandlebars;
use mdbook_core::config::HtmlConfig;
Expand Down
16 changes: 16 additions & 0 deletions crates/mdbook-html/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Utilities for processing HTML.

use std::path::Path;

/// Helper trait for converting a [`Path`] to a string suitable for an HTML path.
pub(crate) trait ToUrlPath {
fn to_url_path(&self) -> 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('\\', "/")
}
}
Loading