-
Notifications
You must be signed in to change notification settings - Fork 180
/
link.go
78 lines (70 loc) Β· 1.48 KB
/
link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ansi
import (
"io"
"net/url"
)
// A LinkElement is used to render hyperlinks.
type LinkElement struct {
Text string
BaseURL string
URL string
Child ElementRenderer // FIXME
}
func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error {
var textRendered bool
if len(e.Text) > 0 && e.Text != e.URL {
textRendered = true
el := &BaseElement{
Token: e.Text,
Style: ctx.options.Styles.LinkText,
}
err := el.Render(w, ctx)
if err != nil {
return err
}
}
/*
if node.LastChild != nil {
if node.LastChild.Type == bf.Image {
el := tr.NewElement(node.LastChild)
err := el.Renderer.Render(w, node.LastChild, tr)
if err != nil {
return err
}
}
if len(node.LastChild.Literal) > 0 &&
string(node.LastChild.Literal) != string(node.LinkData.Destination) {
textRendered = true
el := &BaseElement{
Token: string(node.LastChild.Literal),
Style: ctx.style[LinkText],
}
err := el.Render(w, node.LastChild, tr)
if err != nil {
return err
}
}
}
*/
u, err := url.Parse(e.URL)
if err == nil &&
"#"+u.Fragment != e.URL { // if the URL only consists of an anchor, ignore it
pre := " "
style := ctx.options.Styles.Link
if !textRendered {
pre = ""
style.BlockPrefix = ""
style.BlockSuffix = ""
}
el := &BaseElement{
Token: resolveRelativeURL(e.BaseURL, e.URL),
Prefix: pre,
Style: style,
}
err := el.Render(w, ctx)
if err != nil {
return err
}
}
return nil
}