-
Notifications
You must be signed in to change notification settings - Fork 5
/
renderer.go
471 lines (425 loc) · 13.7 KB
/
renderer.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Package markdown is a goldmark renderer that outputs markdown.
package markdown
import (
"bytes"
"fmt"
"io"
"slices"
"sync"
"unicode"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
)
// NewRenderer returns a new markdown Renderer that is configured by default values.
func NewRenderer(options ...Option) *Renderer {
r := &Renderer{
config: NewConfig(),
rc: renderContext{},
maxKind: 20, // a random number slightly larger than the number of default ast kinds
nodeRendererFuncsTmp: map[ast.NodeKind]renderer.NodeRendererFunc{},
}
for _, opt := range options {
opt.SetMarkdownOption(r.config)
}
return r
}
// Renderer is an implementation of renderer.Renderer that renders nodes as Markdown
type Renderer struct {
config *Config
rc renderContext
nodeRendererFuncsTmp map[ast.NodeKind]renderer.NodeRendererFunc
maxKind int
nodeRendererFuncs []nodeRenderer
initSync sync.Once
}
var _ renderer.Renderer = &Renderer{}
// AddOptions implements renderer.Renderer.AddOptions
func (r *Renderer) AddOptions(opts ...renderer.Option) {
config := renderer.NewConfig()
for _, opt := range opts {
opt.SetConfig(config)
}
for name, value := range config.Options {
r.config.SetOption(name, value)
}
// handle any config.NodeRenderers set by opts
config.NodeRenderers.Sort()
l := len(config.NodeRenderers)
for i := l - 1; i >= 0; i-- {
v := config.NodeRenderers[i]
nr, _ := v.Value.(renderer.NodeRenderer)
nr.RegisterFuncs(r)
}
}
func (r *Renderer) Register(kind ast.NodeKind, fun renderer.NodeRendererFunc) {
r.nodeRendererFuncsTmp[kind] = fun
if int(kind) > r.maxKind {
r.maxKind = int(kind)
}
}
// Render implements renderer.Renderer.Render
func (r *Renderer) Render(w io.Writer, source []byte, n ast.Node) error {
r.rc = newRenderContext(w, source, r.config)
r.initSync.Do(func() {
r.nodeRendererFuncs = make([]nodeRenderer, r.maxKind+1)
// add default functions
// blocks
r.nodeRendererFuncs[ast.KindDocument] = r.renderBlockSeparator
r.nodeRendererFuncs[ast.KindHeading] = r.chainRenderers(r.renderBlockSeparator, r.renderHeading)
r.nodeRendererFuncs[ast.KindBlockquote] = r.chainRenderers(r.renderBlockSeparator, r.renderBlockquote)
r.nodeRendererFuncs[ast.KindCodeBlock] = r.chainRenderers(r.renderBlockSeparator, r.renderCodeBlock)
r.nodeRendererFuncs[ast.KindFencedCodeBlock] = r.chainRenderers(r.renderBlockSeparator, r.renderFencedCodeBlock)
r.nodeRendererFuncs[ast.KindHTMLBlock] = r.chainRenderers(r.renderBlockSeparator, r.renderHTMLBlock)
r.nodeRendererFuncs[ast.KindList] = r.chainRenderers(r.renderBlockSeparator, r.renderList)
r.nodeRendererFuncs[ast.KindListItem] = r.chainRenderers(r.renderBlockSeparator, r.renderListItem)
r.nodeRendererFuncs[ast.KindParagraph] = r.renderBlockSeparator
r.nodeRendererFuncs[ast.KindTextBlock] = r.renderBlockSeparator
r.nodeRendererFuncs[ast.KindThematicBreak] = r.chainRenderers(r.renderBlockSeparator, r.renderThematicBreak)
// inlines
r.nodeRendererFuncs[ast.KindAutoLink] = r.renderAutoLink
r.nodeRendererFuncs[ast.KindCodeSpan] = r.renderCodeSpan
r.nodeRendererFuncs[ast.KindEmphasis] = r.renderEmphasis
r.nodeRendererFuncs[ast.KindImage] = r.renderImage
r.nodeRendererFuncs[ast.KindLink] = r.renderLink
r.nodeRendererFuncs[ast.KindRawHTML] = r.renderRawHTML
r.nodeRendererFuncs[ast.KindText] = r.renderText
// TODO: add KindString
// r.nodeRendererFuncs[ast.KindString] = r.renderString
for kind, fun := range r.nodeRendererFuncsTmp {
r.nodeRendererFuncs[kind] = r.transform(fun)
}
r.nodeRendererFuncsTmp = nil
})
return ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
return r.nodeRendererFuncs[n.Kind()](n, entering), r.rc.writer.Err()
})
}
// transform wraps a renderer.NodeRendererFunc to match the nodeRenderer function signature
func (r *Renderer) transform(fn renderer.NodeRendererFunc) nodeRenderer {
return func(n ast.Node, entering bool) ast.WalkStatus {
status, _ := fn(r.rc.writer, r.rc.source, n, entering)
return status
}
}
// nodeRenderer is a markdown node renderer func.
type nodeRenderer func(ast.Node, bool) ast.WalkStatus
func (r *Renderer) chainRenderers(renderers ...nodeRenderer) nodeRenderer {
return func(node ast.Node, entering bool) ast.WalkStatus {
var walkStatus ast.WalkStatus
for i := range renderers {
// go through renderers in reverse when exiting
if !entering {
i = len(renderers) - 1 - i
}
walkStatus = renderers[i](node, entering)
}
return walkStatus
}
}
func (r *Renderer) renderBlockSeparator(node ast.Node, entering bool) ast.WalkStatus {
if entering {
// Add blank previous line if applicable
if node.PreviousSibling() != nil && node.HasBlankPreviousLines() {
r.rc.writer.EndLine()
}
} else {
// Flush line buffer to complete line written by previous block
r.rc.writer.FlushLine()
}
return ast.WalkContinue
}
func (r *Renderer) renderAutoLink(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.AutoLink)
if entering {
r.rc.writer.WriteBytes([]byte("<"))
r.rc.writer.WriteBytes(n.URL(r.rc.source))
} else {
r.rc.writer.WriteBytes([]byte(">"))
}
return ast.WalkContinue
}
func (r *Renderer) renderBlockquote(node ast.Node, entering bool) ast.WalkStatus {
if entering {
r.rc.writer.PushPrefix([]byte("> "))
} else {
r.rc.writer.PopPrefix()
}
return ast.WalkContinue
}
func (r *Renderer) renderHeading(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.Heading)
// Empty headings or headings above level 2 can only be ATX
if !n.HasChildren() || n.Level > 2 {
return r.renderATXHeading(n, entering)
}
// Multiline headings can only be Setext
if n.Lines().Len() > 1 {
return r.renderSetextHeading(n, entering)
}
// Otherwise it's up to the configuration
if r.config.IsSetext() {
return r.renderSetextHeading(n, entering)
}
return r.renderATXHeading(n, entering)
}
func (r *Renderer) renderATXHeading(node *ast.Heading, entering bool) ast.WalkStatus {
if entering {
r.rc.writer.WriteBytes(bytes.Repeat([]byte("#"), node.Level))
// Only print space after heading if non-empty
if node.HasChildren() {
r.rc.writer.WriteBytes([]byte(" "))
}
} else {
if r.config.HeadingStyle == HeadingStyleATXSurround {
r.rc.writer.WriteBytes([]byte(" "))
r.rc.writer.WriteBytes(bytes.Repeat([]byte("#"), node.Level))
}
}
return ast.WalkContinue
}
func (r *Renderer) renderSetextHeading(node *ast.Heading, entering bool) ast.WalkStatus {
if entering {
return ast.WalkContinue
}
underlineChar := [...][]byte{[]byte(""), []byte("="), []byte("-")}[node.Level]
underlineWidth := 3
if r.config.HeadingStyle == HeadingStyleFullWidthSetext {
lines := node.Lines()
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
lineWidth := line.Len()
if lineWidth > underlineWidth {
underlineWidth = lineWidth
}
}
}
r.rc.writer.WriteBytes([]byte("\n"))
r.rc.writer.WriteBytes(bytes.Repeat(underlineChar, underlineWidth))
return ast.WalkContinue
}
func (r *Renderer) renderThematicBreak(node ast.Node, entering bool) ast.WalkStatus {
if entering {
breakChars := []byte{'-', '*', '_'}
breakChar := breakChars[r.config.ThematicBreakStyle : r.config.ThematicBreakStyle+1]
breakLen := int(max(r.config.ThematicBreakLength, ThematicBreakLengthMinimum))
r.rc.writer.WriteBytes(bytes.Repeat(breakChar, breakLen))
}
return ast.WalkContinue
}
func (r *Renderer) renderCodeBlock(node ast.Node, entering bool) ast.WalkStatus {
if entering {
r.rc.writer.PushPrefix(r.config.Bytes())
r.renderLines(node, entering)
} else {
r.rc.writer.PopPrefix()
}
return ast.WalkContinue
}
func (r *Renderer) renderFencedCodeBlock(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.FencedCodeBlock)
r.rc.writer.WriteBytes([]byte("```"))
if entering {
if info := n.Info; info != nil {
r.rc.writer.WriteBytes(info.Value(r.rc.source))
}
r.rc.writer.FlushLine()
r.renderLines(node, entering)
}
return ast.WalkContinue
}
func (r *Renderer) renderHTMLBlock(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.HTMLBlock)
if entering {
r.renderLines(node, entering)
} else {
if n.HasClosure() {
r.rc.writer.WriteLine(n.ClosureLine.Value(r.rc.source))
}
}
return ast.WalkContinue
}
func (r *Renderer) renderList(node ast.Node, entering bool) ast.WalkStatus {
if entering {
n := node.(*ast.List)
r.rc.lists = append(r.rc.lists, listContext{
list: n,
num: n.Start,
})
} else {
r.rc.lists = r.rc.lists[:len(r.rc.lists)-1]
}
return ast.WalkContinue
}
func (r *Renderer) renderListItem(node ast.Node, entering bool) ast.WalkStatus {
if entering {
var itemPrefix []byte
l := r.rc.lists[len(r.rc.lists)-1]
if l.list.IsOrdered() {
itemPrefix = append(itemPrefix, []byte(fmt.Sprint(l.num))...)
r.rc.lists[len(r.rc.lists)-1].num += 1
}
itemPrefix = append(itemPrefix, l.list.Marker, ' ')
// Prefix the current line with the item prefix
r.rc.writer.PushPrefix(itemPrefix, 0, 0)
// Prefix subsequent lines with padding the same length as the item prefix
indentLen := int(max(r.config.NestedListLength, NestedListLengthMinimum))
indent := bytes.Repeat([]byte{' '}, indentLen)
r.rc.writer.PushPrefix(bytes.Repeat(indent, len(itemPrefix)), 1)
} else {
r.rc.writer.PopPrefix()
r.rc.writer.PopPrefix()
}
return ast.WalkContinue
}
func (r *Renderer) renderRawHTML(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.RawHTML)
if entering {
r.renderSegments(n.Segments, false)
}
return ast.WalkContinue
}
func (r *Renderer) renderText(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.Text)
if entering {
text := n.Value(r.rc.source)
r.rc.writer.WriteBytes(text)
if n.SoftLineBreak() {
r.rc.writer.EndLine()
}
}
return ast.WalkContinue
}
func (r *Renderer) renderSegments(segments *text.Segments, asLines bool) {
for i := 0; i < segments.Len(); i++ {
segment := segments.At(i)
value := segment.Value(r.rc.source)
r.rc.writer.WriteBytes(value)
if asLines {
r.rc.writer.FlushLine()
}
}
}
func (r *Renderer) renderLines(node ast.Node, entering bool) ast.WalkStatus {
if entering {
lines := node.Lines()
r.renderSegments(lines, true)
}
return ast.WalkContinue
}
func (r *Renderer) renderLink(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.Link)
return r.renderLinkCommon(n.Title, n.Destination, entering)
}
func (r *Renderer) renderImage(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.Image)
if entering {
r.rc.writer.WriteBytes([]byte("!"))
}
return r.renderLinkCommon(n.Title, n.Destination, entering)
}
func (r *Renderer) renderLinkCommon(title, destination []byte, entering bool) ast.WalkStatus {
if entering {
r.rc.writer.WriteBytes([]byte("["))
} else {
r.rc.writer.WriteBytes([]byte("]("))
r.rc.writer.WriteBytes(destination)
if len(title) > 0 {
r.rc.writer.WriteBytes([]byte(" \""))
r.rc.writer.WriteBytes(title)
r.rc.writer.WriteBytes([]byte("\""))
}
r.rc.writer.WriteBytes([]byte(")"))
}
return ast.WalkContinue
}
func (r *Renderer) renderCodeSpan(node ast.Node, entering bool) ast.WalkStatus {
if entering {
// get contents of codespan
var contentBytes []byte
for c := node.FirstChild(); c != nil; c = c.NextSibling() {
text := c.(*ast.Text).Segment
contentBytes = append(contentBytes, text.Value(r.rc.source)...)
}
contents := string(contentBytes)
//
var beginsWithSpace bool
var endsWithSpace bool
var beginsWithBackTick bool
var endsWithBackTick bool
isOnlySpace := true
backtickLengths := []int{}
count := 0
for i, c := range contents {
if i == 0 {
beginsWithSpace = unicode.IsSpace(c)
beginsWithBackTick = c == '`'
} else if i == len(contents)-1 {
endsWithSpace = unicode.IsSpace(c)
endsWithBackTick = c == '`'
}
if !unicode.IsSpace(c) {
isOnlySpace = false
}
if c == '`' {
count++
} else if count > 0 {
backtickLengths = append(backtickLengths, count)
count = 0
}
}
if count > 0 {
backtickLengths = append(backtickLengths, count)
}
// Surround the codespan with the minimum number of backticks required to contain the span.
for i := 1; i <= len(contentBytes); i++ {
if !slices.Contains(backtickLengths, i) {
r.rc.codeSpanContext.backtickLength = i
break
}
}
r.rc.writer.WriteBytes(bytes.Repeat([]byte("`"), r.rc.codeSpanContext.backtickLength))
// Check if the code span needs to be padded with spaces
if beginsWithSpace && endsWithSpace && !isOnlySpace || beginsWithBackTick || endsWithBackTick {
r.rc.codeSpanContext.padSpace = true
r.rc.writer.WriteBytes([]byte(" "))
}
} else {
if r.rc.codeSpanContext.padSpace {
r.rc.writer.WriteBytes([]byte(" "))
}
r.rc.writer.WriteBytes(bytes.Repeat([]byte("`"), r.rc.codeSpanContext.backtickLength))
}
return ast.WalkContinue
}
func (r *Renderer) renderEmphasis(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.Emphasis)
r.rc.writer.WriteBytes(bytes.Repeat([]byte{'*'}, n.Level))
return ast.WalkContinue
}
type renderContext struct {
writer *markdownWriter
// source is the markdown source
source []byte
// listMarkers is the marker character used for the current list
lists []listContext
codeSpanContext codeSpanContext
}
type listContext struct {
list *ast.List
num int
}
// codeSpanContext holds state about how the current codespan should be rendererd.
type codeSpanContext struct {
// number of backticks to use
backtickLength int
// whether to surround the codespan with spaces
padSpace bool
}
// newRenderContext returns a new renderContext object
func newRenderContext(writer io.Writer, source []byte, config *Config) renderContext {
return renderContext{
writer: newMarkdownWriter(writer, config),
source: source,
}
}