Only linkify text if inline URLs are enabled

This commit is contained in:
Tulir Asokan 2022-04-15 23:44:59 +03:00
parent ebe3fcc33c
commit e08f23ba65
3 changed files with 13 additions and 6 deletions

View File

@ -147,11 +147,15 @@ func (msg *FileMessage) CalculateBuffer(prefs config.UserPreferences, width int,
if prefs.BareMessageView || prefs.DisableImages || len(msg.imageData) == 0 {
url := msg.matrix.GetDownloadURL(msg.URL)
var urlTString tstring.TString
if prefs.InlineURLs {
urlTString = tstring.NewStyleTString(url, tcell.StyleDefault.Hyperlink(url, msg.eventID.String()))
} else {
urlTString = tstring.NewTString(url)
}
text := tstring.NewTString(msg.Body).
Append(": ").
AppendTString(tstring.NewStyleTString(
url,
tcell.StyleDefault.Hyperlink(url, msg.eventID.String())))
AppendTString(urlTString)
msg.buffer = calculateBufferWithText(prefs, text, width, uiMsg)
return
}

View File

@ -383,10 +383,13 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity {
var spaces = regexp.MustCompile("\\s+")
var links = regexp.MustCompile(`https?://\S+`)
func TextToEntity(text string, eventID id.EventID) Entity {
func TextToEntity(text string, eventID id.EventID, linkify bool) Entity {
if len(text) == 0 {
return nil
}
if !linkify {
return NewTextEntity(text)
}
indices := links.FindAllStringIndex(text, -1)
if len(indices) == 0 {
return NewTextEntity(text)
@ -418,7 +421,7 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
node.Data = strings.ReplaceAll(node.Data, "\n", "")
node.Data = spaces.ReplaceAllLiteralString(node.Data, " ")
}
return TextToEntity(node.Data, parser.evt.ID)
return TextToEntity(node.Data, parser.evt.ID, parser.prefs.InlineURLs)
case html.ElementNode:
parsed := parser.tagNodeToEntity(node)
if parsed != nil && !parsed.IsBlock() && parsed.IsEmpty() {

View File

@ -208,7 +208,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt.Eve
return NewHTMLMessage(evt, displayname, html.Parse(matrix.Preferences(), room, content, evt, displayname))
}
content.Body = strings.Replace(content.Body, "\t", " ", -1)
return NewHTMLMessage(evt, displayname, html.TextToEntity(content.Body, evt.ID))
return NewHTMLMessage(evt, displayname, html.TextToEntity(content.Body, evt.ID, matrix.Preferences().InlineURLs))
case event.MsgImage, event.MsgVideo, event.MsgAudio, event.MsgFile:
msg := NewFileMessage(matrix, evt, displayname)
if !matrix.Preferences().DisableDownloads {