diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 6788549734..a35e201f4a 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -158,7 +158,18 @@ fn markdown_to_prosemirror_inner(markdown: &str, recover_orphans: bool) -> Value // source and append them as the same PM nodes the referenced path // produces. if recover_orphans { - preserve_unreferenced_footnote_definitions(&mut doc, &protected, newline_token.as_deref()); + let footnote_link_references = preserve_unreferenced_footnote_definitions( + &mut doc, + &protected, + newline_token.as_deref(), + ); + preserve_unreferenced_link_reference_definitions( + &mut doc, + root, + &protected, + &footnote_link_references, + newline_token.as_deref(), + ); } doc } @@ -3184,6 +3195,322 @@ mod tests { ); } + #[test] + fn test_unused_link_reference_definitions_survive_roundtrip() { + let original = + "Body text with no reference.\n\n[a]: https://a.example\n[b]: https://b.example\n"; + let doc = markdown_to_prosemirror(original); + let recovered: Vec<&Value> = doc["content"] + .as_array() + .unwrap() + .iter() + .filter(|node| node["type"] == "code_block" && node["attrs"]["htmlBlock"] == true) + .collect(); + assert_eq!( + recovered.len(), + 2, + "both unused definitions are retained: {doc}" + ); + + let (first, second) = two_saves(original); + for definition in ["[a]: https://a.example", "[b]: https://b.example"] { + assert!( + first.contains(definition), + "{definition} survives: {first:?}" + ); + } + assert_eq!(first, second, "unused definitions round-trip to a fixpoint"); + } + + #[test] + fn test_front_matter_definition_is_not_recovered() { + // Codex P2 / CodeRabbit on PR #5949: a definition-shaped line + // inside front matter is metadata; recovering it appended a + // duplicate outside the block on every save. + let original = "---\n[docs]: https://example.com\n---\n\nBody\n"; + let (first, second) = two_saves(original); + assert_eq!( + first.matches("[docs]:").count(), + 1, + "front-matter definition is not duplicated: {first:?}" + ); + assert_eq!(first, second, "front-matter round-trip is a fixpoint"); + } + + #[test] + fn test_dots_line_does_not_close_front_matter_for_definition_collection() { + // Codex P2 on PR #5949 (fresh round): comrak's front-matter + // parser requires the configured `---` closing delimiter; a + // `...` line does not close the block, so the opening lines + // are ordinary Markdown and `[url]: /target` is a real + // definition the orphan footnote's link resolves through. + let original = "---\n[url]: /target\n...\n\n[^n]: [label][url]\n"; + let (first, second) = two_saves(original); + assert!( + first.contains("[label](/target)"), + "footnote link resolves through the definition: {first:?}" + ); + assert_eq!(first, second, "dots-delimiter round-trip is a fixpoint"); + } + + #[test] + fn test_quoted_multiline_definition_label_matches_without_container() { + // Codex P2 on PR #5949 (fresh round): the quoted definition's + // label spans two lines; stripping only the first line's + // container left the continuation's `>` in the matching key, + // so the unused definition was keyed as the used label + // `long > label` and silently dropped. + let original = + "[long > label]\n\n[long > label]: /used\n\n> [long\n> label]: /keep\n"; + let (first, second) = two_saves(original); + assert!( + first.contains("/keep"), + "unused quoted multiline definition is preserved: {first:?}" + ); + assert!( + !first.contains("[long > label]: /used"), + "consumed definition is not appended: {first:?}" + ); + assert_eq!(first, second, "quoted-multiline round-trip is a fixpoint"); + } + + #[test] + fn test_code_span_bracket_does_not_end_link_text() { + // Codex P2 on PR #5949 (fresh round): in `[x`]...` the first + // closing bracket belongs to an inline code span; the link's + // explicit label is `used`, and keying the use as `x\`` made + // the genuinely unused `[x`]` definition look consumed. + let original = "[x`]\u{60}][used]\n\n[used]: /used-target\n[x`]: /keep\n"; + let (first, second) = two_saves(original); + assert!( + first.contains("(/used-target)"), + "explicit reference resolves: {first:?}" + ); + assert!( + first.contains("[x`]: /keep"), + "genuinely unused definition is preserved: {first:?}" + ); + assert!( + !first.contains("[used]: /used-target"), + "consumed definition is not appended: {first:?}" + ); + assert_eq!(first, second, "code-span-bracket round-trip is a fixpoint"); + } + + #[test] + fn test_quoted_multiline_reference_use_ignores_containers() { + // Codex P2 on PR #5949 (round 4): the quoted shortcut link's + // label spans two lines; joining the raw source lines kept + // the continuation's `>` in the used key, so the use was + // keyed `long > label` and the genuinely unused definition + // with that literal label was discarded as consumed. + let original = "> [long\n> label]\n\n[long label]: /used\n[long > label]: /keep\n"; + let (first, second) = two_saves(original); + assert!( + first.contains("[long > label]: /keep"), + "genuinely unused definition is preserved: {first:?}" + ); + assert!( + !first.contains("[long label]: /used"), + "consumed definition is not appended: {first:?}" + ); + assert_eq!(first, second, "quoted-multiline-use round-trip is a fixpoint"); + } + + #[test] + fn test_inline_html_bracket_does_not_end_link_text() { + // Codex P2 on PR #5949 (round 4): the `]` inside an inline + // HTML attribute is not the link-text boundary; the link's + // explicit label is `used`, and keying the use as the HTML + // fragment made the genuinely unused definition look + // consumed. + let original = + "[label][used]\n\n[used]: /used\n[ = doc["content"] + .as_array() + .unwrap() + .iter() + .filter(|node| { + node["type"] == "code_block" + && node["attrs"]["htmlBlock"] == true + && node["content"][0]["text"] == "[quoted]: https://quoted.example" + }) + .collect(); + assert_eq!( + recovered.len(), + 1, + "only the real blockquote definition is recovered: {doc}" + ); + let (first, second) = two_saves(original); + assert!( + first.contains("[quoted]: https://quoted.example"), + "real quoted definition survives after hoisting to document scope: {first:?}" + ); + assert!( + first.contains("```\n[fenced]: https://fenced.example"), + "fenced sample remains code only: {first:?}" + ); + assert!( + first.contains("```\n[indented]: https://indented.example"), + "indented sample remains code only: {first:?}" + ); + assert!( + first.contains("