Skip to content

Commit 146e754

Browse files
authored
URL-encode the image paths in Markdown so that images with filenames (#41788)
Closes #41786 Release Notes: - markdown preview: Fixed an issue where path urls would not be parsed correctly when containing URL-encoded characters <img width="1680" height="1126" alt="569415cb-b3e8-4ad6-b31c-a1898ec32085" src="https://github.com/user-attachments/assets/7de8a892-ff01-4e00-a28c-1c5e9206ce3a" />
1 parent 278fe91 commit 146e754

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/markdown_preview/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pulldown-cmark.workspace = true
3131
settings.workspace = true
3232
theme.workspace = true
3333
ui.workspace = true
34+
urlencoding.workspace = true
3435
util.workspace = true
3536
workspace.workspace = true
3637

crates/markdown_preview/src/markdown_elements.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use gpui::{
44
};
55
use language::HighlightId;
66
use std::{fmt::Display, ops::Range, path::PathBuf};
7+
use urlencoding;
78

89
#[derive(Debug)]
910
#[cfg_attr(test, derive(PartialEq))]
@@ -278,7 +279,12 @@ impl Link {
278279
return Some(Link::Web { url: text });
279280
}
280281

281-
let path = PathBuf::from(&text);
282+
// URL decode the text to handle spaces and other special characters
283+
let decoded_text = urlencoding::decode(&text)
284+
.map(|s| s.into_owned())
285+
.unwrap_or(text);
286+
287+
let path = PathBuf::from(&decoded_text);
282288
if path.is_absolute() && path.exists() {
283289
return Some(Link::Path {
284290
display_path: path.clone(),
@@ -288,7 +294,7 @@ impl Link {
288294

289295
if let Some(file_location_directory) = file_location_directory {
290296
let display_path = path;
291-
let path = file_location_directory.join(text);
297+
let path = file_location_directory.join(decoded_text);
292298
if path.exists() {
293299
return Some(Link::Path { display_path, path });
294300
}

0 commit comments

Comments
 (0)