From 146e754f7396f62b7030f6d4424364b668704005 Mon Sep 17 00:00:00 2001 From: aohanhongzhi <37319319+aohanhongzhi@users.noreply.github.com> Date: Fri, 7 Nov 2025 21:02:40 +0800 Subject: [PATCH] URL-encode the image paths in Markdown so that images with filenames (#41788) Closes https://github.com/zed-industries/zed/issues/41786 Release Notes: - markdown preview: Fixed an issue where path urls would not be parsed correctly when containing URL-encoded characters 569415cb-b3e8-4ad6-b31c-a1898ec32085 --- Cargo.lock | 1 + crates/markdown_preview/Cargo.toml | 1 + crates/markdown_preview/src/markdown_elements.rs | 10 ++++++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f4217ef1528ffb2fa7a1a16f589e5aacda3668a..4553c965950a6f7f0ce74ee7c53265bd1c05c0c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9680,6 +9680,7 @@ dependencies = [ "settings", "theme", "ui", + "urlencoding", "util", "workspace", ] diff --git a/crates/markdown_preview/Cargo.toml b/crates/markdown_preview/Cargo.toml index c351ad8634be45f3c7b845eecdbc24e89d1fd190..89e5ec5921a3ad330a75343e980dfeff0f535b00 100644 --- a/crates/markdown_preview/Cargo.toml +++ b/crates/markdown_preview/Cargo.toml @@ -31,6 +31,7 @@ pulldown-cmark.workspace = true settings.workspace = true theme.workspace = true ui.workspace = true +urlencoding.workspace = true util.workspace = true workspace.workspace = true diff --git a/crates/markdown_preview/src/markdown_elements.rs b/crates/markdown_preview/src/markdown_elements.rs index 17dec6c2194747cfcd397395903a6b4bb5c309d0..9ed885f8765dd58a929b6125d6e7064278dab3bc 100644 --- a/crates/markdown_preview/src/markdown_elements.rs +++ b/crates/markdown_preview/src/markdown_elements.rs @@ -4,6 +4,7 @@ use gpui::{ }; use language::HighlightId; use std::{fmt::Display, ops::Range, path::PathBuf}; +use urlencoding; #[derive(Debug)] #[cfg_attr(test, derive(PartialEq))] @@ -278,7 +279,12 @@ impl Link { return Some(Link::Web { url: text }); } - let path = PathBuf::from(&text); + // URL decode the text to handle spaces and other special characters + let decoded_text = urlencoding::decode(&text) + .map(|s| s.into_owned()) + .unwrap_or(text); + + let path = PathBuf::from(&decoded_text); if path.is_absolute() && path.exists() { return Some(Link::Path { display_path: path.clone(), @@ -288,7 +294,7 @@ impl Link { if let Some(file_location_directory) = file_location_directory { let display_path = path; - let path = file_location_directory.join(text); + let path = file_location_directory.join(decoded_text); if path.exists() { return Some(Link::Path { display_path, path }); }