From 304434d5a339ac98eb0187715cb046d01703b292 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 16:06:48 -0600 Subject: [PATCH] fix(web): enable comrak autolink so bare URLs/emails become links (#998) commonmark_options enabled strikethrough/tasklist/table but not autolink, so bare URLs and emails parsed as plain text. No deliberate-off reason found; autolink produces standard Link nodes handled by the existing codec both directions. Tests: bare URL link mark, bare email mailto mark, save fixpoint, code-span negative. --- pkgs/id/src/web/markdown.rs | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 9d05351b6b..0b67862e56 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -85,6 +85,7 @@ fn commonmark_options() -> Options<'static> { options.extension.strikethrough = true; options.extension.tasklist = true; options.extension.table = true; + options.extension.autolink = true; // #998: bare URLs/emails become links options.extension.footnotes = true; options.extension.front_matter_delimiter = Some("---".to_owned()); options.parse.smart = false; // Don't convert quotes/dashes @@ -5475,6 +5476,73 @@ mod tests { } } + #[test] + fn test_bare_url_becomes_link_mark() { + // #998: with the autolink extension enabled, a bare URL parses + // to a text node carrying a link mark, like the angle form. + let doc = markdown_to_prosemirror("visit https://example.com/path?a=1 now"); + let para_content = doc["content"][0]["content"].as_array().unwrap(); + let link_text = para_content.iter().find(|n| { + n["marks"] + .as_array() + .is_some_and(|m| m.iter().any(|mark| mark["type"] == "link")) + }); + assert!(link_text.is_some(), "bare URL has no link mark"); + let link_mark = link_text.unwrap()["marks"] + .as_array() + .unwrap() + .iter() + .find(|m| m["type"] == "link") + .unwrap(); + assert_eq!( + link_mark["attrs"]["href"], + "https://example.com/path?a=1" + ); + } + + #[test] + fn test_bare_email_becomes_link_mark() { + // #998: a bare email address autolinks with a mailto: href. + let doc = markdown_to_prosemirror("mail me@example.com today"); + let para_content = doc["content"][0]["content"].as_array().unwrap(); + let link_text = para_content.iter().find(|n| { + n["marks"] + .as_array() + .is_some_and(|m| m.iter().any(|mark| mark["type"] == "link")) + }); + assert!(link_text.is_some(), "bare email has no link mark"); + let link = link_text.unwrap(); + assert_eq!(link["text"], "me@example.com"); + let link_mark = link["marks"] + .as_array() + .unwrap() + .iter() + .find(|m| m["type"] == "link") + .unwrap(); + assert_eq!(link_mark["attrs"]["href"], "mailto:me@example.com"); + } + + #[test] + fn test_bare_url_fixpoint_and_code_span_negative() { + // #998: a bare URL round-trips to a stable fixpoint, and a URL + // inside a code span stays literal text with no link mark. + let (first, second) = two_saves("visit https://example.com/path?a=1 now\n"); + assert_eq!(first, second, "no fixpoint for bare URL"); + assert!( + first.contains("https://example.com/path?a=1"), + "URL lost in save: {first:?}" + ); + + let doc = markdown_to_prosemirror("run `https://example.com` now"); + let para_content = doc["content"][0]["content"].as_array().unwrap(); + let has_link = para_content.iter().any(|n| { + n["marks"] + .as_array() + .is_some_and(|m| m.iter().any(|mark| mark["type"] == "link")) + }); + assert!(!has_link, "code-span URL must not carry a link mark"); + } + #[test] fn test_misplaced_nodes_return_error_not_panic() { // #1043: json_to_ast placed any known node type anywhere, so a