mirror of
https://github.com/open-webui/open-webui.git
synced 2026-02-24 20:19:42 +01:00
22 lines
523 B
Python
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()
|