Files
Timothy Jaeryang Baek 4a55167759 refac
2026-01-26 18:04:58 +04:00

22 lines
523 B
Python

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()