1
1
package main
2
2
3
3
import (
4
+ "cmp"
4
5
"fmt"
5
6
"slices"
6
7
"strings"
7
8
)
8
9
9
- func bskyMessageToSlackMarkup (bskyMessage BskyMessage ) (string , error ) {
10
- var slackStringBuilder strings.Builder
10
+ func (b BskyTextFragment ) featureURI () string {
11
+ for _ , feat := range b .Features {
12
+ switch feat .URI {
13
+ case "app.bsky.richtext.facet#link" :
14
+ return feat .URI
15
+ case "app.bsky.richtext.facet#mention" :
16
+ return fmt .Sprintf ("https://bsky.app/profile/%s" , feat .DID )
17
+ case "app.bsky.richtext.facet#tag" :
18
+ return fmt .Sprintf ("https://bsky.app/hashtag/%s" , feat .Tag )
19
+ }
20
+ }
21
+ return ""
22
+ }
23
+
24
+ func bskyMessageToSlackMarkup (msg BskyMessage ) (string , error ) {
25
+ var sb strings.Builder
11
26
12
- fragments , err := facetsToFragments (bskyMessage )
27
+ fragments , err := facetsToFragments (msg )
13
28
if err != nil {
14
29
return "" , err
15
30
}
16
31
17
- for _ , fragment := range fragments {
18
- if fragment . Features == nil {
19
- slackStringBuilder . WriteString ( fragment .Text )
32
+ for _ , frag := range fragments {
33
+ if uri := frag . featureURI (); uri != "" {
34
+ fmt . Fprintf ( & sb , "<%s|%s>" , uri , frag .Text )
20
35
} else {
21
- uri := ""
22
- for _ , feature := range fragment .Features {
23
- if feature .Type == "app.bsky.richtext.facet#link" {
24
- uri = feature .Uri
25
- break
26
- } else if feature .Type == "app.bsky.richtext.facet#mention" {
27
- uri = fmt .Sprintf ("https://bsky.app/profile/%s" , feature .Did )
28
- break
29
- } else if feature .Type == "app.bsky.richtext.facet#tag" {
30
- uri = fmt .Sprintf ("https://bsky.app/hashtag/%s" , feature .Tag )
31
- }
32
- }
33
- if uri != "" {
34
- slackStringBuilder .WriteString (fmt .Sprintf ("<%s|%s>" , uri , fragment .Text ))
35
- } else {
36
- slackStringBuilder .WriteString (fragment .Text )
37
- }
36
+ sb .WriteString (frag .Text )
38
37
}
39
38
}
40
-
41
- return slackStringBuilder .String (), nil
39
+ return sb .String (), nil
42
40
}
43
41
44
42
func facetsToFragments (bskyMessage BskyMessage ) ([]BskyTextFragment , error ) {
@@ -47,8 +45,9 @@ func facetsToFragments(bskyMessage BskyMessage) ([]BskyTextFragment, error) {
47
45
48
46
fragments := []BskyTextFragment {}
49
47
48
+ // We use SortStable here as we want the original order of equal elements to stay the same.
50
49
slices .SortStableFunc (facets , func (a , b BskyFacet ) int {
51
- return a .Index .ByteStart - b .Index .ByteStart
50
+ return cmp . Compare ( a .Index .ByteStart , b .Index .ByteStart )
52
51
})
53
52
54
53
textCursor := 0
0 commit comments