Skip to content

Commit a23ccbb

Browse files
committed
update fence closure to match common mark spec
Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>
1 parent 637b705 commit a23ccbb

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/utils/markdown_repair.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ def close_open_markdown(text: str) -> str:
8686
in_code_fence = True
8787
fence_char = char
8888
fence_len = len(matched_group)
89-
elif char == fence_char and len(matched_group) >= fence_len:
89+
elif (
90+
char == fence_char
91+
and len(matched_group) >= fence_len
92+
and line[fence_match.end() :].strip(" \t") == ""
93+
):
9094
in_code_fence = False
9195
fence_char = ""
9296
fence_len = 0

tests/unit/utils/test_markdown_repair.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ def test_shorter_fence_inside_longer_fence_not_closer(self) -> None:
6060
result = close_open_markdown(text)
6161
assert result == "\n````"
6262

63+
def test_fence_with_trailing_text_not_closer(self) -> None:
64+
"""A fence marker with trailing non-whitespace inside a fence is content."""
65+
text = "```python\nprint('x')\n```not a closer"
66+
result = close_open_markdown(text)
67+
assert result == "\n```"
68+
69+
def test_fence_with_trailing_whitespace_is_closer(self) -> None:
70+
"""A fence marker with only trailing whitespace is a valid closer."""
71+
text = "```python\nprint('x')\n``` "
72+
result = close_open_markdown(text)
73+
assert result == ""
74+
6375

6476
class TestCloseOpenMarkdownHtmlTags:
6577
"""Tests for closing unclosed HTML block tags."""

0 commit comments

Comments
 (0)