Add proper facets for hashtags when posting to bluesky
authorMagnus Hagander <magnus@hagander.net>
Thu, 20 Feb 2025 17:46:09 +0000 (18:46 +0100)
committerMagnus Hagander <magnus@hagander.net>
Thu, 20 Feb 2025 17:46:09 +0000 (18:46 +0100)
postgresqleu/util/messaging/bluesky.py

index 4ee37fb8f2a6edea7924a226a7430bb0bfc27615..e722b691c5f7384444184a835a18af569a813c18 100644 (file)
@@ -277,6 +277,18 @@ class Bluesky(object):
             )
         return spans
 
+    def _parse_hashtags(self, text: str):
+        hashtags_regex = re.compile(rb'[$|\W](#\w+)')
+        text_bytes = text.encode("UTF-8")
+        for m in hashtags_regex.finditer(text_bytes):
+            yield {
+                "index": {
+                    "byteStart": m.start(1),
+                    "byteEnd": m.end(1),
+                },
+                "features": [{"$type": "app.bsky.richtext.facet#tag", "tag": m.group(1).decode("UTF-8")}],
+            }
+
     def _parse_facets(self, text: str):
         """
         parses post text and returns a list of app.bsky.richtext.facet objects for any mentions (@handle.example.com) or URLs (https://example.com)
@@ -303,6 +315,9 @@ class Bluesky(object):
                     "features": [{"$type": "app.bsky.richtext.facet#mention", "did": did}],
                 }
             )
+
+        facets.extend(self._parse_hashtags(text))
+
         newtext, urls = self._parse_urls(text)
 
         for u in urls: