From f4b722e79502f2a2d03cd2be824b0c0b49ee8c40 Mon Sep 17 00:00:00 2001 From: Dylan Treisman Date: Fri, 31 Jul 2020 14:05:53 +1000 Subject: [PATCH 1/2] Modify redirect behaviour to respect site_url --- src/renderer/html_handlebars/hbs_renderer.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index fa576998f1..46befd6ff6 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -140,7 +140,7 @@ impl HtmlHandlebars { } else { debug!( "HTML 'site-url' parameter not set, defaulting to '/'. Please configure \ - this to ensure the 404 page work correctly, especially if your site is hosted in a \ + this to ensure the 404 page works correctly, especially if your site is hosted in a \ subdirectory on the HTTP server." ); "/" @@ -348,6 +348,7 @@ impl HtmlHandlebars { root: &Path, handlebars: &Handlebars<'_>, redirects: &HashMap, + site_url: &Option, ) -> Result<()> { if redirects.is_empty() { return Ok(()); @@ -357,12 +358,18 @@ impl HtmlHandlebars { for (original, new) in redirects { log::debug!("Redirecting \"{}\" → \"{}\"", original, new); - // Note: all paths are relative to the build directory, so the + // Note: all original paths are relative to the build directory, so the // leading slash in an absolute path means nothing (and would mess // up `root.join(original)`). let original = original.trim_start_matches("/"); let filename = root.join(original); - self.emit_redirect(handlebars, &filename, new)?; + let regex = Regex::new(r##"(^/.*$)"##).unwrap(); + let destination = if site_url.is_some() && site_url.as_ref() != Some(&"/".to_string()) && regex.is_match(new) { + format!("{}{}", site_url.as_ref().unwrap().to_string(), new.trim_start_matches("/")) + } else { + new.to_string() + }; + self.emit_redirect(handlebars, &filename, &destination)?; } Ok(()) @@ -535,8 +542,7 @@ impl Renderer for HtmlHandlebars { super::search::create_files(&search, &destination, &book)?; } } - - self.emit_redirects(&ctx.destination, &handlebars, &html_config.redirect) + self.emit_redirects(&ctx.destination, &handlebars, &html_config.redirect, &html_config.site_url) .context("Unable to emit redirects")?; // Copy all remaining files, avoid a recursive copy from/to the book build dir From 5271dc418fab868fbcbd3e32f1caa730f6826db8 Mon Sep 17 00:00:00 2001 From: Dylan Treisman Date: Fri, 31 Jul 2020 15:21:03 +1000 Subject: [PATCH 2/2] Clean up site_url redirect logic --- src/renderer/html_handlebars/hbs_renderer.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 46befd6ff6..032e41ecef 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -363,9 +363,10 @@ impl HtmlHandlebars { // up `root.join(original)`). let original = original.trim_start_matches("/"); let filename = root.join(original); - let regex = Regex::new(r##"(^/.*$)"##).unwrap(); + let regex = Regex::new(r##"(^/)"##).unwrap(); let destination = if site_url.is_some() && site_url.as_ref() != Some(&"/".to_string()) && regex.is_match(new) { - format!("{}{}", site_url.as_ref().unwrap().to_string(), new.trim_start_matches("/")) + let site_url_value = site_url.as_ref().unwrap().to_string(); + format!("{}{}", site_url_value.trim_end_matches("/"), new) } else { new.to_string() };