Skip to content

Fix GitHub relative SVGs not rendering properly #1852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,39 @@ impl SanitizeUrl {
}
}

/// Groups media-related URL info
struct MediaUrl {
is_media: bool,
add_sanitize_query: bool,
}

/// Determine whether the given URL has a media file externsion.
fn is_media_url(url: &str) -> bool {
/// Also check if `sanitize=true` must be added to the query string,
/// which is required to load SVGs properly from GitHub.
fn is_media_url(url: &str) -> MediaUrl {
Path::new(url)
.extension()
.and_then(std::ffi::OsStr::to_str)
.map_or(false, |e| match e {
"png" | "svg" | "jpg" | "jpeg" | "gif" | "mp4" | "webm" | "ogg" => true,
_ => false,
})
.map_or(
MediaUrl {
is_media: false,
add_sanitize_query: false,
},
|e| match e {
"svg" => MediaUrl {
is_media: true,
add_sanitize_query: true,
},
"png" | "jpg" | "jpeg" | "gif" | "mp4" | "webm" | "ogg" => MediaUrl {
is_media: true,
add_sanitize_query: false,
},
_ => MediaUrl {
is_media: false,
add_sanitize_query: false,
},
},
)
}

impl UrlRelativeEvaluate for SanitizeUrl {
Expand All @@ -169,7 +193,11 @@ impl UrlRelativeEvaluate for SanitizeUrl {
let mut new_url = base_url.clone();
// Assumes GitHub’s URL scheme. GitHub renders text and markdown
// better in the "blob" view, but images need to be served raw.
new_url += if is_media_url(url) {
let MediaUrl {
is_media,
add_sanitize_query,
} = is_media_url(url);
new_url += if is_media {
"raw/master"
} else {
"blob/master"
Expand All @@ -178,6 +206,12 @@ impl UrlRelativeEvaluate for SanitizeUrl {
new_url.push('/');
}
new_url += url;
if add_sanitize_query {
if let Ok(mut parsed_url) = Url::parse(&new_url) {
parsed_url.query_pairs_mut().append_pair("sanitize", "true");
new_url = parsed_url.into_string();
}
}
Cow::Owned(new_url)
})
}
Expand Down Expand Up @@ -356,6 +390,7 @@ mod tests {
let absolute = "[hi](/hi)";
let relative = "[there](there)";
let image = "![alt](img.png)";
let svg = "![alt](sanitize.svg)";

for host in &["github.com", "gitlab.com", "bitbucket.org"] {
for (&extra_slash, &dot_git) in [true, false].iter().zip(&[true, false]) {
Expand Down Expand Up @@ -392,6 +427,15 @@ mod tests {
host
)
);

let result = markdown_to_html(svg, Some(&url));
assert_eq!(
result,
format!(
"<p><img src=\"https://{}/rust-lang/test/raw/master/sanitize.svg?sanitize=true\" alt=\"alt\"></p>\n",
host
)
);
}
}

Expand Down