import re import cross MFM_PATTERN = re.compile(r"\$\[([^\[\]]+)\]") def strip_mfm(tokens: list[cross.Token]) -> tuple[list[cross.Token], bool]: modified = False for tk in tokens: if isinstance(tk, cross.TextToken): original = tk.text cleaned = __strip_mfm(original) if cleaned != original: modified = True tk.text = cleaned elif isinstance(tk, cross.LinkToken): original = tk.label cleaned = __strip_mfm(original) if cleaned != original: modified = True tk.label = cleaned return tokens, modified def __strip_mfm(text: str) -> str: def match_contents(match: re.Match[str]): content = match.group(1).strip() parts = content.split(" ", 1) return parts[1] if len(parts) > 1 else "" while MFM_PATTERN.search(text): text = MFM_PATTERN.sub(match_contents, text) return text