fix: preserve data URI in img src during markdown rendering

ammonia's default url_schemes whitelist excludes 'data', causing
data:image/* URLs to be stripped from img src attributes. Add 'data'
to allowed schemes. Also skip data: URIs in JS thumbnail logic.
This commit is contained in:
xfy 2026-06-09 10:06:34 +08:00
parent 3974856f3d
commit 42b39266a6
2 changed files with 12 additions and 0 deletions

View File

@ -15,6 +15,7 @@ pub fn clean_html(input: &str) -> String {
])
.add_tags(&["details", "summary"])
.url_relative(ammonia::UrlRelative::PassThrough)
.add_url_schemes(&["data"])
.add_tag_attributes("a", &["class", "aria-hidden", "aria-label"])
.add_tag_attributes("span", &["class"])
.add_tag_attributes("h1", &["id", "class"])
@ -384,4 +385,11 @@ mod tests {
assert!(result.html.contains("<pre><code>"));
assert!(result.html.contains("main"));
}
#[test]
fn render_markdown_data_uri_image() {
let result = render_markdown_enhanced("![alt](data:image/svg+xml,%3csvg%3e%3c/svg%3e)");
assert!(result.html.contains("data:image/svg+xml"), "data URI should be preserved in img src, got: {}", result.html);
assert!(result.html.contains("alt=\"alt\""));
}
}

View File

@ -46,6 +46,10 @@ pub fn PostContent(content_html: String) -> Element {
let original_src = img_element.src();
let alt = img_element.alt();
if original_src.starts_with("data:") {
continue;
}
// Replace src with thumbnail version (add ?w=800)
let thumb_src = if original_src.contains('?') {
format!("{}&w=800", original_src)