-
Notifications
You must be signed in to change notification settings - Fork 5
/
renderer_test.go
599 lines (585 loc) · 12 KB
/
renderer_test.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package markdown
import (
"bytes"
"fmt"
"testing"
"github.com/rhysd/go-fakeio"
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var (
transformer = testHelperASTTransformer{}
md = goldmark.New(
goldmark.WithRenderer(NewRenderer()),
goldmark.WithParserOptions(parser.WithASTTransformers(util.Prioritized(&transformer, 0))),
)
)
// testHelperASTTransformer is a goldmark AST transformer that helps with debugging failed tests.
type testHelperASTTransformer struct {
lastDocument *ast.Document
}
// Transform implements goldmark.parser.ASTTransformer
func (t *testHelperASTTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
t.lastDocument = node
}
// DumpLastAST calls Node.Dump() on the most recently parsed AST and returns its output.
func (t *testHelperASTTransformer) DumpLastAST(source []byte) string {
// Node.Dump() is hardcoded to write to stdout. Fake stdout to get the output as a string.
result, err := fakeio.Stdout().Do(func() {
transformer.lastDocument.Dump(source, 0)
})
if err != nil {
panic(err)
}
return result
}
// TestRenderError tests the renderer's behavior when an error is encountered
func TestRenderError(t *testing.T) {
err := fmt.Errorf("TestRenderError")
ew := &errorWriter{err: err}
renderer := NewRenderer()
source := []byte("foo")
paragraph := ast.NewParagraph()
paragraph.SetBlankPreviousLines(true)
text := ast.NewTextSegment(text.NewSegment(0, len(source)))
paragraph.AppendChild(paragraph, text)
result := renderer.Render(ew, source, paragraph)
assert.Equal(t, err, result)
}
// TestCustomRenderers tests that the renderer uses any config.NodeRenderers defined by the user
func TestCustomRenderers(t *testing.T) {
md := goldmark.New(
goldmark.WithRenderer(NewRenderer()),
goldmark.WithParserOptions(parser.WithASTTransformers(util.Prioritized(&transformer, 0))),
)
buf := bytes.Buffer{}
source := `# My Tasks
- [x] Add support for custom renderers
`
extension.TaskList.Extend(md)
err := md.Convert([]byte(source), &buf)
assert.NoError(t, err)
t.Log(buf.String())
}
// TestRenderedOutput tests that the renderer produces the expected output for all test cases
func TestRenderedOutput(t *testing.T) {
testCases := []struct {
name string
options []Option
source string
expected string
}{
// Document
{
"Empty doc",
[]Option{},
"",
"",
},
{
"Non-empty doc trailing newline",
[]Option{},
"x",
"x\n",
},
// Headings
{
"Setext to ATX heading",
[]Option{WithHeadingStyle(HeadingStyleATX)},
"Foo\n---",
"## Foo\n",
},
{
"ATX to setext heading",
[]Option{WithHeadingStyle(HeadingStyleSetext)},
"## FooBar",
"FooBar\n---\n",
},
{
"Full width setext heading",
[]Option{WithHeadingStyle(HeadingStyleFullWidthSetext)},
"Foo Bar\n---",
"Foo Bar\n-------\n",
},
{
"ATX heading with closing sequence",
[]Option{WithHeadingStyle(HeadingStyleATXSurround)},
"## Foo",
"## Foo ##\n",
},
{
"Empty ATX heading with closing sequence",
[]Option{WithHeadingStyle(HeadingStyleATXSurround)},
"##",
"## ##\n",
},
{
// Setext headings cannot be empty, will always be ATX
"Empty setext heading",
[]Option{WithHeadingStyle(HeadingStyleSetext)},
"##",
"##\n",
},
{
// ATX headings cannot be multiline, must be setext
"Multiline ATX heading",
[]Option{WithHeadingStyle(HeadingStyleATX)},
"Foo\nBar\n---",
"Foo\nBar\n---\n",
},
// Autolink
{
"Url autolink",
[]Option{},
"<https://github.com/teekennedy/github-markdown>",
"<https://github.com/teekennedy/github-markdown>\n",
},
{
"Mailto autolink",
[]Option{},
"<foo@bar.com>",
"<foo@bar.com>\n",
},
// Blockquote
{
"Blockquote",
[]Option{},
"> You will speak\n> an infinite deal\n> of nothing\n\n\\- William Shakespeare",
"> You will speak\n> an infinite deal\n> of nothing\n\n\\- William Shakespeare\n",
},
{
"Nested blockquote",
[]Option{},
"> one\n> > two\n> > > three\n\n> one again",
"> one\n> > two\n> > > three\n\n> one again\n",
},
// Code Block
{
"Space indented code block",
[]Option{},
" foo",
" foo\n",
},
{
"Tab indented code block",
[]Option{WithIndentStyle(IndentStyleTabs)},
" foo",
"\tfoo\n",
},
{
"Multiline code block",
[]Option{WithIndentStyle(IndentStyleSpaces)},
"\tfoo\n\tbar\n\tbaz",
" foo\n bar\n baz\n",
},
// Code Span
{
"Simple code span",
[]Option{},
"`foo`",
"`foo`\n",
},
{
"Two-backtick code span",
[]Option{},
"``foo ` bar``",
"``foo ` bar``\n",
},
{
"Code span stripping leading and trailing spaces",
[]Option{},
"` `` `",
"````\n",
},
{
"Code span stripping one space",
[]Option{},
"` `` `",
"` `` `\n",
},
{
"Unstrippable left space only",
[]Option{},
"` a`",
"` a`\n",
},
{
"Unstrippable only spaces",
[]Option{},
"` `\n` `",
"` `\n` `\n",
},
{
"Line-ending treated as space",
[]Option{},
"``\nfoo \n``",
"`foo `\n",
},
{
"Backlashes are treated literally",
[]Option{},
"`foo\\`bar`",
"`foo\\`bar`\n",
},
{
"Two backticks act as delimiters",
[]Option{},
"``foo`bar``",
"``foo`bar``\n",
},
{
"Two backtics inside single ones with spaces trimmed",
[]Option{},
"` foo `` bar `",
"`foo `` bar`\n",
},
{
"Codespan backticks have precedence over emphasis",
[]Option{},
"*foo`*`",
"*foo`*`\n",
},
{
"Codespan backticks have equal precedence with HTML",
[]Option{},
"`<a href=\"`\">`",
"`<a href=\"`\">`\n",
},
{
"HTML tag with backtick",
[]Option{},
"<a href=\"`\">`",
"<a href=\"`\">`\n",
},
{
"Autolink split by a backtick",
[]Option{},
"`<http://foo.bar.`baz>`",
"`<http://foo.bar.`baz>`\n",
},
{
"Unbalanced 3-2 backticks remain intact",
[]Option{},
"```foo``",
"```foo``\n",
},
{
"Unbalanced 1-0 backticks remain intact",
[]Option{},
"`foo",
"`foo\n",
},
{
"Unbalanced double backticks",
[]Option{},
"`foo``bar``",
"`foo`bar`\n",
},
// Emphasis
{
"Emphasis",
[]Option{},
"*emph*",
"*emph*\n",
},
{
"Strong",
[]Option{},
"**strong**",
"**strong**\n",
},
{
"Strong emphasis",
[]Option{},
"***strong emph***",
"***strong emph***\n",
},
{
"Strong in emphasis",
[]Option{},
"***strong** in emph*",
"***strong** in emph*\n",
},
{
"Emphasis in strong",
[]Option{},
"***emph* in strong**",
"***emph* in strong**\n",
},
{
"Escaped emphasis",
[]Option{},
"*escaped\\*emphasis*",
"*escaped\\*emphasis*\n",
},
{
"In emphasis strong",
[]Option{},
"*in emph **strong***",
"*in emph **strong***\n",
},
// Paragraph
{
"Simple paragraph",
[]Option{},
"foo",
"foo\n",
},
{
"Paragraph with escaped characters",
[]Option{},
"\\# foo \\*bar\\* \\__baz\\_\\_",
"\\# foo \\*bar\\* \\__baz\\_\\_\n",
},
// Thematic Break
{
"Thematic break default style",
[]Option{},
"---",
"---\n",
},
{
"Thematic break underline style",
[]Option{WithThematicBreakStyle(ThematicBreakStyleUnderlined)},
"---",
"___\n",
},
{
"Thematic break starred style",
[]Option{WithThematicBreakStyle(ThematicBreakStyleStarred)},
"---",
"***\n",
},
{
// Thematic breaks are a minimum of three characters
"Thematic break zero value",
[]Option{WithThematicBreakLength(ThematicBreakLength(0))},
"---",
"---\n",
},
{
"Thematic break longer length",
[]Option{WithThematicBreakLength(ThematicBreakLength(10))},
"---",
"----------\n",
},
// Fenced Code Block
{
"Fenced Code Block",
[]Option{},
"```\nfoo\nbar\nbaz\n```",
"```\nfoo\nbar\nbaz\n```\n",
},
{
"Fenced Code Block with info",
[]Option{},
"```ruby startline=3\ndef foo(x)\n return 3\nend\n```",
"```ruby startline=3\ndef foo(x)\n return 3\nend\n```\n",
},
{
"Fenced Code Block with special chars",
[]Option{},
"```\n!@#$%^&*\\[],./;'()\n```",
"```\n!@#$%^&*\\[],./;'()\n```\n",
},
// Raw HTML
{
"Raw HTML open tags",
[]Option{},
"<a><bab><c2c>",
"<a><bab><c2c>\n",
},
{
"Raw HTML empty elements",
[]Option{},
"<a/><b2/>",
"<a/><b2/>\n",
},
{
"Raw HTML with attributes",
[]Option{},
"<a foo=\"bar\" bam = 'baz <em>\"</em>'\n_boolean zoop:33=zoop:33 />",
"<a foo=\"bar\" bam = 'baz <em>\"</em>'\n_boolean zoop:33=zoop:33 />\n",
},
// HTML blocks
{
"HTML Block Type 1",
[]Option{},
"<pre>\nfoo\n</pre>",
"<pre>\nfoo\n</pre>\n",
},
{
"HTML Block Type 2",
[]Option{},
"<!--\ncomment\n-->",
"<!--\ncomment\n-->\n",
},
{
"HTML Block Type 3",
[]Option{},
"<?\nfoo\n?>",
"<?\nfoo\n?>\n",
},
{
"HTML Block Type 4",
[]Option{},
"<!FOO\n!>",
"<!FOO\n!>\n",
},
{
"HTML Block Type 5",
[]Option{},
"<![CDATA[\nfoo\n]]>",
"<![CDATA[\nfoo\n]]>\n",
},
{
"HTML Block Type 6",
[]Option{},
"<hr />",
"<hr />\n",
},
{
"HTML Block Type 7",
[]Option{},
"</a>",
"</a>\n",
},
// Lists
{
"Unordered list",
[]Option{},
"- A1\n- B1\n - C2\n - D3\n- E1",
"- A1\n- B1\n - C2\n - D3\n- E1\n",
},
{
"Ordered list",
[]Option{},
"1. X1\n2. B1\n 1. C2\n 1. D3\n3. E1\n",
"1. X1\n2. B1\n 1. C2\n 1. D3\n3. E1\n",
},
{
"Mixed list",
[]Option{},
"1. A1\n2. B1\n - C2\n 1. D3\n 2. E3\n - F2\n - G2\n3. H1\n",
"1. A1\n2. B1\n - C2\n 1. D3\n 2. E3\n - F2\n - G2\n3. H1\n",
},
// Block separators
{
"ATX heading block separator",
[]Option{},
"# Foo\n# Bar\n\n# Baz",
"# Foo\n# Bar\n\n# Baz\n",
},
{
"Setext heading block separator",
[]Option{WithHeadingStyle(HeadingStyleSetext)},
"Foo\n---\nBar\n---\n\nBaz\n---",
"Foo\n---\nBar\n---\n\nBaz\n---\n",
},
{
"Code block separator",
[]Option{WithIndentStyle(IndentStyleTabs)},
"\tcode 1\n---\n\tcode 2\n---\n\n\tcode 3",
"\tcode 1\n---\n\tcode 2\n---\n\n\tcode 3\n",
},
{
"Fenced code block separator",
[]Option{},
"```\ncode 1\n```\n```\ncode 2\n```\n\n```\ncode 3\n```",
"```\ncode 1\n```\n```\ncode 2\n```\n\n```\ncode 3\n```\n",
},
{
"HTML block separator",
[]Option{},
"<?foo?>\n<?bar?>\n\n<?baz?>",
"<?foo?>\n<?bar?>\n\n<?baz?>\n",
},
{
"List block separator",
[]Option{},
"- foo\n+ bar\n\n* baz",
"- foo\n+ bar\n\n* baz\n",
},
{
"List item block separator",
[]Option{},
"- foo\n- bar\n\n- baz",
"- foo\n- bar\n\n- baz\n",
},
{
"Text block separator",
[]Option{},
"- foo\n- bar\n\n- baz",
"- foo\n- bar\n\n- baz\n",
},
// Tight and "loose" lists
{
"Tight list",
[]Option{},
"Paragraph\n- A1\n- B1",
"Paragraph\n- A1\n- B1\n",
},
{
"Loose list",
[]Option{},
"Paragraph\n\n- A1\n- B1",
"Paragraph\n\n- A1\n- B1\n",
},
// Links
{
"Empty Link",
[]Option{},
"[]()",
"[]()\n",
},
{
"Link",
[]Option{},
"[link](/uri)",
"[link](/uri)\n",
},
{
"Link with title",
[]Option{},
"[link](/uri \"title\")",
"[link](/uri \"title\")\n",
},
// Images
{
"Empty image",
[]Option{},
"![]()",
"![]()\n",
},
{
"Image",
[]Option{},
"![image](/uri)",
"![image](/uri)\n",
},
{
"Image with title",
[]Option{},
"![image](/uri \"title\")",
"![image](/uri \"title\")\n",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
buf := bytes.Buffer{}
renderer := NewRenderer(tc.options...)
md.SetRenderer(renderer)
err := md.Convert([]byte(tc.source), &buf)
assert.NoError(err)
assert.Equal(tc.expected, buf.String())
t.Logf("Markdown source: %q\n", tc.source)
t.Log(transformer.DumpLastAST([]byte(tc.source)))
})
}
}