social media crossposting tool. 3rd time's the charm
mastodon misskey crossposting bluesky

fix link handling

zenfyr.dev ff18481f 7b2d0f10

verified
Changed files
+28 -1
+14
bluesky.py
···
def tokens_to_richtext(tokens: list[cross.Token]) -> client_utils.TextBuilder | None:
builder = client_utils.TextBuilder()
+
+
def flatten_link(href: str):
+
split = href.split('://', 1)
+
if len(split) > 1:
+
href = split[1]
+
+
if len(href) > 32:
+
href = href[:32] + '...'
+
+
return href
for token in tokens:
if isinstance(token, cross.TextToken):
builder.text(token.text)
elif isinstance(token, cross.LinkToken):
+
if util.canonical_label(token.label, token.href):
+
builder.link(flatten_link(token.href), token.href)
+
continue
+
builder.link(token.label, token.href)
elif isinstance(token, cross.TagToken):
builder.tag('#' + token.tag, token.tag)
+14 -1
util.py
···
ALTERNATE = re.compile(r'\S+|\s+')
+
def canonical_label(label: str | None, href: str):
+
if not label or label == href:
+
return True
+
+
split = href.split('://', 1)
+
if len(split) > 1:
+
if split[1] == label:
+
return True
+
+
return False
+
def split_tokens(tokens: list[cross.Token], max_chars: int) -> list[list[cross.Token]]:
def start_new_block():
nonlocal current_block, blocks, current_length
···
current_length = wlen
elif isinstance(token, cross.LinkToken):
-
link_len = min(len(token.label), 35)
+
link_len = len(token.label)
+
if canonical_label(token.label, token.href):
+
link_len = min(link_len, 35)
if current_length + link_len <= max_chars:
current_block.append(token)