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..e1d5a6b743 100644 --- a/tests/test_markdown_sections.py +++ b/tests/test_markdown_sections.py @@ -58,6 +58,31 @@ 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("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", [