File size: 788 Bytes
b453cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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()