make links in bluesky posts clickable
Bluesky linkifies nothing by itself. A URL in the text of a record is plain text unless a richtext facet says which bytes of the post are a link and where they point, so our posts carried URLs no one could press. Each link now travels on the Post as an output.Link with the offsets of the text it occupies, and the bluesky output turns those into app.bsky.richtext.facet#link. The offsets are UTF-8 byte offsets, not character counts: a facet measured in characters slides off the URL as soon as any Japanese text precedes it, and underlines the wrong words. Scanning moved from preview.go to findLinks in main.go, which now reports every link with its offsets rather than just the first one; the preview card still goes to the first, which is the one a reader meets first. X is unaffected, as it linkifies URLs itself. The facet feature only gains its lexicon type when marshalled, so the test checks the encoded record rather than the struct. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
25
main.go
25
main.go
@@ -46,6 +46,24 @@ func trimURL(match string) string {
|
||||
return strings.TrimRight(match, ".,!?、。)]}>")
|
||||
}
|
||||
|
||||
// findLinks returns every link in content, each with the UTF-8 byte offsets
|
||||
// of the text it occupies.
|
||||
func findLinks(content string) []output.Link {
|
||||
var links []output.Link
|
||||
for _, span := range urlPattern.FindAllStringIndex(content, -1) {
|
||||
raw := trimURL(content[span[0]:span[1]])
|
||||
if u, err := url.Parse(raw); err != nil || u.Host == "" {
|
||||
continue
|
||||
}
|
||||
links = append(links, output.Link{
|
||||
URL: raw,
|
||||
ByteStart: span[0],
|
||||
ByteEnd: span[0] + len(raw),
|
||||
})
|
||||
}
|
||||
return links
|
||||
}
|
||||
|
||||
// tweetLength counts a message the way Twitter does, charging every link a
|
||||
// fixed length instead of its actual one.
|
||||
func tweetLength(content string) int {
|
||||
@@ -221,9 +239,11 @@ func (dist *distributor) created(event discord.Event) {
|
||||
return
|
||||
}
|
||||
|
||||
// The card goes to the first link, the one a reader meets first.
|
||||
links := findLinks(event.Content)
|
||||
var preview *output.Preview
|
||||
if link := findLink(event.Content); link != "" {
|
||||
preview, err = linkPreview(link)
|
||||
if len(links) > 0 {
|
||||
preview, err = linkPreview(links[0].URL)
|
||||
if err != nil {
|
||||
// The post is still worth making without its card.
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
@@ -241,6 +261,7 @@ func (dist *distributor) created(event discord.Event) {
|
||||
post := output.Post{
|
||||
Text: event.Content,
|
||||
Images: images,
|
||||
Links: links,
|
||||
Preview: preview,
|
||||
}
|
||||
if parent, ok := parents[out.GetName()]; ok && !parent.IsZero() {
|
||||
|
||||
Reference in New Issue
Block a user