Linkify links in HTML messages too
This commit is contained in:
parent
0fb06067ae
commit
7bf6785689
@ -381,6 +381,7 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var spaces = regexp.MustCompile("\\s+")
|
var spaces = regexp.MustCompile("\\s+")
|
||||||
|
var links = regexp.MustCompile(`https?://\S+`)
|
||||||
|
|
||||||
func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
|
func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
|
||||||
switch node.Type {
|
switch node.Type {
|
||||||
@ -392,7 +393,28 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
|
|||||||
if len(node.Data) == 0 {
|
if len(node.Data) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return NewTextEntity(node.Data)
|
indices := links.FindAllStringIndex(node.Data, -1)
|
||||||
|
if len(indices) == 0 {
|
||||||
|
return NewTextEntity(node.Data)
|
||||||
|
}
|
||||||
|
ent := &ContainerEntity{
|
||||||
|
BaseEntity: &BaseEntity{Tag: "span"},
|
||||||
|
}
|
||||||
|
var lastEnd int
|
||||||
|
for i, item := range indices {
|
||||||
|
start, end := item[0], item[1]
|
||||||
|
if start > lastEnd {
|
||||||
|
ent.Children = append(ent.Children, NewTextEntity(node.Data[lastEnd:start]))
|
||||||
|
}
|
||||||
|
link := node.Data[start:end]
|
||||||
|
linkID := fmt.Sprintf("%s-%d", parser.evt.ID, i)
|
||||||
|
ent.Children = append(ent.Children, NewTextEntity(link).AdjustStyle(AdjustStyleLink(link, linkID), AdjustStyleReasonNormal))
|
||||||
|
lastEnd = end
|
||||||
|
}
|
||||||
|
if lastEnd < len(node.Data) {
|
||||||
|
ent.Children = append(ent.Children, NewTextEntity(node.Data[lastEnd:]))
|
||||||
|
}
|
||||||
|
return ent
|
||||||
case html.ElementNode:
|
case html.ElementNode:
|
||||||
parsed := parser.tagNodeToEntity(node)
|
parsed := parser.tagNodeToEntity(node)
|
||||||
if parsed != nil && !parsed.IsBlock() && parsed.IsEmpty() {
|
if parsed != nil && !parsed.IsBlock() && parsed.IsEmpty() {
|
||||||
|
@ -76,7 +76,7 @@ func (msg *TextMessage) getCache(uiMsg *UIMessage) tstring.TString {
|
|||||||
link := msg.Text[start:end]
|
link := msg.Text[start:end]
|
||||||
linkID := fmt.Sprintf("%s-%d", msg.eventID, i)
|
linkID := fmt.Sprintf("%s-%d", msg.eventID, i)
|
||||||
content = content.
|
content = content.
|
||||||
Append(msg.Text[:start]).
|
Append(msg.Text[lastEnd:start]).
|
||||||
AppendTString(tstring.NewStyleTString(link, tcell.StyleDefault.Hyperlink(link, linkID)))
|
AppendTString(tstring.NewStyleTString(link, tcell.StyleDefault.Hyperlink(link, linkID)))
|
||||||
lastEnd = end
|
lastEnd = end
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user