From 2418b695e437ef39df396f1c9de8202902a0c1ae Mon Sep 17 00:00:00 2001 From: Hermeto Romano Date: Wed, 2 Dec 2026 18:35:04 -0500 Subject: [PATCH] fix(presets): skip HTML comments when scanning section headings replace-section treated an ATX heading inside an HTML comment as a real heading, so a commented example cut the selected section short and exposed the rest of the comment, or made the selector ambiguous. Track CommonMark HTML comment blocks the same way fenced code is tracked. Fixes #5682 Assisted-by: Claude Code (model: Claude Opus 5.5, supervised) --- src/specify_cli/markdown_sections.py | 10 +++++++ tests/test_markdown_sections.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) 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