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:
2026-08-03 09:35:59 +09:00
parent 953664ff1c
commit d794f65673
7 changed files with 153 additions and 37 deletions

View File

@@ -30,6 +30,55 @@ func noiseJPEG(t *testing.T, width, height int) []byte {
return buf.Bytes()
}
func TestFindLinks(t *testing.T) {
tests := []struct {
name string
content string
want []output.Link
}{
{
// The offsets are byte offsets, so the multibyte prefix counts
// for more than the three characters it looks like.
"after multibyte text",
"みてね https://example.com/article",
[]output.Link{{URL: "https://example.com/article", ByteStart: 10, ByteEnd: 37}},
},
{
"trailing punctuation is outside the link",
"これ→https://example.com/x。",
[]output.Link{{URL: "https://example.com/x", ByteStart: 9, ByteEnd: 30}},
},
{
"several",
"https://one.example https://two.example",
[]output.Link{
{URL: "https://one.example", ByteStart: 0, ByteEnd: 19},
{URL: "https://two.example", ByteStart: 20, ByteEnd: 39},
},
},
{"no link", "ただのつぶやき", nil},
{"scheme only", "http:// と書いただけ", nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := findLinks(tt.content)
if len(got) != len(tt.want) {
t.Fatalf("findLinks(%q) returned %d links, want %d", tt.content, len(got), len(tt.want))
}
for i, link := range got {
if link != tt.want[i] {
t.Errorf("link %d = %+v, want %+v", i, link, tt.want[i])
}
// A facet pointing at the wrong bytes marks up the wrong text.
if slice := tt.content[link.ByteStart:link.ByteEnd]; slice != link.URL {
t.Errorf("bytes [%d:%d] are %q, want %q", link.ByteStart, link.ByteEnd, slice, link.URL)
}
}
})
}
}
func TestTweetLength(t *testing.T) {
tests := []struct {
name string