This commit is contained in:
Timothy Jaeryang Baek
2026-01-26 18:04:58 +04:00
parent c5c4aef7b1
commit 4a55167759
2 changed files with 167 additions and 86 deletions
+21
View File
@@ -0,0 +1,21 @@
import re
def strip_markdown_code_fences(code: str) -> str:
"""
Strip markdown code fences if present.
This is a defensive, non-breaking change — if the code doesn't
contain fences, it passes through unchanged.
Handles patterns like:
- ```python
- ```py
- ```
"""
code = code.strip()
# Remove opening fence (```python, ```py, ``` etc.)
code = re.sub(r"^```\w*\n?", "", code)
# Remove closing fence
code = re.sub(r"\n?```\s*$", "", code)
return code.strip()