1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-01 20:34:27 +00:00
cchat-discord/segments/link.go

57 lines
967 B
Go
Raw Normal View History

2020-06-19 01:00:24 +00:00
package segments
import (
"github.com/diamondburned/cchat/text"
"github.com/yuin/goldmark/ast"
)
type LinkSegment struct {
start, end int
url string
}
var _ text.Linker = (*LinkSegment)(nil)
func (r *TextRenderer) link(n *ast.Link, enter bool) ast.WalkStatus {
if enter {
// Make a segment with a start pointing to the end of buffer.
seg := LinkSegment{
start: r.i(),
url: string(n.Destination),
}
// Write the actual title.
r.buf.Write(n.Title)
// Close the segment.
seg.end = r.i()
r.append(seg)
}
return ast.WalkContinue
}
func (r *TextRenderer) autoLink(n *ast.AutoLink, enter bool) ast.WalkStatus {
if enter {
seg := LinkSegment{
start: r.i(),
url: string(n.URL(r.src)),
}
r.buf.Write(n.URL(r.src))
seg.end = r.i()
r.append(seg)
}
return ast.WalkContinue
}
func (l LinkSegment) Bounds() (start, end int) {
return l.start, l.end
}
func (l LinkSegment) Link() (url string) {
return l.url
}