@@ -290,12 +290,19 @@ public function create_post( $did, $post, $post_type, $hashtags, $access_token =
290290
291291 $ now = gmdate ( 'Y-m-d\TH:i:s\Z ' );
292292
293+ $ text = $ post ['content ' ] . $ hashtags ;
294+
293295 $ record = array (
294296 '$type ' => 'app.bsky.feed.post ' ,
295- 'text ' => $ post [ ' content ' ] . $ hashtags ,
297+ 'text ' => $ text ,
296298 'createdAt ' => $ now ,
297299 );
298300
301+ $ facets = $ this ->generate_facets ( $ text );
302+ if ( ! empty ( $ facets ) ) {
303+ $ record ['facets ' ] = $ facets ;
304+ }
305+
299306 if ( $ post_type === 'link ' && isset ( $ post ['post_url ' ] ) && ! empty ( $ post ['post_url ' ] ) ) {
300307 $ record ['embed ' ] = array (
301308 '$type ' => 'app.bsky.embed.external ' ,
@@ -369,6 +376,41 @@ public function create_post( $did, $post, $post_type, $hashtags, $access_token =
369376 }
370377 }
371378
379+ /**
380+ * Generate facets for hashtags.
381+ *
382+ * @param string $text The text to parse.
383+ *
384+ * @return mixed|array
385+ */
386+ private function generate_facets ( $ text ) {
387+ $ facets = array ();
388+ preg_match_all ( '/#\S+/ ' , $ text , $ matches , PREG_OFFSET_CAPTURE );
389+
390+ if ( ! empty ( $ matches [0 ] ) ) {
391+ foreach ( $ matches [0 ] as $ match ) {
392+ $ hashtag = $ match [0 ];
393+ $ start = $ match [1 ];
394+ $ end = $ start + strlen ( $ hashtag );
395+
396+ $ facets [] = array (
397+ 'index ' => array (
398+ 'byteStart ' => $ start ,
399+ 'byteEnd ' => $ end ,
400+ ),
401+ 'features ' => array (
402+ array (
403+ '$type ' => 'app.bsky.richtext.facet#tag ' ,
404+ 'tag ' => ltrim ( $ hashtag , '# ' ),
405+ ),
406+ ),
407+ );
408+ }
409+ }
410+
411+ return $ facets ;
412+ }
413+
372414 /**
373415 * Fetch Website Card embeds.
374416 *
0 commit comments