diff --git a/src/specify_cli/markdown_sections.py b/src/specify_cli/markdown_sections.py index 29d61fc7db..5d46a71fbf 100644 --- a/src/specify_cli/markdown_sections.py +++ b/src/specify_cli/markdown_sections.py @@ -133,6 +133,7 @@ def replace_section( headings: list[tuple[int, str, int, int, int]] = [] fence_marker = "" fence_length = 0 + in_comment = False metadata = content.startswith(("---\n", "---\r\n")) offset = 0 for number, line in enumerate( @@ -150,6 +151,15 @@ def replace_section( if closing and closing[1][0] == fence_marker and len(closing[1]) >= fence_length: fence_marker = "" continue + if in_comment: + in_comment = "-->" not in text + continue + # CommonMark HTML block, start condition 2: an HTML comment runs from + # a line starting with "", + # which may be the opening line itself. + if re.match(r" {0,3}" not in text[text.index("", + " ", + "", + ], +) +def test_commented_headings_do_not_end_section(comment: str) -> None: + assert replace_section( + f"## Target\nold\n{comment}\nmore\n## Next\nkeep\n", "new\n", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) == "## Target\nnew\n## Next\nkeep\n" + + +def test_commented_heading_does_not_match_selector() -> None: + assert replace_section( + "\n## Target\nold\n", "new\n", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) == "\n## Target\nnew\n" + + +def test_heading_after_comment_close_ends_section() -> None: + assert replace_section( + "## Target\nold\n\n## Next\nkeep\n", "new\n", "target", + preset="team", contribution="body.md", artifact="spec-template", + ) == "## Target\nnew\n## Next\nkeep\n" + + @pytest.mark.parametrize( "content,code,count,lines", [ @@ -174,6 +203,17 @@ def test_fenced_headings_neither_match_nor_end_section(fence: str, close: str) - (" ## Target\nnot ATX\n", "SECTION_NOT_FOUND", 0, []), ("##Target\nnot ATX\n", "SECTION_NOT_FOUND", 0, []), ("Target\n------\n", "SECTION_NOT_FOUND", 0, []), + ("\n## Target\n\n## Target\n", + "SECTION_AMBIGUOUS", 2, [4, 6], + ), + ("\n## Target\n\n## Target\n", "SECTION_AMBIGUOUS", 2, [2, 4]), + ( + " \n## Target\n", + "SECTION_AMBIGUOUS", 2, [2, 6], + ), + ("text