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 {
|
|
|
|
// Write the actual title.
|
2020-07-06 00:18:40 +00:00
|
|
|
start, end := r.write(n.Title)
|
2020-06-19 01:00:24 +00:00
|
|
|
|
|
|
|
// Close the segment.
|
2020-07-06 00:18:40 +00:00
|
|
|
r.append(LinkSegment{
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
string(n.Destination),
|
|
|
|
})
|
2020-06-19 01:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TextRenderer) autoLink(n *ast.AutoLink, enter bool) ast.WalkStatus {
|
|
|
|
if enter {
|
2020-07-06 00:18:40 +00:00
|
|
|
start, end := r.write(n.URL(r.src))
|
2020-06-19 01:00:24 +00:00
|
|
|
|
2020-07-06 00:18:40 +00:00
|
|
|
r.append(LinkSegment{
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
string(n.URL((r.src))),
|
|
|
|
})
|
2020-06-19 01:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l LinkSegment) Bounds() (start, end int) {
|
|
|
|
return l.start, l.end
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l LinkSegment) Link() (url string) {
|
|
|
|
return l.url
|
|
|
|
}
|