From 9554ecbcbf933354a0b0e73111b0f8aab8bfa326 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:34:46 +0800 Subject: [PATCH 1/2] fix(presets): tolerate trailing whitespace on the front matter end replace_section ended front matter only at a closing line that was exactly "---". A closing delimiter followed by a space or a tab, which is common in hand-edited files, was never recognised: every heading stayed hidden as metadata and every selector failed with SECTION_NOT_FOUND, a diagnostic that points at the selector rather than at the stray character. Accept trailing spaces and tabs on the closing line only, the same way the scanner already accepts them on ATX headings and fence closers. The opening delimiter is unchanged. Add a parametrized regression test covering the exact, space-padded and tab-padded closing delimiters. The padded rows fail on main with SECTION_NOT_FOUND and pass with this change. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- src/specify_cli/markdown_sections.py | 2 +- tests/test_markdown_sections.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/specify_cli/markdown_sections.py b/src/specify_cli/markdown_sections.py index 34a61fd44a..41cfd163b8 100644 --- a/src/specify_cli/markdown_sections.py +++ b/src/specify_cli/markdown_sections.py @@ -142,7 +142,7 @@ def replace_section( offset += len(line) text = line.rstrip("\r\n") if metadata: - if number > 1 and text == "---": + if number > 1 and re.fullmatch(r"---[ \t]*", text): metadata = False continue if fence_marker: diff --git a/tests/test_markdown_sections.py b/tests/test_markdown_sections.py index 99bd4c82b8..9a3eff2531 100644 --- a/tests/test_markdown_sections.py +++ b/tests/test_markdown_sections.py @@ -58,6 +58,16 @@ def test_preserves_everything_outside_body( ) == expected +@pytest.mark.parametrize("closing", ["---", "--- ", "---\t"]) +def test_front_matter_end_with_trailing_whitespace(closing: str) -> None: + front = "---\ndescription: |\n ## Target\n" + closing + "\n" + body = "\n# Doc\n## Target\nold\n## Next\nkeep\n" + assert replace_section( + front + body, "new\n", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) == front + "\n# Doc\n## Target\nnew\n## Next\nkeep\n" + + @pytest.mark.parametrize( "content,replacement,expected", [ From 983fb7e13df7b11a2ff031ddd7c72aeb4e80cb6b Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:48:25 +0800 Subject: [PATCH 2/2] test(presets): keep non-delimiter lines in front matter Add a parametrized negative case asserting that "---- " and "--- text" on the closing line do not end the front matter and still fail the selector with SECTION_NOT_FOUND. Both rows fail against a "---" prefix match. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- tests/test_markdown_sections.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_markdown_sections.py b/tests/test_markdown_sections.py index 9a3eff2531..e1d5a6b743 100644 --- a/tests/test_markdown_sections.py +++ b/tests/test_markdown_sections.py @@ -68,6 +68,21 @@ def test_front_matter_end_with_trailing_whitespace(closing: str) -> None: ) == front + "\n# Doc\n## Target\nnew\n## Next\nkeep\n" +@pytest.mark.parametrize("closing", ["---- ", "--- text"]) +def test_front_matter_end_rejects_non_delimiter_lines(closing: str) -> None: + front = "---\ndescription: |\n ## Target\n" + closing + "\n" + body = "\n# Doc\n## Target\nold\n## Next\nkeep\n" + with pytest.raises(SectionReplacementError) as caught: + replace_section( + front + body, "new\n", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) + details = caught.value.details + assert details["code"] == "SECTION_NOT_FOUND" + assert details["match_count"] == 0 + assert details["candidates"] == [] + + @pytest.mark.parametrize( "content,replacement,expected", [