From b9d7664598f0a14c993e4d5e37a549e85dbb8385 Mon Sep 17 00:00:00 2001 From: wangshun <96341168+xixi-box@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:21:23 +0000 Subject: [PATCH] Ignore HTML comment blocks during Markdown section replacement Assisted-by: Codex (model: gpt-6.2-sol, autonomous) Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com> --- src/specify_cli/markdown_sections.py | 4 +++ tests/test_markdown_sections.py | 28 ++++++++++++++++++++ tests/test_resolve_template_python_parity.py | 10 +++++++ 3 files changed, 42 insertions(+) diff --git a/src/specify_cli/markdown_sections.py b/src/specify_cli/markdown_sections.py index 29d61fc7db..dbe8ec4388 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_html_comment = False metadata = content.startswith(("---\n", "---\r\n")) offset = 0 for number, line in enumerate( @@ -150,6 +151,9 @@ def replace_section( if closing and closing[1][0] == fence_marker and len(closing[1]) >= fence_length: fence_marker = "" continue + if in_html_comment or re.match(r" {0,3}" not in text + continue opening = re.match(r" {0,3}(`{3,}|~{3,})(.*)$", text) if opening and not (opening[1][0] == "`" and "`" in opening[2]): fence_marker, fence_length = opening[1][0], len(opening[1]) diff --git a/tests/test_markdown_sections.py b/tests/test_markdown_sections.py index 1ef5ce306c..68c2d98b1c 100644 --- a/tests/test_markdown_sections.py +++ b/tests/test_markdown_sections.py @@ -162,6 +162,34 @@ def test_fenced_headings_neither_match_nor_end_section(fence: str, close: str) - ) == f"# Doc\n{fence}\n## Target\n{close}\n## Target\nnew\n## Next\nkeep\n" +@pytest.mark.parametrize( + "prefix", + [ + " trailing text\n", + "\n", + "```md\n\nmore\n## Next\nkeep\n", + "target", "new\n", "## Target\nnew\n## Next\nkeep\n", None, + id="html_comment_section_boundary", + ), + pytest.param( + "\r\n## Target\r\nold\r\n", + "target", "new\r\n", "\r\n## Target\r\nnew\r\n", None, + id="html_comment_duplicate_anchor", + ), ("---\ndescription: |\n ## Target\n---\n## Target\nold", "target", "new", "---\ndescription: |\n ## Target\n---\n## Target\nnew", None), ("# Missing\n", "target", "", None, "SECTION_NOT_FOUND"), ("## Target\n### TARGET!\n", "target", "", None, "SECTION_AMBIGUOUS"),