@@ -4529,8 +4529,9 @@ fn list_kind(node: &Value) -> Option<&str> { |
4529 | 4529 | /// the output is returned untouched. |
4530 | 4530 | fn strip_end_list_separators(doc: &Value, output: &str) -> String { |
4531 | 4531 | // Recompute with kinds: the drop decision needs them, so collect |
4532 | | - // (prev_kind, kind, tight_pair, merge_safe) tuples alongside. |
4533 | | - let mut kinds: Vec<(String, String, bool, bool)> = Vec::new(); |
| 4532 | + // (prev_kind, kind, tight_pair, merge_safe, needs_blank) tuples |
| 4533 | + // alongside. |
| 4534 | + let mut kinds: Vec<(String, String, bool, bool, bool)> = Vec::new(); |
4534 | 4535 | collect_boundary_kinds(doc, &mut kinds); |
4535 | 4536 | if kinds.is_empty() { |
4536 | 4537 | return output.to_string(); |
@@ -4581,16 +4582,21 @@ fn strip_end_list_separators(doc: &Value, output: &str) -> String { |
4581 | 4582 | let line = lines[idx]; |
4582 | 4583 | if is_separator[idx] { |
4583 | 4584 | match boundary.next() { |
4584 | | - Some((a, b, tight_pair, merge_safe)) if a != b && *merge_safe => { |
| 4585 | + Some((a, b, tight_pair, merge_safe, needs_blank)) if a != b && *merge_safe => { |
4585 | 4586 | // Drop the separator. The renderer also puts a blank |
4586 | 4587 | // line on each side of it; with the comment gone, |
4587 | 4588 | // blanks between the lists would fuse them into ONE |
4588 | 4589 | // loose list on reparse. A tight pair keeps no blank |
4589 | | - // line at all; a loose pair keeps exactly one. |
| 4590 | + // line at all; a loose pair keeps exactly one. A |
| 4591 | + // needs_blank pair also keeps exactly one: an ordered |
| 4592 | + // list starting above 1 cannot interrupt the previous |
| 4593 | + // item's paragraph, so with no blank line at all its |
| 4594 | + // first line reparses as a lazy continuation and the |
| 4595 | + // list is lost (Codex review, PR #3258). |
4590 | 4596 | let prev_blank = kept.last().is_some_and(|l| is_blank_line(l)); |
4591 | 4597 | let next_blank = lines.get(idx + 1).is_some_and(|l| is_blank_line(l)); |
4592 | 4598 | if prev_blank && next_blank { |
4593 | | - if *tight_pair { |
| 4599 | + if *tight_pair && !*needs_blank { |
4594 | 4600 | kept.pop(); |
4595 | 4601 | } |
4596 | 4602 | idx += 1; // skip the following blank line too |
@@ -4610,14 +4616,14 @@ fn strip_end_list_separators(doc: &Value, output: &str) -> String { |
4610 | 4616 | out |
4611 | 4617 | } |
4612 | 4618 |
|
4613 | | -/// (prev PM kind, PM kind, both tight, merge-safe) for every list |
| 4619 | +/// (prev PM kind, PM kind, both tight, merge-safe, needs-blank) for every list |
4614 | 4620 | /// adjacency in the doc, in render order, interleaved with authored |
4615 | | -/// htmlBlocks (recorded as ("", "", false, false) so they consume a |
| 4621 | +/// htmlBlocks (recorded as ("", "", false, false, false) so they consume a |
4616 | 4622 | /// separator-line slot but are never dropped - see |
4617 | 4623 | /// strip_end_list_separators' caller contract). Merge-safe is false |
4618 | 4624 | /// only when dropping the separator would merge two ordered-rendering |
4619 | 4625 | /// lists whose numbering does not continue across the boundary. |
4620 | | -fn collect_boundary_kinds(node: &Value, out: &mut Vec<(String, String, bool, bool)>) { |
| 4626 | +fn collect_boundary_kinds(node: &Value, out: &mut Vec<(String, String, bool, bool, bool)>) { |
4621 | 4627 | let Some(content) = node["content"].as_array() else { |
4622 | 4628 | return; |
4623 | 4629 | }; |
@@ -4657,11 +4663,19 @@ fn collect_boundary_kinds(node: &Value, out: &mut Vec<(String, String, bool, boo |
4657 | 4663 | let tight_merge_safe = |
4658 | 4664 | !(renders_ordered(prev) == renders_ordered(child) && tight_differs); |
4659 | 4665 | let merge_safe = ordered_merge_safe && tight_merge_safe; |
| 4666 | + // Codex review, PR #3258: an ordered-rendering list that |
| 4667 | + // starts above 1 cannot interrupt a paragraph, so when the |
| 4668 | + // previous list does not itself render ordered, the boundary |
| 4669 | + // must keep a blank line or the second list's first line |
| 4670 | + // reparses as a lazy continuation of the previous item. |
| 4671 | + let needs_blank = matches!(ordered_start(child), Some(start) if start != 1) |
| 4672 | + && ordered_start(prev).is_none(); |
4660 | 4673 | out.push(( |
4661 | 4674 | list_kind(prev).unwrap_or_default().to_string(), |
4662 | 4675 | kind.to_string(), |
4663 | 4676 | tight_pair, |
4664 | 4677 | merge_safe, |
| 4678 | + needs_blank, |
4665 | 4679 | )); |
4666 | 4680 | } |
4667 | 4681 | if child["type"].as_str() == Some("code_block") |
@@ -4675,7 +4689,7 @@ fn collect_boundary_kinds(node: &Value, out: &mut Vec<(String, String, bool, boo |
4675 | 4689 | == "<!-- end list -->" |
4676 | 4690 | }) |
4677 | 4691 | { |
4678 | | - out.push((String::new(), String::new(), false, false)); |
| 4692 | + out.push((String::new(), String::new(), false, false, false)); |
4679 | 4693 | } |
4680 | 4694 | // Codex review, PR #3258: lists inside a table cell are |
4681 | 4695 | // flattened to text by append_table_cell_child and emit no |
@@ -6973,6 +6987,34 @@ mod tests { |
6973 | 6987 | assert!(!out.contains("end list"), "real separator dropped: {out:?}"); |
6974 | 6988 | } |
6975 | 6989 |
|
| 6990 | + #[test] |
| 6991 | + fn test_adjacent_bullet_to_ordered_starting_above_one_keeps_blank() { |
| 6992 | + // Codex review on PR #3258 (P2): a tight bullet list followed by |
| 6993 | + // a tight ordered list starting above 1 saved as `- a\n2. b` - |
| 6994 | + // an ordered marker above 1 cannot interrupt a paragraph, so on |
| 6995 | + // reparse `2. b` became a lazy continuation of the bullet item |
| 6996 | + // and the ordered list (and its start) was lost. The boundary |
| 6997 | + // keeps exactly one blank line instead of none. |
| 6998 | + for original in ["- a\n\n2. b\n", "- [ ] a\n\n3. b\n"] { |
| 6999 | + let (first, second) = two_saves(original); |
| 7000 | + assert_eq!(first, original, "blank kept, start preserved: {first:?}"); |
| 7001 | + assert!( |
| 7002 | + !first.contains("end list"), |
| 7003 | + "no end-list artefact for {original:?}: {first:?}" |
| 7004 | + ); |
| 7005 | + assert_eq!(first, second, "fixpoint for {original:?}"); |
| 7006 | + assert_eq!( |
| 7007 | + markdown_to_prosemirror(&first), |
| 7008 | + markdown_to_prosemirror(original), |
| 7009 | + "reparse yields the same two lists for {original:?}" |
| 7010 | + ); |
| 7011 | + let doc = markdown_to_prosemirror(&first); |
| 7012 | + let content = doc["content"].as_array().unwrap(); |
| 7013 | + assert_eq!(content.len(), 2, "two lists for {original:?}"); |
| 7014 | + assert_eq!(content[1]["type"], "ordered_list", "second list stays ordered for {original:?}"); |
| 7015 | + } |
| 7016 | + } |
| 7017 | + |
6976 | 7018 | #[test] |
6977 | 7019 | fn test_adjacent_different_kind_lists_in_blockquote_fixpoint() { |
6978 | 7020 | // #1397: the blockquote shape was not even a save fixpoint. |
|