/
render_hooks.go
234 lines (200 loc) · 6.29 KB
/
render_hooks.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package goldmark
import (
"bytes"
"github.com/sunwei/hugo-playground/common/types/hstring"
"github.com/sunwei/hugo-playground/markup/converter/hooks"
"github.com/sunwei/hugo-playground/markup/goldmark/goldmark_config"
"github.com/sunwei/hugo-playground/markup/goldmark/internal/render"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
"strings"
)
func newLinks(cfg goldmark_config.Config) goldmark.Extender {
return &links{cfg: cfg}
}
type links struct {
cfg goldmark_config.Config
}
// Extend implements goldmark.Extender.
func (e *links) Extend(m goldmark.Markdown) {
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(newLinkRenderer(e.cfg), 100),
))
}
func newLinkRenderer(cfg goldmark_config.Config) renderer.NodeRenderer {
r := &hookedRenderer{
linkifyProtocol: []byte(cfg.Extensions.LinkifyProtocol),
Config: html.Config{
Writer: html.DefaultWriter,
},
}
return r
}
type hookedRenderer struct {
linkifyProtocol []byte
html.Config
}
// RegisterFuncs implements NodeRenderer.RegisterFuncs.
func (r *hookedRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(ast.KindLink, r.renderLink)
reg.Register(ast.KindAutoLink, r.renderAutoLink)
}
func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Link)
var lr hooks.LinkRenderer
ctx, ok := w.(*render.Context)
if ok {
h := ctx.RenderContext().GetRenderer(hooks.LinkRendererType, nil)
ok = h != nil
if ok {
lr = h.(hooks.LinkRenderer)
}
}
if !ok {
return r.renderLinkDefault(w, source, node, entering)
}
if entering {
// Store the current pos so we can capture the rendered text.
ctx.PushPos(ctx.Buffer.Len())
return ast.WalkContinue, nil
}
pos := ctx.PopPos()
text := ctx.Buffer.Bytes()[pos:]
ctx.Buffer.Truncate(pos)
err := lr.RenderLink(
w,
linkContext{
page: ctx.DocumentContext().Document,
destination: string(n.Destination),
title: string(n.Title),
text: hstring.RenderedString(text),
plainText: string(n.Text(source)),
},
)
// TODO(bep) I have a working branch that fixes these rather confusing identity types,
// but for now it's important that it's not .GetIdentity() that's added here,
// to make sure we search the entire chain on changes.
ctx.AddIdentity(lr)
return ast.WalkContinue, err
}
// Fall back to the default Goldmark render funcs. Method below borrowed from:
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
func (r *hookedRenderer) renderLinkDefault(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.WriteByte('"')
if n.Title != nil {
_, _ = w.WriteString(` title="`)
r.Writer.Write(w, n.Title)
_ = w.WriteByte('"')
}
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("</a>")
}
return ast.WalkContinue, nil
}
type linkContext struct {
page any
destination string
title string
text hstring.RenderedString
plainText string
}
func (ctx linkContext) Destination() string {
return ctx.destination
}
func (ctx linkContext) Resolved() bool {
return false
}
func (ctx linkContext) Page() any {
return ctx.page
}
func (ctx linkContext) Text() hstring.RenderedString {
return ctx.text
}
func (ctx linkContext) PlainText() string {
return ctx.plainText
}
func (ctx linkContext) Title() string {
return ctx.title
}
func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
n := node.(*ast.AutoLink)
var lr hooks.LinkRenderer
ctx, ok := w.(*render.Context)
if ok {
h := ctx.RenderContext().GetRenderer(hooks.LinkRendererType, nil)
ok = h != nil
if ok {
lr = h.(hooks.LinkRenderer)
}
}
if !ok {
return r.renderAutoLinkDefault(w, source, node, entering)
}
url := string(r.autoLinkURL(n, source))
label := string(n.Label(source))
if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(url), "mailto:") {
url = "mailto:" + url
}
err := lr.RenderLink(
w,
linkContext{
page: ctx.DocumentContext().Document,
destination: url,
text: hstring.RenderedString(label),
plainText: label,
},
)
// TODO(bep) I have a working branch that fixes these rather confusing identity types,
// but for now it's important that it's not .GetIdentity() that's added here,
// to make sure we search the entire chain on changes.
ctx.AddIdentity(lr)
return ast.WalkContinue, err
}
// Fall back to the default Goldmark render funcs. Method below borrowed from:
// https://github.com/yuin/goldmark/blob/5588d92a56fe1642791cf4aa8e9eae8227cfeecd/renderer/html/html.go#L439
func (r *hookedRenderer) renderAutoLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.AutoLink)
if !entering {
return ast.WalkContinue, nil
}
_, _ = w.WriteString(`<a href="`)
url := r.autoLinkURL(n, source)
label := n.Label(source)
if n.AutoLinkType == ast.AutoLinkEmail && !bytes.HasPrefix(bytes.ToLower(url), []byte("mailto:")) {
_, _ = w.WriteString("mailto:")
}
_, _ = w.Write(util.EscapeHTML(util.URLEscape(url, false)))
if n.Attributes() != nil {
_ = w.WriteByte('"')
html.RenderAttributes(w, n, html.LinkAttributeFilter)
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString(`">`)
}
_, _ = w.Write(util.EscapeHTML(label))
_, _ = w.WriteString(`</a>`)
return ast.WalkContinue, nil
}
func (r *hookedRenderer) autoLinkURL(n *ast.AutoLink, source []byte) []byte {
url := n.URL(source)
if len(n.Protocol) > 0 && !bytes.Equal(n.Protocol, r.linkifyProtocol) {
// The CommonMark spec says "http" is the correct protocol for links,
// but this doesn't make much sense (the fact that they should care about the rendered output).
// Note that n.Protocol is not set if protocol is provided by user.
url = append(r.linkifyProtocol, url[len(n.Protocol):]...)
}
return url
}