From 3d68de23d4c3aadf1ca48cb9c67f995de17beb98 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 23 Apr 2024 22:55:41 +0200 Subject: [PATCH] Only strip `index.html` if it is at the end ... and only if it is the full file name... I noticed this because I want to convert https://git-scm.com/ to use Pagefind, but any search hitting `/docs/git-checkout-index.html` would mistakenly link to `/docs/git-checkout-`. Signed-off-by: Johannes Schindelin --- pagefind/src/fossick/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pagefind/src/fossick/mod.rs b/pagefind/src/fossick/mod.rs index 590f7383..4ffddeb2 100644 --- a/pagefind/src/fossick/mod.rs +++ b/pagefind/src/fossick/mod.rs @@ -486,6 +486,16 @@ impl Fossicker { } } +fn strip_index_html(url: &str) -> &str { + if url.ends_with("/index.html") { + &url[..url.len() - 10] + } else if url == "index.html" { + "" + } else { + url + } +} + fn build_url(page_url: &Path, relative_to: Option<&Path>, options: &SearchOptions) -> String { let prefix = relative_to.unwrap_or(&options.site_source); @@ -503,7 +513,7 @@ fn build_url(page_url: &Path, relative_to: Option<&Path>, options: &SearchOption }; let final_url: String = if !options.keep_index_url { - url.to_slash_lossy().to_owned().replace("index.html", "") + strip_index_html(&url.to_slash_lossy()).to_string() } else { url.to_slash_lossy().to_owned().to_string() };