social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1import re
2
3import cross
4
5MFM_PATTERN = re.compile(r"\$\[([^\[\]]+)\]")
6
7
8def strip_mfm(tokens: list[cross.Token]) -> tuple[list[cross.Token], bool]:
9 modified = False
10
11 for tk in tokens:
12 if isinstance(tk, cross.TextToken):
13 original = tk.text
14 cleaned = __strip_mfm(original)
15 if cleaned != original:
16 modified = True
17 tk.text = cleaned
18
19 elif isinstance(tk, cross.LinkToken):
20 original = tk.label
21 cleaned = __strip_mfm(original)
22 if cleaned != original:
23 modified = True
24 tk.label = cleaned
25
26 return tokens, modified
27
28
29def __strip_mfm(text: str) -> str:
30 def match_contents(match: re.Match[str]):
31 content = match.group(1).strip()
32 parts = content.split(" ", 1)
33 return parts[1] if len(parts) > 1 else ""
34
35 while MFM_PATTERN.search(text):
36 text = MFM_PATTERN.sub(match_contents, text)
37
38 return text