From 023daef67536059362c13c5167ab0d414968269e Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Thu, 12 Nov 2026 13:55:49 -0600 Subject: [PATCH] fix(id-web): choose spaced-destination placeholders absent from the document (#1348) The index-only placeholder token could already occur in the document text, and the global restore replace then rewrote the document own words into the spaced destination. substitute_spaced_destinations now scans a corpus of all rendered literals and link/image urls and titles and extends the token until absent from it, mirroring the parse-side protect_newline_destinations discipline. Regression test: literal token prose plus a spaced destination round-trips unchanged. --- pkgs/id/src/web/markdown.rs | 15 +++++++++ .../web/markdown/markdown_serialization.rs | 33 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 11810b660b..2fd2ae5f03 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -3155,6 +3155,21 @@ mod tests { assert_eq!(link["attrs"]["href"], "my notes.md", "PM href unmutated"); } + #[test] + fn test_spaced_destination_placeholder_token_in_document_text_survives() { + // #1348: the document's own prose contains the literal + // placeholder token for the first substitution. The save + // direction must not rewrite that prose into the spaced + // destination: the token is chosen absent from the document, + // the literal text round-trips unchanged, and the destination + // still lands in <...> angle form. + let original = + "the token idweb-spaced-destination-0-placeholder is prose\n\n[notes]()\n"; + let (first, second) = two_saves(original); + assert_eq!(first, original, "literal token text unchanged"); + assert_eq!(first, second, "fixpoint"); + } + #[test] fn test_spaced_image_destination_roundtrips_in_angle_form() { // #1019: same preservation for image src. diff --git a/pkgs/id/src/web/markdown/markdown_serialization.rs b/pkgs/id/src/web/markdown/markdown_serialization.rs index 875680ab66..a4335c78f8 100644 --- a/pkgs/id/src/web/markdown/markdown_serialization.rs +++ b/pkgs/id/src/web/markdown/markdown_serialization.rs @@ -1096,6 +1096,31 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { /// so no separate definition path exists in this codec. pub(crate) fn substitute_spaced_destinations<'a>(root: &'a AstNode<'a>) -> Vec<(String, String)> { let mut substitutions: Vec<(String, String)> = Vec::new(); + // #1348: restoration after rendering is a global string replace over + // the whole document, so a placeholder token that already occurs in + // the document would rewrite the document's own text into the spaced + // destination. Gather everything that can reach the rendered output + // (text/code/html literals and every link/image url and title) and + // choose tokens absent from it -- the same collision-safe discipline + // `protect_newline_destinations` uses on the parse side. + let mut corpus = String::new(); + let mut scan: Vec<&AstNode<'a>> = vec![root]; + while let Some(node) = scan.pop() { + scan.extend(node.children()); + let data = node.data.borrow(); + match &data.value { + NodeValue::Text(literal) => corpus.push_str(&literal.to_string()), + NodeValue::Code(code) => corpus.push_str(&code.literal), + NodeValue::CodeBlock(block) => corpus.push_str(&block.literal), + NodeValue::HtmlBlock(block) => corpus.push_str(&block.literal), + NodeValue::HtmlInline(html) => corpus.push_str(html), + NodeValue::Link(link) | NodeValue::Image(link) => { + corpus.push_str(&link.url); + corpus.push_str(&link.title); + } + _ => {} + } + } let mut stack: Vec<&AstNode<'a>> = vec![root]; while let Some(node) = stack.pop() { stack.extend(node.children()); @@ -1113,10 +1138,16 @@ pub(crate) fn substitute_spaced_destinations<'a>(root: &'a AstNode<'a>) -> Vec<( { *url = substitutions[index].0.clone(); } else { - let placeholder = format!( + let mut placeholder = format!( "idweb-spaced-destination-{}-placeholder", substitutions.len() ); + // #1348: extend the token until it is absent from the + // document corpus, so the global restore replace can only + // ever match the destination this substitution planted. + while corpus.contains(&placeholder) { + placeholder.push('x'); + } let raw = std::mem::take(url); *url = placeholder.clone(); substitutions.push((placeholder, raw));