mirror of
https://github.com/sartoopjj/thefeed.git
synced 2026-05-19 10:04:36 +03:00
fix: output named links as [label](url) in logged-in Telegram fetcher
Aligns applyTextURLEntities with the public HTML fetcher so both modes produce the same markdown link format for the client-side linkify renderer.
This commit is contained in:
@@ -114,7 +114,7 @@ func TestApplyTextURLEntities(t *testing.T) {
|
||||
entities: []tg.MessageEntityClass{
|
||||
&tg.MessageEntityTextURL{Offset: 10, Length: 9, URL: "https://example.com"},
|
||||
},
|
||||
want: "Check out this link (https://example.com) for details",
|
||||
want: "Check out [this link](https://example.com) for details",
|
||||
},
|
||||
{
|
||||
name: "display text equals url",
|
||||
@@ -139,7 +139,7 @@ func TestApplyTextURLEntities(t *testing.T) {
|
||||
&tg.MessageEntityTextURL{Offset: 4, Length: 5, URL: "https://one.com"},
|
||||
&tg.MessageEntityTextURL{Offset: 14, Length: 6, URL: "https://two.com"},
|
||||
},
|
||||
want: "see first (https://one.com) and second (https://two.com) links",
|
||||
want: "see [first](https://one.com) and [second](https://two.com) links",
|
||||
},
|
||||
{
|
||||
name: "emoji in text (surrogate pair)",
|
||||
@@ -147,7 +147,7 @@ func TestApplyTextURLEntities(t *testing.T) {
|
||||
entities: []tg.MessageEntityClass{
|
||||
&tg.MessageEntityTextURL{Offset: 3, Length: 10, URL: "https://poll.com"},
|
||||
},
|
||||
want: "📊 click here (https://poll.com)",
|
||||
want: "📊 [click here](https://poll.com)",
|
||||
},
|
||||
{
|
||||
name: "non-text-url entities ignored",
|
||||
|
||||
@@ -196,7 +196,7 @@ func TestExtractMessageTextPreservesLinks(t *testing.T) {
|
||||
}
|
||||
node := findFirstByClass(doc, "tgme_widget_message_text")
|
||||
text := extractMessageText(node)
|
||||
want := "Check out this link (https://example.com) for details"
|
||||
want := "Check out [this link](https://example.com) for details"
|
||||
if text != want {
|
||||
t.Fatalf("extractMessageText = %q, want %q", text, want)
|
||||
}
|
||||
|
||||
@@ -466,7 +466,7 @@ func (tr *TelegramReader) extractText(msg *tg.Message) string {
|
||||
}
|
||||
|
||||
// applyTextURLEntities embeds hyperlink URLs from MessageEntityTextURL entities
|
||||
// into the message text, producing output like "display text (https://url)".
|
||||
// into the message text, producing output like "[display text](https://url)".
|
||||
// This mirrors what the public HTML reader does when it extracts <a> tags.
|
||||
// Offsets are in UTF-16 code units per the Telegram API spec.
|
||||
func applyTextURLEntities(text string, entities []tg.MessageEntityClass) string {
|
||||
@@ -519,10 +519,11 @@ func applyTextURLEntities(text string, entities []tg.MessageEntityClass) string
|
||||
if string(runes[startIdx:endIdx]) == u.url {
|
||||
continue
|
||||
}
|
||||
ins := []rune(" (" + u.url + ")")
|
||||
newRunes := make([]rune, 0, len(runes)+len(ins))
|
||||
newRunes = append(newRunes, runes[:endIdx]...)
|
||||
newRunes = append(newRunes, ins...)
|
||||
label := string(runes[startIdx:endIdx])
|
||||
replacement := []rune("[" + label + "](" + u.url + ")")
|
||||
newRunes := make([]rune, 0, len(runes)-len([]rune(label))+len(replacement))
|
||||
newRunes = append(newRunes, runes[:startIdx]...)
|
||||
newRunes = append(newRunes, replacement...)
|
||||
newRunes = append(newRunes, runes[endIdx:]...)
|
||||
runes = newRunes
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user