(telegram|matrix)fmt: mention formatting

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2024-08-05 14:40:55 -06:00
parent b539e5d63d
commit f56f520308
14 changed files with 123 additions and 59 deletions
+6 -3
View File
@@ -243,7 +243,7 @@ func (ctx Context) WithWhitespace() Context {
// HTMLParser is a somewhat customizable Matrix HTML parser.
type HTMLParser struct {
ParseGhostMXID func(id.UserID) (networkid.UserID, bool)
GetGhostDetails func(context.Context, id.UserID) (networkid.UserID, string, int64, bool)
}
// TaggedString is a string that also contains a HTML tag.
@@ -367,11 +367,14 @@ func (parser *HTMLParser) linkToString(node *html.Node, ctx Context) *EntityStri
// Mention not allowed, use name as-is
return str
}
userID, ok := parser.ParseGhostMXID(mxid)
userID, username, accessHash, ok := parser.GetGhostDetails(ctx.Ctx, mxid)
if !ok {
return str
} else if username == "" {
return ent.Format(telegramfmt.Mention{UserID: userID, AccessHash: accessHash})
} else {
return NewEntityString("@" + username).Format(telegramfmt.Mention{UserID: userID, Username: username})
}
return ent.Format(telegramfmt.Mention{UserID: userID})
}
if str.String.String() == href {
return ent.Format(telegramfmt.Style{Type: telegramfmt.StyleURL, URL: href})