@@ -91,18 +91,28 @@ def extract_twitter_url(text: str) -> str:
91
91
return twitter_url .split ("?" )[0 ]
92
92
93
93
@staticmethod
94
- def extract_mastodon_url (text : str ) -> str :
94
+ def extract_mastodon_url (text : str ) -> None | str :
95
95
"""
96
- Extract the Mastodon URL from the answer, handle @ username@instance format
96
+ Normalize Mastodon handle or URL to the format: https://<instance>/@< username>
97
97
"""
98
- if not text .startswith (("https://" , "http://" )) and text .count ("@" ) == 2 :
99
- mastodon_url = f"https://{ text .split ('@' )[2 ]} /@{ text .split ('@' )[1 ]} "
100
- else :
101
- mastodon_url = (
102
- f"https://{ text .removeprefix ('https://' ).removeprefix ('http://' )} "
103
- )
98
+ text = text .strip ().split ("?" , 1 )[0 ]
99
+
100
+ # Handle @username@instance or username@instance formats
101
+ if "@" in text and not text .startswith ("http" ):
102
+ parts = text .split ("@" )
103
+ if len (parts ) == 3 : # @username@instance
104
+ _ , username , instance = parts
105
+ elif len (parts ) == 2 : # username@instance
106
+ username , instance = parts
107
+ else :
108
+ return None
109
+ return f"https://{ instance } /@{ username } "
104
110
105
- return mastodon_url .split ("?" )[0 ]
111
+ # Handle full URLs
112
+ if text .startswith ("http://" ):
113
+ text = "https://" + text [len ("http://" ) :]
114
+
115
+ return text
106
116
107
117
@staticmethod
108
118
def extract_linkedin_url (text : str ) -> str :
@@ -126,7 +136,7 @@ def extract_bluesky_url(text: str) -> str:
126
136
Returns a normalized BlueSky URL in the form https://bsky.app/profile/<USERNAME>.bsky.social,
127
137
or uses the entire domain if it's custom (e.g., .dev).
128
138
"""
129
- text = text .split ("?" , 1 )[0 ]. strip ()
139
+ text = text .strip (). split ("?" , 1 )[0 ]
130
140
131
141
if text .startswith ("https://" ):
132
142
text = text [8 :]
@@ -136,6 +146,10 @@ def extract_bluesky_url(text: str) -> str:
136
146
if text .startswith ("www." ):
137
147
text = text [4 :]
138
148
149
+ # Remove @ if present
150
+ if text .startswith ("@" ):
151
+ text = text [1 :]
152
+
139
153
for marker in ("bsky.app/profile/" , "bsky/" ):
140
154
if marker in text :
141
155
text = text .split (marker , 1 )[1 ]
0 commit comments