-
Notifications
You must be signed in to change notification settings - Fork 126
/
from_test.go
659 lines (588 loc) · 13.8 KB
/
from_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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package md
import (
"bytes"
"flag"
"io/ioutil"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/PuerkitoBio/goquery"
)
var update = flag.Bool("update", false, "update .golden files")
func TestFromString(t *testing.T) {
/*
type rule struct {
Before func()
After func() AdvancedResult
}
type X struct {
f func(*X)
i int
}
x := X{
f: func(x *X) {
x.i = 5
},
}
fmt.Println("before", x.i)
x.f(&x)
fmt.Println("after", x.i)
*/
var tests = []struct {
name string
domain string
html string
options *Options
}{
{
name: "p tag",
html: `<p>Some Text</p>`,
},
{
name: "two p tags",
html: `
<div>
<p>Text</p>
<p>Some Text</p>
</div>
`,
},
{
name: "span in p tag",
html: "<p>Some <span>Text</span></p>",
},
{
name: "strong in p tag",
html: "<p>Some <strong>Text</strong></p>",
},
{
name: "strong in p tag with whitespace",
html: "<p> Some <strong> Text </strong></p>",
},
{
name: "h1",
html: "<h1>Header</h1>",
},
{
name: "h2",
html: "<h2>Header</h2>",
},
{
name: "h6",
html: "<h6>Header</h6>",
},
{
name: "setext h1",
html: "<h1>Header</h1>",
options: &Options{
HeadingStyle: "setext",
},
},
{
name: "setext h2",
html: "<h2>Header</h2>",
options: &Options{
HeadingStyle: "setext",
},
},
{
name: "setext h3",
html: "<h3>Header</h3>",
options: &Options{
HeadingStyle: "setext",
},
},
{
name: "ul",
html: `
<ul>
<li>Some Thing</li>
<li>Another Thing</li>
</ul>
`,
},
{
name: "ol",
html: `
<ol>
<li>First Thing</li>
<li>Second Thing</li>
</ol>
`,
},
{
name: "indent content in li",
html: `
<ul>
<li>
Indent First Thing
<p>Second Thing</p>
</li>
<li>Third Thing</li>
</ul>
`,
},
{
name: "nested list",
html: `
<ul>
<li>foo
<ul>
<li>bar
<ul>
<li>baz
<ul>
<li>boo</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
`,
},
{
name: "ul in ol",
html: `
<ol>
<li>
<p>First Thing</p>
<ul>
<li>Some Thing</li>
<li>Another Thing</li>
</ul>
</li>
<li>Second Thing</li>
</ol>
`,
},
{
name: "empty list item",
html: `
<ul>
<li>foo</li>
<li></li>
<li>bar</li>
</ul>
`,
},
{
name: "list items ending with a space",
html: `
<ul>
<li>List items </li>
<li>Ending with </li>
<li>A space </li>
</ul>
`,
},
{
name: "sup element",
html: `
<p>One of the most common equations in all of physics is
<var>E</var>=<var>m</var><var>c</var><sup>2</sup>.<p>
`,
},
{
name: "sup element in list",
html: `
<p>The ordinal number "fifth" can be abbreviated in various languages as follows:</p>
<ul>
<li>English: 5<sup>th</sup></li>
<li>French: 5<sup>ème</sup></li>
</ul>
`,
},
{
name: "image",
html: `<img alt="website favicon" src="http://commonmark.org/help/images/favicon.png" />`,
},
{
name: "link",
html: `<a href="http://commonmark.org/">Link</a>`,
},
{
name: "link with title",
html: `<a href="http://commonmark.org/" title="Some Text">Link</a>`,
},
{
name: "reference link: full",
html: `
<a href="http://commonmark.org/first">First Link</a>
<a href="http://commonmark.org/second">Second Link</a>
`,
options: &Options{
LinkStyle: "referenced",
LinkReferenceStyle: "full",
},
},
{
name: "reference link: collapsed",
html: `<a href="http://commonmark.org/">Link</a>`,
options: &Options{
LinkStyle: "referenced",
LinkReferenceStyle: "collapsed",
},
},
{
name: "reference link: shortcut",
html: `<a href="http://commonmark.org/">Link</a>`,
options: &Options{
LinkStyle: "referenced",
LinkReferenceStyle: "shortcut",
},
},
{
name: "escape strong",
html: `<p>**Not Strong**
**Still Not
Strong**</p>`,
},
{
name: "escape italic",
html: `<p>_Not Italic_</p>`,
},
{
name: "escape ordered list",
html: `<p>1. Not List 1. Not List
1. Not List</p>`,
},
{
name: "escape unordered list",
html: `<p>- Not List</p>`,
},
{
name: "pre tag",
html: `
<div>
<p>Who ate the most donuts this week?</p>
<pre><code class="language-foo+bar">Jeff 15
Sam 11
Robin 6</code></pre>
</div>
`,
},
{
name: "code tag",
html: `
<p>When <code>x = 3</code>, that means <code>x + 2 = 5</code></p>
`,
},
{
name: "hr",
html: `
<p>Some Content</p>
<hr>
</p>Other Content</p>
`,
},
{
name: "blockquote",
html: `
<blockquote>
Some Quote
Next Line
</blockquote>
`,
},
{
name: "large blockquote",
html: `
<blockquote>
<p>Allowing an unimportant mistake to pass without comment is a wonderful social grace.</p>
<p>Ideological differences are no excuse for rudeness.</p>
</blockquote>
`,
},
{
name: "turndown demo",
html: `
<h1>Turndown Demo</h1>
<p>This demonstrates <a href="https://github.com/domchristie/turndown">turndown</a> – an HTML to Markdown converter in JavaScript.</p>
<h2>Usage</h2>
<pre><code class="language-js">var turndownService = new TurndownService()
console.log(
turndownService.turndown('<h1>Hello world</h1>')
)</code></pre>
<hr />
<p>It aims to be <a href="http://commonmark.org/">CommonMark</a>
compliant, and includes options to style the output. These options include:</p>
<ul>
<li>headingStyle (setext or atx)</li>
<li>horizontalRule (*, -, or _)</li>
<li>bullet (*, -, or +)</li>
<li>codeBlockStyle (indented or fenced)</li>
<li>fence</li>
<li>emDelimiter (_ or *)</li>
<li>strongDelimiter (** or __)</li>
<li>linkStyle (inlined or referenced)</li>
<li>linkReferenceStyle (full, collapsed, or shortcut)</li>
</ul>
`,
},
{
name: "keep tag",
html: `<keep-tag><p>Content</p></keep-tag>`,
},
{
name: "remove tag",
html: `<remove-tag><p>Content</p></remove-tag>`,
},
{
/*
When a header (eg. <h3>) contains any new lines in its body, it will split the header contents
over multiple lines, breaking the header in Markdown (because in Markdown, a header just
starts with #'s and anything on the next line is not part of the header). Since in HTML
and Markdown all white space is treated the same, I chose to replace line endings with spaces.
-> https://github.com/lunny/html2md/pull/6
*/
name: "strip newlines from header",
html: `
<h3>
Header
Containing
Newlines
</h3>
`,
},
{
name: "text with whitespace",
html: `
<div id="sport_single_post-2" class="widget sport_single_post">
<h1 class="widget-title">Aktuelles</h1>
<!-- featured image -->
<div class="mosaic-block fade"><a href="http://www.bonnerruderverein.de/wp-content/uploads/2015/09/BRV-abend.jpg" class="mosaic-overlay fancybox" title="BRV-abend"></a><div class="mosaic-backdrop"><div class="corner-date">25 Mai</div><img src="http://www.bonnerruderverein.de/wp-content/uploads/2015/09/BRV-abend.jpg" alt="" /></div></div>
<!-- title -->
<h3 class="title"><a href="http://www.bonnerruderverein.de/bonner-nachtlauf/">9. Bonner Nachtlauf - Einschränkungen am Bootshaus</a></h3>
<!-- excerpt -->
am Mittwoch, dem 30. Mai 2018 findet am Bonner Rheinufer der 9. ...
<a href="http://www.bonnerruderverein.de/bonner-nachtlauf/" class="more">More</a>
</div>
<hr />
<div>
<h1 class="widget-title">Aktuelles</h1>
<h3 class="title"><a href="some_url">Title</a></h3>
<!-- excerpt -->
Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Vestibulum id ligula porta felis euismod semper.
<a href="other_url" class="more">More</a>
</div>
`,
},
{
name: "pre tag without code tag",
html: `
<div class="code"><pre>// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {</pre></div>
`,
},
/*
{ // TODO: not working yet
name: "p tag with lots of whitespace",
html: `
<p>
Sometimes a struct field, function, type, or even a whole package becomes
redundant or unnecessary, but must be kept for compatibility with existing
programs.
To signal that an identifier should not be used, add a paragraph to its doc
comment that begins with "Deprecated:" followed by some information about the
deprecation.
There are a few examples <a href="https://golang.org/search?q=Deprecated:" target="_blank">in the standard library</a>.
</p>
`,
},
*/
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
converter := NewConverter(test.domain, true, test.options)
converter.Keep("keep-tag").Remove("remove-tag")
markdown, err := converter.ConvertString(test.html)
if err != nil {
t.Error(err)
}
data := []byte(markdown)
// output := blackfriday.Run(data)
// fmt.Println(string(output))
gp := filepath.Join("testdata", strings.ReplaceAll(t.Name(), ":", "")+".golden")
if *update {
t.Log("update golden file")
if err := ioutil.WriteFile(gp, data, 0644); err != nil {
t.Fatalf("failed to update golden file: %s", err)
}
}
g, err := ioutil.ReadFile(gp)
if err != nil {
t.Logf("Result:\n'%s'\n", markdown)
t.Fatalf("failed reading .golden: %s", err)
}
if !bytes.Equal([]byte(markdown), g) {
t.Errorf("written json does not match .golden file \nexpected:\n'%s'\nbut got:\n'%s'", string(g), markdown)
}
})
}
}
func BenchmarkFromString(b *testing.B) {
converter := NewConverter("www.google.com", true, nil)
strongRule := Rule{
Filter: []string{"strong"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
return nil
},
}
var wg sync.WaitGroup
convert := func(html string) {
defer wg.Done()
_, err := converter.ConvertString(html)
if err != nil {
b.Error(err)
}
}
add := func() {
defer wg.Done()
converter.AddRules(strongRule)
}
for n := 0; n < b.N; n++ {
wg.Add(2)
go add()
go convert("<strong>Bold</strong>")
}
wg.Wait()
}
func TestAddRules_ChangeContent(t *testing.T) {
expected := "Some other Content"
var wasCalled bool
rule := Rule{
Filter: []string{"p"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
wasCalled = true
if content != "Some Content" {
t.Errorf("got wrong `content`: '%s'", content)
}
if !selec.Is("p") {
t.Error("selec is not p")
}
return String(expected)
},
}
conv := NewConverter("", true, nil)
conv.AddRules(rule)
md, err := conv.ConvertString(`<p>Some Content</p>`)
if err != nil {
t.Error(err)
}
if md != expected {
t.Errorf("wanted '%s' but got '%s'", expected, md)
}
if !wasCalled {
t.Error("rule was not called")
}
}
func TestAddRules_Fallback(t *testing.T) {
// firstExpected := "Some other Content"
expected := "Totally different Content"
var firstWasCalled bool
var secondWasCalled bool
firstRule := Rule{
Filter: []string{"p"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
firstWasCalled = true
if secondWasCalled {
t.Error("expected first rule to be called before second rule. second is already called")
}
if content != "Some Content" {
t.Errorf("got wrong `content`: '%s'", content)
}
if !selec.Is("p") {
t.Error("selec is not p")
}
return nil
},
}
secondRule := Rule{
Filter: []string{"p"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
secondWasCalled = true
if !firstWasCalled {
t.Error("expected first rule to be called before second rule. first is not called yet")
}
if content != "Some Content" {
t.Errorf("got wrong `content`: '%s'", content)
}
if !selec.Is("p") {
t.Error("selec is not p")
}
return String(expected)
},
}
conv := NewConverter("", true, nil)
conv.AddRules(secondRule, firstRule)
md, err := conv.ConvertString(`<p>Some Content</p>`)
if err != nil {
t.Error(err)
}
if md != expected {
t.Errorf("wanted '%s' but got '%s'", expected, md)
}
if !firstWasCalled {
t.Error("first rule was not called")
}
if !secondWasCalled {
t.Error("second rule was not called")
}
}
func TestWholeSite(t *testing.T) {
var tests = []struct {
name string
domain string
file string
}{
{
name: "golang.org",
domain: "golang.org",
},
{
name: "bonnerruderverein.de",
domain: "bonnerruderverein.de",
},
{
name: "blog.golang.org",
domain: "blog.golang.org",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
converter := NewConverter(test.domain, true, nil)
htmlData, err := ioutil.ReadFile(
filepath.Join("testdata", t.Name()+".html"),
)
if err != nil {
t.Error(err)
}
markdownData, err := converter.ConvertBytes(htmlData)
if err != nil {
t.Error(err)
}
// output := blackfriday.Run(data)
// fmt.Println(string(output))
gp := filepath.Join("testdata", t.Name()+".md")
if *update {
t.Log("update golden file")
if err := ioutil.WriteFile(gp, markdownData, 0644); err != nil {
t.Fatalf("failed to update golden file: %s", err)
}
}
g, err := ioutil.ReadFile(gp)
if err != nil {
t.Logf("Result:\n'%s'\n", string(markdownData))
t.Fatalf("failed reading .golden: %s", err)
}
if !bytes.Equal(markdownData, g) {
t.Errorf("written json does not match .golden file \nexpected:\n'%s'\nbut got:\n'%s'", string(g), string(markdownData))
}
})
}
}