From 07ca658a5a8303a93c6a2252836af9d90066a428 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 07:03:09 -0600 Subject: [PATCH 1/4] =?UTF-8?q?fix(id/web):=20strip=5Fleading=5Fblank=5Fqu?= =?UTF-8?q?ote=5Flines=20tracks=20fences=20=E2=80=94=20stop=20eating=20a?= =?UTF-8?q?=20fenced=20code=20block=20first=20line=20(#8665)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/id/src/web/markdown.rs | 45 +++++++++++++++++++ .../web/markdown/markdown_serialization.rs | 44 ++++++++++++++++-- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index a35e201f4a..2a1962d09b 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -1891,6 +1891,51 @@ mod tests { ); } + #[test] + fn test_top_level_fence_with_quote_like_content_round_trips() { + // #8665: strip_leading_blank_quote_lines had no fence state, so + // a top-level fenced block whose first content lines are `> ` + // and `> text` lost its first line on save — the fence opener + // reset the quote-run scan and the literal code content was + // stripped as if it led a blockquote. + for original in [ + "```\n> \n> text\n```\n", + "```rust\n> \n> text\n```\n", + "```\n> \n> text\n```\n\n> quote\n", + ] { + let saved = prosemirror_to_markdown(&markdown_to_prosemirror(original)).unwrap(); + assert_eq!(saved, original, "first save polluted {original:?}"); + assert_eq!( + markdown_to_prosemirror(&saved), + markdown_to_prosemirror(original), + "structure changed for {original:?}" + ); + let resaved = prosemirror_to_markdown(&markdown_to_prosemirror(&saved)).unwrap(); + assert_eq!(resaved, saved, "no fixpoint for {original:?}"); + } + // Control: quote-like lines inside a blockquote's own fence are + // content too — structure and fixpoint must hold. + let quoted = "> ```\n> \n> text\n> ```\n"; + let saved = prosemirror_to_markdown(&markdown_to_prosemirror(quoted)).unwrap(); + assert_eq!( + markdown_to_prosemirror(&saved), + markdown_to_prosemirror(quoted), + "quoted fence structure changed: {saved:?}" + ); + let resaved = prosemirror_to_markdown(&markdown_to_prosemirror(&saved)).unwrap(); + assert_eq!(resaved, saved, "no fixpoint for quoted fence"); + // Tilde fences are canonicalized to backticks by the renderer, + // but their content must survive intact and reach a fixpoint. + let tilde = "~~~\n> \n> text\n~~~\n"; + let saved = prosemirror_to_markdown(&markdown_to_prosemirror(tilde)).unwrap(); + assert_eq!( + saved, "```\n> \n> text\n```\n", + "tilde fence content changed: {saved:?}" + ); + let resaved = prosemirror_to_markdown(&markdown_to_prosemirror(&saved)).unwrap(); + assert_eq!(resaved, saved, "no fixpoint for tilde fence"); + } + #[test] fn test_info_less_fence_in_blockquote_trailing_content_round_trips() { // #1496 acceptance cases: a no-info fence inside a blockquote diff --git a/pkgs/id/src/web/markdown/markdown_serialization.rs b/pkgs/id/src/web/markdown/markdown_serialization.rs index 3f9aba2f1a..27ab32add5 100644 --- a/pkgs/id/src/web/markdown/markdown_serialization.rs +++ b/pkgs/id/src/web/markdown/markdown_serialization.rs @@ -935,9 +935,47 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { let lines: Vec<&str> = output.split('\n').collect(); let mut result: Vec<&str> = Vec::with_capacity(lines.len()); let mut at_quote_start = true; + // #8665: fenced code is literal content. Without fence tracking the + // scan mistook a top-level fence whose first content lines are `> ` + // and `> text` for the start of a quote run — the fence opener is + // neither a blank quote line nor quote content, so it set + // at_quote_start and the stripper deleted the code block's first + // line on save. Track fences exactly as + // restore_github_alert_markers does: a fence is owned by the quote + // depth that opened it, and leaving that container ends it with no + // closing delimiter. Lines inside a fence are never stripped. + let mut fence = FenceTracker::default(); + let mut fence_depth: Option = None; let mut i = 0; while i < lines.len() { - if at_quote_start && is_blank_quote_line(lines[i]) { + let line = lines[i]; + let indent = line.bytes().take_while(|&b| b == b' ').count(); + if indent <= 3 { + let (depth, rest) = split_quote_prefix(&line[indent..]); + if let Some(opened_at) = fence_depth { + if depth < opened_at { + fence.reset(); + fence_depth = None; + } else if let Some(body) = strip_quote_depth(&line[indent..], opened_at) { + fence.feed(fence_feed_line(body)); + if !fence.in_fence() { + fence_depth = None; + } + result.push(line); + at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); + i += 1; + continue; + } + } + if fence_depth.is_none() && fence.feed(fence_feed_line(rest)) { + fence_depth = Some(depth); + result.push(line); + at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); + i += 1; + continue; + } + } + if at_quote_start && fence_depth.is_none() && is_blank_quote_line(line) { let mut j = i + 1; while j < lines.len() && is_blank_quote_line(lines[j]) { j += 1; @@ -947,8 +985,8 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { continue; } } - result.push(lines[i]); - at_quote_start = !is_blank_quote_line(lines[i]) && !is_quote_content_line(lines[i]); + result.push(line); + at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); i += 1; } result.join("\n") From 32004b1840bbdc9395500a65ed9c7b46e7c8ee8b Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 07:28:13 -0600 Subject: [PATCH 2/4] fix(id/web): account for list prefixes when opening a tracked fence (#8665) --- pkgs/id/src/web/markdown.rs | 20 +++++++++++++ .../web/markdown/markdown_serialization.rs | 30 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 2a1962d09b..cb6529df74 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -1936,6 +1936,26 @@ mod tests { assert_eq!(resaved, saved, "no fixpoint for tilde fence"); } + #[test] + fn test_list_fence_does_not_suppress_quote_cleanup() { + // PR #8678 Codex P2: a list item whose first block is fenced + // code renders its opener behind the item marker (- ```), so + // the fence scan missed it and mistook the closing delimiter + // for a new fence; the phantom fence then suppressed the + // #1002 leading-blank cleanup for every later quote in the + // document. The rule-first quote below needs that cleanup. + let original = "- ```\n code\n ```\n\n> -----\n> \n> after\n"; + let saved = prosemirror_to_markdown(&markdown_to_prosemirror(original)).unwrap(); + assert_eq!(saved, original, "first save polluted {original:?}"); + assert_eq!( + markdown_to_prosemirror(&saved), + markdown_to_prosemirror(original), + "structure changed for {original:?}" + ); + let resaved = prosemirror_to_markdown(&markdown_to_prosemirror(&saved)).unwrap(); + assert_eq!(resaved, saved, "no fixpoint for {original:?}"); + } + #[test] fn test_info_less_fence_in_blockquote_trailing_content_round_trips() { // #1496 acceptance cases: a no-info fence inside a blockquote diff --git a/pkgs/id/src/web/markdown/markdown_serialization.rs b/pkgs/id/src/web/markdown/markdown_serialization.rs index 27ab32add5..b2f0e9e92f 100644 --- a/pkgs/id/src/web/markdown/markdown_serialization.rs +++ b/pkgs/id/src/web/markdown/markdown_serialization.rs @@ -931,6 +931,34 @@ fn is_quote_content_line(line: &str) -> bool { /// inside a quote are the required separators and are kept: anything /// after the run's first content line passes through untouched, as does /// an all-blank run (a lone `>` with no quoted content). +/// The body to test for a fence opening: a fenced block that is a +/// list item's first block is rendered with the item marker in front +/// of the opener (`- ``` `), while its closing delimiter carries no +/// marker — so a scan that only looks at the line start misses the +/// opener and later mistakes the closer for a new fence, whose +/// phantom state then suppresses quote cleanup for the rest of the +/// document (Codex P2 on PR #8678). Strip one list marker (bullet or +/// ordered) when present; the caller applies fence_feed_line to the +/// result, so spacing after the marker is handled there. +fn fence_open_body(rest: &str) -> &str { + let body = fence_feed_line(rest); + for marker in ["- ", "* ", "+ "] { + if let Some(after) = body.strip_prefix(marker) { + return after; + } + } + let digits = body.bytes().take_while(|b| b.is_ascii_digit()).count(); + if digits > 0 && digits <= 9 { + if let Some(after) = body[digits..] + .strip_prefix(". ") + .or_else(|| body[digits..].strip_prefix(") ")) + { + return after; + } + } + body +} + fn strip_leading_blank_quote_lines(output: &str) -> String { let lines: Vec<&str> = output.split('\n').collect(); let mut result: Vec<&str> = Vec::with_capacity(lines.len()); @@ -967,7 +995,7 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { continue; } } - if fence_depth.is_none() && fence.feed(fence_feed_line(rest)) { + if fence_depth.is_none() && fence.feed(fence_feed_line(fence_open_body(rest))) { fence_depth = Some(depth); result.push(line); at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); From 7262032fd681fea1bacd84be092c95d354b63f96 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 07:40:51 -0600 Subject: [PATCH 3/4] fix(id/web): strip scan also tracks raw HTML blocks (#8665) --- pkgs/id/src/web/markdown.rs | 19 ++++++ .../web/markdown/markdown_serialization.rs | 62 ++++++++++++++++--- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index cb6529df74..129cf6dfe0 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -1956,6 +1956,25 @@ mod tests { assert_eq!(resaved, saved, "no fixpoint for {original:?}"); } + #[test] + fn test_html_comment_fence_does_not_suppress_quote_cleanup() { + // PR #8678 Codex P2, second round: a fence-shaped line inside + // a raw HTML comment is literal content, not a fence opener. + // Treating it as one opened a phantom fence that suppressed + // the #1002 cleanup for the rest of the document (the + // rule-first quote below needs it). + let original = "\n\n> -----\n> \n> after\n"; + let saved = prosemirror_to_markdown(&markdown_to_prosemirror(original)).unwrap(); + assert_eq!(saved, original, "first save polluted {original:?}"); + assert_eq!( + markdown_to_prosemirror(&saved), + markdown_to_prosemirror(original), + "structure changed for {original:?}" + ); + let resaved = prosemirror_to_markdown(&markdown_to_prosemirror(&saved)).unwrap(); + assert_eq!(resaved, saved, "no fixpoint for {original:?}"); + } + #[test] fn test_info_less_fence_in_blockquote_trailing_content_round_trips() { // #1496 acceptance cases: a no-info fence inside a blockquote diff --git a/pkgs/id/src/web/markdown/markdown_serialization.rs b/pkgs/id/src/web/markdown/markdown_serialization.rs index b2f0e9e92f..2270c43657 100644 --- a/pkgs/id/src/web/markdown/markdown_serialization.rs +++ b/pkgs/id/src/web/markdown/markdown_serialization.rs @@ -963,23 +963,46 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { let lines: Vec<&str> = output.split('\n').collect(); let mut result: Vec<&str> = Vec::with_capacity(lines.len()); let mut at_quote_start = true; - // #8665: fenced code is literal content. Without fence tracking the - // scan mistook a top-level fence whose first content lines are `> ` - // and `> text` for the start of a quote run — the fence opener is - // neither a blank quote line nor quote content, so it set - // at_quote_start and the stripper deleted the code block's first - // line on save. Track fences exactly as - // restore_github_alert_markers does: a fence is owned by the quote - // depth that opened it, and leaving that container ends it with no - // closing delimiter. Lines inside a fence are never stripped. + // #8665: fenced code and raw HTML are literal content. Without + // tracking them the scan mistook a top-level fence whose first + // content lines are `> ` and `> text` for the start of a quote run + // — the fence opener is neither a blank quote line nor quote + // content, so it set at_quote_start and the stripper deleted the + // code block's first line on save; and a fence-shaped line inside + // an HTML comment opened a phantom fence that suppressed quote + // cleanup for the rest of the document. Track both exactly as + // restore_github_alert_markers does: each is owned by the quote + // depth that opened it, and leaving that container ends it with + // no closing delimiter. Lines inside either are never stripped. let mut fence = FenceTracker::default(); let mut fence_depth: Option = None; + let mut html = HtmlBlockTracker::default(); + let mut html_depth: Option = None; + let mut at_block_start = true; let mut i = 0; while i < lines.len() { let line = lines[i]; let indent = line.bytes().take_while(|&b| b == b' ').count(); + let mut block_boundary = line.trim().is_empty(); if indent <= 3 { let (depth, rest) = split_quote_prefix(&line[indent..]); + if let Some(opened_at) = html_depth { + if depth < opened_at { + html.reset(); + html_depth = None; + } else if let Some(body) = strip_quote_depth(&line[indent..], opened_at) { + html.feed(fence_feed_line(body), at_block_start); + if !html.in_html() { + html_depth = None; + block_boundary = true; + } + result.push(line); + at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); + i += 1; + at_block_start = block_boundary; + continue; + } + } if let Some(opened_at) = fence_depth { if depth < opened_at { fence.reset(); @@ -988,22 +1011,40 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { fence.feed(fence_feed_line(body)); if !fence.in_fence() { fence_depth = None; + block_boundary = true; } result.push(line); at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); i += 1; + at_block_start = block_boundary; continue; } } + if html_depth.is_none() + && fence_depth.is_none() + && html.feed(fence_feed_line(fence_open_body(rest)), at_block_start) + { + html_depth = Some(depth); + result.push(line); + at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); + i += 1; + at_block_start = false; + continue; + } if fence_depth.is_none() && fence.feed(fence_feed_line(fence_open_body(rest))) { fence_depth = Some(depth); result.push(line); at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); i += 1; + at_block_start = false; continue; } } - if at_quote_start && fence_depth.is_none() && is_blank_quote_line(line) { + if at_quote_start + && fence_depth.is_none() + && html_depth.is_none() + && is_blank_quote_line(line) + { let mut j = i + 1; while j < lines.len() && is_blank_quote_line(lines[j]) { j += 1; @@ -1016,6 +1057,7 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { result.push(line); at_quote_start = !is_blank_quote_line(line) && !is_quote_content_line(line); i += 1; + at_block_start = block_boundary; } result.join("\n") } From b5ea7aba254e9a58134234bc02517fd4eda0187a Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 07:50:13 -0600 Subject: [PATCH 4/4] fix(id/web): feed active literal blocks at any indent in strip scan (#8665) --- pkgs/id/src/web/markdown.rs | 21 +++++++++++++++++++ .../web/markdown/markdown_serialization.rs | 17 +++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 129cf6dfe0..4050926067 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -1975,6 +1975,27 @@ mod tests { assert_eq!(resaved, saved, "no fixpoint for {original:?}"); } + #[test] + fn test_indented_html_comment_terminator_releases_cleanup() { + // PR #8678 Codex P2, third round: the htmlBlock serialization + // path preserves indentation, so a comment terminator can be + // indented four spaces. The scan's indent limit applies to + // opening detection, not to an already-active block: the + // terminator must still be fed, or html state leaks into the + // rest of the document and suppresses the #1002 cleanup for + // the rule-first quote below. + let original = "\n\n> -----\n> \n> after\n"; + let saved = prosemirror_to_markdown(&markdown_to_prosemirror(original)).unwrap(); + assert_eq!(saved, original, "first save polluted {original:?}"); + assert_eq!( + markdown_to_prosemirror(&saved), + markdown_to_prosemirror(original), + "structure changed for {original:?}" + ); + let resaved = prosemirror_to_markdown(&markdown_to_prosemirror(&saved)).unwrap(); + assert_eq!(resaved, saved, "no fixpoint for {original:?}"); + } + #[test] fn test_info_less_fence_in_blockquote_trailing_content_round_trips() { // #1496 acceptance cases: a no-info fence inside a blockquote diff --git a/pkgs/id/src/web/markdown/markdown_serialization.rs b/pkgs/id/src/web/markdown/markdown_serialization.rs index 2270c43657..647b32abc7 100644 --- a/pkgs/id/src/web/markdown/markdown_serialization.rs +++ b/pkgs/id/src/web/markdown/markdown_serialization.rs @@ -974,6 +974,11 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { // restore_github_alert_markers does: each is owned by the quote // depth that opened it, and leaving that container ends it with // no closing delimiter. Lines inside either are never stripped. + // The indentation restriction applies to OPENING detection only: + // an already-active block's lines are fed however deeply they are + // indented, because the htmlBlock path preserves indentation and + // an indented terminator still ends the block (Codex P2s on + // PR #8678). let mut fence = FenceTracker::default(); let mut fence_depth: Option = None; let mut html = HtmlBlockTracker::default(); @@ -984,13 +989,14 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { let line = lines[i]; let indent = line.bytes().take_while(|&b| b == b' ').count(); let mut block_boundary = line.trim().is_empty(); - if indent <= 3 { - let (depth, rest) = split_quote_prefix(&line[indent..]); + if html_depth.is_some() || fence_depth.is_some() { + let trimmed = line.trim_start_matches(' '); + let (depth, _) = split_quote_prefix(trimmed); if let Some(opened_at) = html_depth { if depth < opened_at { html.reset(); html_depth = None; - } else if let Some(body) = strip_quote_depth(&line[indent..], opened_at) { + } else if let Some(body) = strip_quote_depth(trimmed, opened_at) { html.feed(fence_feed_line(body), at_block_start); if !html.in_html() { html_depth = None; @@ -1007,7 +1013,7 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { if depth < opened_at { fence.reset(); fence_depth = None; - } else if let Some(body) = strip_quote_depth(&line[indent..], opened_at) { + } else if let Some(body) = strip_quote_depth(trimmed, opened_at) { fence.feed(fence_feed_line(body)); if !fence.in_fence() { fence_depth = None; @@ -1020,6 +1026,9 @@ fn strip_leading_blank_quote_lines(output: &str) -> String { continue; } } + } + if indent <= 3 { + let (depth, rest) = split_quote_prefix(&line[indent..]); if html_depth.is_none() && fence_depth.is_none() && html.feed(fence_feed_line(fence_open_body(rest)), at_block_start)