diff --git a/src/main.rs b/src/main.rs index 9ba8031..7eb3c6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -776,8 +776,11 @@ fn load_recent_files() -> Vec { }; let mut files = Vec::new(); for line in txt.lines() { - let path = PathBuf::from(line); - if line.is_empty() || !path.exists() || files.iter().any(|p| p == &path) { + if line.is_empty() { + continue; + } + let path = session::strip_verbatim(PathBuf::from(line)); + if !path.exists() || files.iter().any(|p| p == &path) { continue; } files.push(path); @@ -819,33 +822,11 @@ fn forget_recent_file(files: &Arc>>, path: &Path) -> bool { true } -fn percent_encode_file_path(s: &str) -> String { - let mut out = String::with_capacity(s.len()); - for &b in s.as_bytes() { - match b { - b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'/' | b':' | b'-' | b'_' | b'.' | b'~' => { - out.push(b as char) - } - _ => out.push_str(&format!("%{b:02X}")), - } - } - out -} - fn base_href_for_file(path: &Path) -> Option { let dir = path.parent()?; - Some(file_url_for_path_dir(dir)) -} - -fn file_url_for_path_dir(dir: &Path) -> String { - let mut path = dir.to_string_lossy().replace('\\', "/"); - if cfg!(windows) && !path.starts_with('/') { - path.insert(0, '/'); - } - if !path.ends_with('/') { - path.push('/'); - } - format!("file://{}", percent_encode_file_path(&path)) + url::Url::from_directory_path(dir) + .ok() + .map(|url| url.to_string()) } fn starts_mermaid_fence(line: &str) -> bool { @@ -2114,6 +2095,20 @@ mod tests { assert!(!html.contains("mdp-mark"), "code blocks must not get highlight markup"); } + #[test] + fn base_href_is_a_directory_file_url() { + let dir = temp_test_dir("base-href"); + let file = dir.join("note.md"); + fs::write(&file, "# x").unwrap(); + + let href = base_href_for_file(&file).unwrap(); + + assert!(href.starts_with("file:///"), "{href}"); + assert!(href.ends_with('/'), "{href}"); + assert!(!href.contains("%3F"), "{href}"); + let _ = fs::remove_dir_all(dir); + } + #[test] fn local_relative_images_are_embedded_from_markdown_directory() { let dir = temp_test_dir("local-image"); diff --git a/src/session.rs b/src/session.rs index 7cf6a3e..14a3b98 100644 --- a/src/session.rs +++ b/src/session.rs @@ -153,13 +153,30 @@ fn normalize_path(path: PathBuf) -> PathBuf { } else { std::env::current_dir().unwrap_or_default().join(path) }; - fs::canonicalize(&absolute).unwrap_or_else(|_| { + let resolved = fs::canonicalize(&absolute).unwrap_or_else(|_| { absolute .parent() .and_then(|parent| fs::canonicalize(parent).ok()) .and_then(|parent| absolute.file_name().map(|name| parent.join(name))) .unwrap_or(absolute) - }) + }); + strip_verbatim(resolved) +} + +/// Windows canonicalize returns verbatim paths. Strip that prefix so session +/// and recent-file entries stay readable and usable by other tools. +pub(crate) fn strip_verbatim(path: PathBuf) -> PathBuf { + #[cfg(target_os = "windows")] + { + let text = path.to_string_lossy().into_owned(); + if let Some(rest) = text.strip_prefix(r"\\?\UNC\") { + return PathBuf::from(format!(r"\\{rest}")); + } + if let Some(rest) = text.strip_prefix(r"\\?\") { + return PathBuf::from(rest.to_string()); + } + } + path } #[cfg(test)] @@ -217,6 +234,22 @@ mod tests { let _ = fs::remove_dir_all(dir); } + #[cfg(target_os = "windows")] + #[test] + fn opened_paths_drop_windows_verbatim_prefix() { + let dir = temp_dir("verbatim"); + let file = dir.join("note.md"); + fs::write(&file, "# Note").unwrap(); + let mut session = DocumentSession::default(); + + session.open(file.clone(), false); + + let stored = session.tabs[0].path.to_string_lossy().to_string(); + assert!(!stored.starts_with(r"\\?\"), "verbatim prefix leaked: {stored}"); + assert!(session.tabs[0].path.ends_with("note.md")); + let _ = fs::remove_dir_all(dir); + } + #[test] fn persisted_session_keeps_missing_tabs_and_active_order() { let dir = temp_dir("roundtrip"); @@ -232,12 +265,13 @@ mod tests { let restored = DocumentSession::load(&state_path); assert_eq!(restored.tabs.len(), 2); - assert_eq!(restored.tabs[0].path, fs::canonicalize(existing).unwrap()); + assert_eq!( + restored.tabs[0].path, + strip_verbatim(fs::canonicalize(existing).unwrap()) + ); assert_eq!( restored.tabs[1].path, - fs::canonicalize(&dir) - .unwrap() - .join(missing.file_name().unwrap()) + strip_verbatim(fs::canonicalize(&dir).unwrap()).join(missing.file_name().unwrap()) ); assert!(!restored.tabs[0].missing); assert!(restored.tabs[1].missing);