From 4074ea35d7de3c51a0d40f8b58ac19998094545c Mon Sep 17 00:00:00 2001 From: Winget Software <173072168+Winget-Software@users.noreply.github.com> Date: Thu, 26 Nov 2026 05:54:43 +0000 Subject: [PATCH] fix(presets): keep the next heading on its own line after replace-section When a replace-section contribution did not end with a line ending and the selected section was followed by another heading, the replacement was joined directly to that heading ("new body# Second"). The heading was no longer an ATX heading, so a later replace-section that targeted it failed with SECTION_NOT_FOUND and the composed template lost it. Append the line ending that already precedes the following heading (\n, \r\n or \r) when the replacement does not end with one. A replacement at the end of the document is left unchanged. Fixes #5585 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VcnG2kiF8RiLc8o3w5L5pe --- src/specify_cli/markdown_sections.py | 8 ++++++ tests/test_markdown_sections.py | 39 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/specify_cli/markdown_sections.py b/src/specify_cli/markdown_sections.py index 34a61fd44a..6600f599d6 100644 --- a/src/specify_cli/markdown_sections.py +++ b/src/specify_cli/markdown_sections.py @@ -193,6 +193,14 @@ def replace_section( last_newline = content.rfind("\n") separator = "\r\n" if last_newline > 0 and content[last_newline - 1] == "\r" else "\n" replacement = separator + replacement + if ( + replacement + and body_end < len(content) + and not replacement.endswith(("\n", "\r")) + ): + # Keep the following heading on its own line, using the line ending + # that already precedes it in the target. + replacement += "\r\n" if content.endswith("\r\n", 0, body_end) else content[body_end - 1] return content[:body_start] + replacement + content[body_end:] diff --git a/tests/test_markdown_sections.py b/tests/test_markdown_sections.py index 99bd4c82b8..33621edb6e 100644 --- a/tests/test_markdown_sections.py +++ b/tests/test_markdown_sections.py @@ -86,6 +86,45 @@ def test_unterminated_heading_remains_selectable_for_later_replacements(): ) == "## Target\nsecond\n" +@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"]) +@pytest.mark.parametrize( + "content,selector,expected", + [ + ("# First{n}old{n}# Second{n}keep{n}", "first", "# First{n}new body{n}# Second{n}keep{n}"), + ("# Doc{n}## Target{n}old{n}# Next{n}keep{n}", "target", "# Doc{n}## Target{n}new body{n}# Next{n}keep{n}"), + ("## Target{n}## Next{n}keep{n}", "target", "## Target{n}new body{n}## Next{n}keep{n}"), + ("## Target{n}old{n}### Child{n}nested{n}## Next{n}keep", "target", "## Target{n}new body{n}## Next{n}keep"), + ], +) +def test_replacement_without_trailing_newline_keeps_next_heading_on_its_own_line( + newline: str, content: str, selector: str, expected: str, +) -> None: + result = replace_section( + content.format(n=newline), "new body", selector, + preset="team", contribution="body.md", artifact="spec-template", + ) + assert result == expected.format(n=newline) + + +@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"]) +def test_heading_after_unterminated_replacement_remains_selectable(newline: str) -> None: + first = replace_section( + f"# First{newline}old{newline}# Second{newline}keep{newline}", "new body", "first", + preset="first", contribution="first.md", artifact="spec-template", + ) + assert replace_section( + first, f"changed{newline}", "second", + preset="second", contribution="second.md", artifact="spec-template", + ) == f"# First{newline}new body{newline}# Second{newline}changed{newline}" + + +def test_unterminated_replacement_at_end_of_document_is_unchanged() -> None: + assert replace_section( + "# Doc\n## Target\nold\n", "new body", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) == "# Doc\n## Target\nnew body" + + @pytest.mark.parametrize( "fence,close", [("```md", "```"), ("~~~~ text", "~~~~~"), (" ```", " ````")],