import re def strip_markdown_for_twitch(markdown: str) -> str: text = markdown text = re.sub(r"!\[[^\]]*\]\([^)]+\)", "", text) text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text) text = re.sub(r"\*\*([^*]+)\*\*", r"\1", text) text = re.sub(r"__([^_]+)__", r"\1", text) text = re.sub(r"(? list[str]: cleaned = strip_markdown_for_twitch(text) if len(cleaned) <= limit: return [cleaned] if cleaned else [] chunks = [] remaining = cleaned while len(remaining) > limit: window = remaining[:limit] last_space = window.rfind(" ") if last_space == -1: chunks.append(window) remaining = remaining[limit:] else: chunks.append(window[:last_space]) remaining = remaining[last_space + 1 :] if remaining: chunks.append(remaining) return chunks