ling-series-spaces / code_kit /code_trimmer.py
GitHub Action
Sync ling-space changes from GitHub commit 225a47b
b453cca
raw
history blame
788 Bytes
import re
def trim_html_markdown_blocks(code_string: str) -> str:
"""
Trims Markdown HTML code block delimiters (```html and ```) from the start and end of a string.
Also removes any leading/trailing whitespace/newlines.
"""
if not code_string:
return ""
# Pattern to match ```html or ``` followed by optional whitespace/newlines
# and capture the actual code content.
# It attempts to match the opening ```html block
# and the closing ``` block at the very beginning and end of the string.
# Trim leading ```html or ```
trimmed_code = re.sub(r"^\s*```(html)?\s*\n", "", code_string, flags=re.IGNORECASE)
# Trim trailing ```
trimmed_code = re.sub(r"\n\s*```\s*$", "", trimmed_code)
return trimmed_code.strip()