From 675050fd56e1bb94fd4e2511881b09f7f8e475ef Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Sat, 24 Jan 2026 21:52:12 -0700 Subject: [PATCH] fix: correct depth calculation for relative paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - src/templates.rs: Add path_depth() helper that counts directory segments minus filename, not raw slash count. "/blog/slug.html" → depth=1 (one directory level) "/index.html" → depth=0 (root level) This fixes relative path generation after adding canonical URLs. file:// navigation now works correctly alongside absolute canonical URLs. --- src/templates.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/templates.rs b/src/templates.rs index 3d52364..9aa18b7 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -16,6 +16,17 @@ fn relative_prefix(depth: usize) -> String { } } +/// Calculate directory depth from page path. +/// "/index.html" → 0 +/// "/about.html" → 0 +/// "/blog/index.html" → 1 +/// "/blog/slug.html" → 1 +fn path_depth(page_path: &str) -> usize { + // Count segments: split by '/', filter empties, subtract 1 for filename + let segments: Vec<_> = page_path.split('/').filter(|s| !s.is_empty()).collect(); + segments.len().saturating_sub(1) +} + /// Render a blog post with the base layout. pub fn render_post( frontmatter: &Frontmatter, @@ -23,7 +34,7 @@ pub fn render_post( page_path: &str, config: &SiteConfig, ) -> Markup { - let depth = page_path.matches('/').count(); + let depth = path_depth(page_path); let prefix = relative_prefix(depth); base_layout( &frontmatter.title, @@ -173,7 +184,7 @@ pub fn render_projects_index( /// Base HTML layout wrapper. fn base_layout(title: &str, content: Markup, page_path: &str, config: &SiteConfig) -> Markup { - let depth = page_path.matches('/').count(); + let depth = path_depth(page_path); let prefix = relative_prefix(depth); let canonical_url = format!("{}{}", config.base_url.trim_end_matches('/'), page_path);