From 79de1afde2f14d1b8cb96fd5ecc54cb2edf56610 Mon Sep 17 00:00:00 2001 From: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:35:56 -0800 Subject: [PATCH] fix(presets): split replace-section lines only at Markdown line endings replace_section scanned the document with str.splitlines(), which also breaks lines at \v, \f, \x1c-\x1e, \x85, U+2028 and U+2029. Markdown only ends lines at LF, CR and CRLF, so text after one of those characters was scanned as a new line: a "## ..." after it became a fake heading that ended the selected body early and left part of the old body in the composed template, a ``` after it opened a fake fence that hid every later heading, and diagnostic line numbers were too high. Split only at "\r\n", "\r" and "\n", keeping the line endings, so the offsets and everything else stay unchanged. Assisted-by: OpenAI Codex CLI (model: gpt-5.6-sol, autonomous) Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) --- src/specify_cli/markdown_sections.py | 4 ++- tests/test_markdown_sections.py | 52 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/specify_cli/markdown_sections.py b/src/specify_cli/markdown_sections.py index c543cbbb88..34a61fd44a 100644 --- a/src/specify_cli/markdown_sections.py +++ b/src/specify_cli/markdown_sections.py @@ -135,7 +135,9 @@ def replace_section( fence_length = 0 metadata = content.startswith(("---\n", "---\r\n")) offset = 0 - for number, line in enumerate(content.splitlines(keepends=True), 1): + for number, line in enumerate( + re.findall(r"[^\r\n]*(?:\r\n|\r|\n)|[^\r\n]+", content), 1 + ): start = offset offset += len(line) text = line.rstrip("\r\n") diff --git a/tests/test_markdown_sections.py b/tests/test_markdown_sections.py index 285a018d2d..99bd4c82b8 100644 --- a/tests/test_markdown_sections.py +++ b/tests/test_markdown_sections.py @@ -138,6 +138,58 @@ def test_unclosed_fence_cannot_end_selected_body() -> None: ) == "## Target\n" +# Characters str.splitlines() treats as line breaks that Markdown does not. +NON_MARKDOWN_LINE_SEPARATORS = ["\v", "\f", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"] + + +@pytest.mark.parametrize("separator", NON_MARKDOWN_LINE_SEPARATORS) +def test_non_markdown_line_separators_do_not_end_selected_body(separator: str) -> None: + content = f"## Notes\nSee page{separator}## Fake\nmore\n## Next\nkeep\n" + assert replace_section( + content, "new\n", "notes", + preset="team", contribution="body.md", artifact="spec-template", + ) == "## Notes\nnew\n## Next\nkeep\n" + + +@pytest.mark.parametrize("separator", NON_MARKDOWN_LINE_SEPARATORS) +def test_non_markdown_line_separators_do_not_open_fences(separator: str) -> None: + content = f"# Notes\ntext{separator}```\n## Target\nold\n## Next\nkeep\n" + assert replace_section( + content, "new\n", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) == f"# Notes\ntext{separator}```\n## Target\nnew\n## Next\nkeep\n" + + +@pytest.mark.parametrize("separator", NON_MARKDOWN_LINE_SEPARATORS) +def test_non_markdown_line_separators_do_not_create_headings(separator: str) -> None: + content = f"## Notes\nSee page{separator}## Fake\nmore\n## Next\nkeep\n" + with pytest.raises(SectionReplacementError, match="SECTION_NOT_FOUND"): + replace_section( + content, "new\n", "fake", + preset="team", contribution="body.md", artifact="spec-template", + ) + + +def test_non_markdown_line_separator_does_not_shift_diagnostic_lines() -> None: + content = "intro\u2028still intro\n## Target\nold\n## Target\nagain\n" + with pytest.raises(SectionReplacementError) as caught: + replace_section( + content, "new\n", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) + assert caught.value.details["code"] == "SECTION_AMBIGUOUS" + assert [candidate["line"] for candidate in caught.value.details["candidates"]] == [2, 4] + + +@pytest.mark.parametrize("newline", ["\r", "\r\n"]) +def test_carriage_return_line_endings_remain_supported(newline: str) -> None: + content = f"## Notes{newline}old{newline}## Next{newline}keep" + assert replace_section( + content, f"new{newline}", "notes", + preset="team", contribution="body.md", artifact="spec-template", + ) == f"## Notes{newline}new{newline}## Next{newline}keep" + + def test_code_underscores_do_not_match_emphasis_anchor() -> None: with pytest.raises(SectionReplacementError, match="SECTION_NOT_FOUND"): replace_section(