INNER CODE UNIT · Python

escape_telegram_html

sameerasw/essentials · .github/scripts/clean_release_notes.py:5

def escape_telegram_html(text: str) -> str:
    text = re.sub(r"&(?!amp;|lt;|gt;|quot;|#\d+;|#x[0-9a-fA-F]+;)", "&", text)
    valid_tag_pattern = r"(</?(?:a|b|i|s|u|code|pre|blockquote)(?:\s+href=\"[^\"]*\")?\s*>)"
    parts = re.split(valid_tag_pattern, text, flags=re.IGNORECASE)
    for i in range(len(parts)):
        if not re.match(valid_tag_pattern, parts[i], flags=re.IGNORECASE):
            parts[i] = parts[i].replace("<", "&lt;").replace(">", "&gt;")
    return "".join(parts)

def clean_release_notes(raw: str) -> str:
    text = re.sub(r"<img\b[^>]*\/?>", "", raw, flags=re.IGNORECASE)
    text = re.sub(r"<a\b[^>]*>\s*</a>", "", text, flags=re.IGNORECASE)
    text = re.sub(r"^[-\*_]{3,}\s*$", "", text, flags=re.MULTILINE)
    text = re.sub(r"</?(?:details|summary|h[1-6]|p|div|span|align)\b[^>]*>", "", text, flags=re.IGNORECASE)
    text = re.sub(r"!\[.*?\]\(.*?\)", "", text)

    def convert_md_link(match):
        return f'<a href="{match.group(2)}">{match.group(1)}</a>'

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…