-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmarkdown_test.go
More file actions
263 lines (242 loc) · 6.96 KB
/
markdown_test.go
File metadata and controls
263 lines (242 loc) · 6.96 KB
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
package markdown_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mtlynch/screenjournal/v2/markdown"
"github.com/mtlynch/screenjournal/v2/screenjournal"
)
func TestRenderBlurbAndComment(t *testing.T) {
for _, tt := range []struct {
description string
in string
out string
}{
{
"renders unformatted text",
"hello, world!",
"<p>hello, world!</p>",
},
{
"formats italics",
"hello, _world_!",
"<p>hello, <em>world</em>!</p>",
},
{
"formats bold text",
"hello, **world**!",
"<p>hello, <strong>world</strong>!</p>",
},
{
"formats multiline text",
`Instant movie of the year for me. It's such a delightful and creative way to play with the genre of musical biopics.
If you think of Weird Al as just a parody music guy, give it a chance. I was never that excited about his parody music, but I always enjoy seeing him in TV and movies.
Daniel Radcliffe is fantastic, and it's a great film role for Rainn Wilson. There are a million great cameos.
You'll like it if you enjoy things like Children's Hospital, Comedy Bang Bang, or Popstar.`,
`<p>Instant movie of the year for me. It's such a delightful and creative way to play with the genre of musical biopics.</p>
<p>If you think of Weird Al as just a parody music guy, give it a chance. I was never that excited about his parody music, but I always enjoy seeing him in TV and movies.</p>
<p>Daniel Radcliffe is fantastic, and it's a great film role for Rainn Wilson. There are a million great cameos.</p>
<p>You'll like it if you enjoy things like Children's Hospital, Comedy Bang Bang, or Popstar.</p>`,
},
{
"renders links",
"you can see it [on my blog](http://example.com/blog)",
`<p>you can see it <a href="http://example.com/blog">on my blog</a></p>`,
},
{
"adds divs for spoilers",
`Great, but predictable
!spoilers
The butler did it!`,
`<p>Great, but predictable</p>
<div class="spoilers d-none">
<p>The butler did it!</p>
</div>`,
},
{
// We don't really want this behavior, but it doesn't hurt anything right
// now, so keep the test to show the behavior.
"renders backticks",
"hello, `world`!",
"<p>hello, <code>world</code>!</p>",
},
{
// We don't really want this behavior, but it doesn't hurt anything right
// now, so keep the test to show the behavior.
"renders triple backticks",
"hello, ```world```!",
"<p>hello, <code>world</code>!</p>",
},
{
"does not render script tags",
"hello, <script>alert(1)</script>",
"<p>hello, alert(1)</p>",
},
{
"does not render images",
"check out my cat! ",
"<p>check out my cat! </p>",
},
{
"does not render HTML images",
`check out my cat! <img src="http://example.com/cat.jpg">`,
"<p>check out my cat! </p>",
},
{
"does not render pre tags for indented text",
`Remember this line?
Frank: I love pistachios...`,
`<p>Remember this line?</p>
<p>Frank: I love pistachios...</p>`,
},
} {
t.Run(tt.description, func(t *testing.T) {
if got, want := markdown.RenderBlurb(screenjournal.Blurb(tt.in)), tt.out; got != want {
t.Errorf("rendered blurb mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
if got, want := markdown.RenderComment(screenjournal.CommentText(tt.in)), tt.out; got != want {
t.Errorf("rendered comment mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
}
func TestRenderBlurbAsPlaintext(t *testing.T) {
for _, tt := range []struct {
description string
in string
out string
}{
{
"leaves unformatted text as-is",
"hello, world!",
"hello, world!",
},
{
"strips italics",
"hello, _world_!",
"hello, world!",
},
{
"strips bold formatting",
"hello, **world**!",
"hello, world!",
},
{
"leaves multiline text untouched",
`Instant movie of the year for me.
If you think of Weird Al as just a parody music guy, give it a chance. I was never that excited about his parody music, but I always enjoy seeing him in TV and movies.`,
`Instant movie of the year for me.
If you think of Weird Al as just a parody music guy, give it a chance. I was never that excited about his parody music, but I always enjoy seeing him in TV and movies.`,
},
{
"renders links",
"you can see it [on my blog](http://example.com/blog)",
`you can see it on my blog`,
},
{
"leaves apostrophes untouched",
"it's my favorite",
`it's my favorite`,
},
{
"strips backticks",
"hello, `world`!",
"hello, world!",
},
{
"strips triple backticks",
"hello, ```world```!",
"hello, world!",
},
{
"does not render script tags",
"hello, <script>alert(1)</script>",
"hello, alert(1)",
},
{
"does not render images",
"check out my cat! ",
"check out my cat!",
},
{
"does not render HTML images",
`check out my cat! <img src="http://example.com/cat.jpg">`,
"check out my cat!",
},
{
"excludes spoilers from plaintext rendering",
`Great, but predictable
!spoilers
The butler did it!`,
"Great, but predictable",
},
} {
t.Run(tt.description, func(t *testing.T) {
if got, want := markdown.RenderBlurbAsPlaintext(screenjournal.Blurb(tt.in)), tt.out; got != want {
t.Errorf("plaintext=[%s], want=[%s]", got, want)
}
})
}
}
func TestRenderEmail(t *testing.T) {
for _, tt := range []struct {
description string
in string
out string
}{
{
"renders unformatted text",
"hello, world!",
"<p>hello, world!</p>",
},
{
"formats italics",
"hello, _world_!",
"<p>hello, <em>world</em>!</p>",
},
{
"formats bold text",
"hello, **world**!",
"<p>hello, <strong>world</strong>!</p>",
},
{
"renders links",
"Check out their [latest review](http://example.com/review)",
`<p>Check out their <a href="http://example.com/review">latest review</a></p>`,
},
{
// We don't really want this behavior, but it doesn't hurt anything right
// now, so keep the test to show the behavior.
"renders backticks",
"hello, `world`!",
"<p>hello, <code>world</code>!</p>",
},
{
// We don't really want this behavior, but it doesn't hurt anything right
// now, so keep the test to show the behavior.
"renders triple backticks",
"hello, ```world```!",
"<p>hello, <code>world</code>!</p>",
},
{
"does not render script tags",
"hello, <script>alert(1)</script>",
"<p>hello, alert(1)</p>",
},
{
"does not render images",
"check out my cat! ",
"<p>check out my cat! </p>",
},
{
"does not render HTML images",
`check out my cat! <img src="http://example.com/cat.jpg">`,
"<p>check out my cat! </p>",
},
} {
t.Run(tt.description, func(t *testing.T) {
if got, want := markdown.RenderEmail(screenjournal.EmailBodyMarkdown(tt.in)), tt.out; got != want {
t.Errorf("rendered=%s, want=%s", got, want)
}
})
}
}