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
|
package tailwind
import (
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
)
type CustomLinkRenderer struct {
html.Config
}
func NewCustomLinkRenderer(opts ...html.Option) renderer.NodeRenderer {
r := &CustomLinkRenderer{
Config: html.NewConfig(),
}
for _, opt := range opts {
opt.SetHTMLOption(&r.Config)
}
return r
}
func (r *CustomLinkRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Link)
if entering {
_, _ = w.WriteString("<a href=\"")
if r.Unsafe || !html.IsDangerousURL(n.Destination) {
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
}
_, _ = w.WriteString(`"`)
if n.Title != nil {
_, _ = w.WriteString(` title="`)
_, _ = w.Write(util.EscapeHTML(n.Title))
_, _ = w.WriteString(`"`)
}
if n.Attributes() != nil {
html.RenderAttributes(w, n, nil)
}
_, _ = w.WriteString(">")
} else {
_, _ = w.WriteString("</a>")
}
return ast.WalkContinue, nil
}
func (r *CustomLinkRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(ast.KindLink, r.renderLink)
}
|