-
Notifications
You must be signed in to change notification settings - Fork 1
/
html2md_test.go
144 lines (125 loc) · 2.64 KB
/
html2md_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
package main
import (
"bytes"
"strings"
t "testing"
)
func TestText(t *t.T) {
check(t, "some text", "some text")
}
func TestText_escape(t *t.T) {
check(t, "n\\a ~ *text*", "n\\\\a ~ \\*text\\*")
}
func TestEmph(t *t.T) {
check(t,
"<b>bold</b> <s>strikeout</s> <em>emph</em>",
"**bold** ~~strikeout~~ *emph*")
}
func TestEmph_nested(t *t.T) {
check(t,
"<b>bold & <s>strikeout</s></b> <em><b>em</b>phasis</em>",
"**bold & ~~strikeout~~** ***em**phasis*")
}
func TestEmph_code(t *t.T) {
check(t,
"<code>~~this *is* code~~</code>",
"`~~this *is* code~~`")
}
func TestP(t *t.T) {
check(t,
"<p>First paragraph</p><p>Second <span>para</span>graph</p>",
"\nFirst paragraph\n\nSecond paragraph\n")
}
func TestUl(t *t.T) {
check(t,
`<ul>
<li>first <b>item</b></li>
<li>second item with a <a href="http://example.com">link</a></li>
</ul>
`,
`
- first **item**
- second item with a [link](http://example.com) `)
}
func TestOl(t *t.T) {
check(t,
`<ol>
<li>first <b>item</b></li>
<li>second item with a <a href="http://example.com">link</a></li>
</ol>
`,
`
1. first **item**
2. second item with a [link](http://example.com) `)
}
func TestOl_Ul(t *t.T) {
check(t,
`<ol>
<li>first <b>item</b></li>
<li>second</li>
<ul><li>sub one</li><li>sub two</li></ul>
<li>third item
<ul>
<li>with a <a href="http://example.com">link</a></li>
<li>and a profit</li>
</ul>
</li>
</ol>
`,
`
1. first **item**
2. second
- sub one
- sub two
3. third item
- with a [link](http://example.com)
- and a profit `)
}
func TestH1(t *t.T) {
check(t,
"<h1>Hello!</h1>",
"\n\nHello!\n======\n")
}
func TestH1_withLink(t *t.T) {
check(t,
"<h1><a href='http://ya.ru'>Hello!</a></h1>",
"\n\n[Hello!](http://ya.ru)\n======================\n")
}
func TestH1_withLinkAndText(t *t.T) {
check(t,
"<h1>Hello <a href='http://ya.ru'>there</a>!</h1>",
"\n\nHello [there](http://ya.ru)!\n============================\n")
}
func TestH2(t *t.T) {
check(t,
"<h2>Hello!</h2>",
"\n\nHello!\n------\n")
}
func TestH4_H5(t *t.T) {
check(t,
"<h4>Section</h4><h5>Subsection</h5>",
"\n\n#### Section\n\n\n##### Subsection\n")
}
func TestH5(t *t.T) {
check(t,
"<h5>Hello!</h5>",
"\n\n##### Hello!\n")
}
func TestLink(t *t.T) {
check(t,
"<a href='http://ya.ru'>ya.ru</a>",
"[ya.ru](http://ya.ru)")
}
func TestLink_withText(t *t.T) {
check(t,
"Click <a href='http://ya.ru'>here</a> please.",
"Click [here](http://ya.ru) please.")
}
func check(t *t.T, in, out string) {
w := new(bytes.Buffer)
html2md(strings.NewReader(in), w)
res := w.String()
if out != res {
t.Errorf("\n%s\n!=\n%s", res, out)
}
}