-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
parse_test.rb
387 lines (313 loc) · 20.8 KB
/
parse_test.rb
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
require 'test_helper'
class Markdown::ParseTest < ActiveSupport::TestCase
test "empty in is empty out" do
assert_equal "", Markdown::Parse.("\n")
end
test "converts markdown to html" do
expected = '<p>So I was split between two ways of doing this.</p>
<ol>
<li>Either method pairs with adjectives (which I did),</li>
<li>Some sort of data structure (e.g. a hash might look like)</li>
</ol>
<p><a href="http://example.com" target="_blank" rel="noopener">Some link</a></p>
<p>Watch this:</p>
<pre><code class="language-plain">$ go home
</code></pre>'
actual = Markdown::Parse.('
So I was split between two ways of doing this.
1. Either method pairs with adjectives (which I did),
2. Some sort of data structure (e.g. a hash might look like)
[Some link](http://example.com)
Watch this:
```
$ go home
```
')
assert_equal expected.chomp, actual.chomp
end
test "sanitizes bad tags" do
expected = '<p>Here is a sample of what a textarea block looks like:</p>
<iframe></iframe>
<p>Done</p>'
actual = Markdown::Parse.('
Here is a sample of what a textarea block looks like:
<iframe></iframe>
Done')
assert_equal expected.chomp, actual.chomp
end
test "allows details" do
expected = '<p>Here is a sample of what a details/summary block looks like:</p>
<details><summary>Click the little arrow to get a hint!</summary>
This is the spoiler that I want to show.</details>
<p>Done</p>'
actual = Markdown::Parse.('
Here is a sample of what a details/summary block looks like:
<details><summary>Click the little arrow to get a hint!</summary>
This is the spoiler that I want to show.</details>
Done')
assert_equal expected.chomp, actual.chomp
end
test "doesn't blow up with nil" do
assert_equal "", Markdown::Parse.(nil)
end
test "parses tables" do
table = <<~TABLE
1 | 2
--- | ---
3 | 4
TABLE
expected = <<~HTML
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
HTML
assert_equal expected, Markdown::Parse.(table)
end
test "respects rel_nofollow" do
normal = '<p><a href="http://example.com" target="_blank" rel="noopener">Some link</a></p>'
rel_nofollow = '<p><a href="http://example.com" target="_blank" rel="noopener nofollow">Some link</a></p>'
assert_equal normal.chomp, Markdown::Parse.('[Some link](http://example.com)').chomp
assert_equal rel_nofollow.chomp, Markdown::Parse.('[Some link](http://example.com)', nofollow_links: true).chomp
end
test 'adds target="blank" to external links' do
expected = '<p><a href="http://example.com" target="_blank" rel="noopener">Some link</a></p>'
assert_equal expected.chomp, Markdown::Parse.('[Some link](http://example.com)').chomp
end
test 'does not add target="blank" to relative link' do
expected = '<p><a href="./link/to/page">Some link</a></p>'
assert_equal expected.chomp, Markdown::Parse.('[Some link](./link/to/page)').chomp
end
test 'does not add target="blank" to parent relative link' do
expected = '<p><a href="../link/to/page">Some link</a></p>'
assert_equal expected.chomp, Markdown::Parse.('[Some link](../link/to/page)').chomp
end
test 'does not add target="blank" to absolute link' do
expected = '<p><a href="/link/to/page">Some link</a></p>'
assert_equal expected.chomp, Markdown::Parse.('[Some link](/link/to/page)').chomp
end
schemes = %w[https http]
domains = %w[exercism.io exercism.lol local.exercism.io exercism.org]
schemes.product(domains).each do |scheme, domain|
test "does not add target=\"blank\" to internal link on #{scheme}://#{domain}" do
expected = %(<p><a href="#{scheme}://#{domain}/tracks/ruby">Some link</a></p>)
assert_equal expected.chomp, Markdown::Parse.("[Some link](#{scheme}://#{domain}/tracks/ruby)").chomp
end
end
test 'adds rel="noopener" to external links' do
expected = '<p><a href="http://example.com" target="_blank" rel="noopener">Some link</a></p>'
assert_equal expected.chomp, Markdown::Parse.('[Some link](http://example.com)').chomp
end
test 'does not add rel="noopener" to external links with no_follow' do
expected = '<p><a href="http://example.com" target="_blank" rel="noopener nofollow">Some link</a></p>'
assert_equal expected.chomp, Markdown::Parse.('[Some link](http://example.com)', nofollow_links: true).chomp
end
test "parses double tildes as strikethrough" do
assert_equal "<p><del>Hello</del></p>\n", Markdown::Parse.("~~Hello~~")
end
test "removes level one headings by default" do
assert_equal "<p>Content</p>\n", Markdown::Parse.("# Top heading\n\nContent")
end
test "can not remove level one headings" do
assert_equal "<h2>Top heading</h2>\n<p>Content</p>\n", Markdown::Parse.("# Top heading\n\nContent", strip_h1: false)
end
test "does not remove level one headings in code blocks" do
assert_equal "<pre><code class=\"language-ruby\"># Top heading\n</code></pre>\n",
Markdown::Parse.("```ruby\n# Top heading\n```")
end
test "increment level of headings with greater than one" do
assert_equal "<h3>Level two</h3>\n<h4>Level three</h4>\n", Markdown::Parse.("## Level two\n\n### Level three")
end
test "does not lower headings beyond h6" do
str = "#### Level four\n\n##### Level five\n\n###### Level six\n\n####### Level seven"
assert_equal "<h5>Level four</h5>\n<h6>Level five</h6>\n<h6>Level six</h6>\n<p>####### Level seven</p>\n", Markdown::Parse.(str)
end
test "does not increment level of level one heading if stripping" do
assert_equal "", Markdown::Parse.("# Level one\n", strip_h1: true)
assert_equal "<h2>Level one</h2>\n", Markdown::Parse.("# Level one\n", strip_h1: false)
end
test "removes html comments" do
assert_equal "\n<p>Regular text</p>\n", Markdown::Parse.("<!-- Comment text -->\n\nRegular text\n", strip_h1: false)
end
test "render internal concept link using absolute URL" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/concepts/basics" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.org/tracks/ruby/concepts/basics)")
end
test "render internal concept link using absolute path" do
expected = %(<p><a href="/tracks/ruby/concepts/basics" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](/tracks/ruby/concepts/basics)")
end
test "render internal exercise link using absolute URL" do
skip # TODO: enable one exercise tooltips are enabled
expected = %(<p><a href="https://exercism.org/tracks/ruby/exercises/anagram" data-tooltip-type="exercise" data-endpoint="/tracks/ruby/exercises/anagram/tooltip">anagram</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[anagram](https://exercism.org/tracks/ruby/exercises/anagram)")
end
test "render internal exercise link using absolute path" do
skip # TODO: enable one exercise tooltips are enabled
expected = %(<p><a href="/tracks/ruby/exercises/anagram" data-tooltip-type="exercise" data-endpoint="/tracks/ruby/exercises/anagram/tooltip">anagram</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[anagram](/tracks/ruby/exercises/anagram)")
end
test "render internal link using exercism.org domain" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/concepts/basics" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.org/tracks/ruby/concepts/basics)")
end
test "render internal link using exercism.lol domain" do
expected = %(<p><a href="https://exercism.lol/tracks/ruby/concepts/basics" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.lol/tracks/ruby/concepts/basics)")
end
test "render internal link using local.exercism.io domain" do
expected = %(<p><a href="http://local.exercism.io/tracks/ruby/concepts/basics" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](http://local.exercism.io/tracks/ruby/concepts/basics)")
end
test "render internal link with trailing slash" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/concepts/basics/" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.org/tracks/ruby/concepts/basics/)")
end
test "render internal link without trailing slash" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/concepts/basics" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.org/tracks/ruby/concepts/basics)")
end
test "render internal link ending with hash" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/concepts/basics#intro" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.org/tracks/ruby/concepts/basics#intro)")
end
test "render internal link ending with question mark" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/concepts/basics?refresh" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.org/tracks/ruby/concepts/basics?refresh)")
end
test "render internal link with track containing special characters" do
expected = %(<p><a href="https://exercism.org/tracks/common-lisp_2-%F0%9F%8E%99/concepts/basics/" data-tooltip-type="concept" data-endpoint="/tracks/common-lisp_2-%F0%9F%8E%99/concepts/basics/tooltip">basics</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[basics](https://exercism.org/tracks/common-lisp_2-🎙/concepts/basics/)")
end
test "render internal link with concept containing special characters" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/concepts/1_2-3_%F0%9F%8E%B8" data-tooltip-type="concept" data-endpoint="/tracks/ruby/concepts/1_2-3_%F0%9F%8E%B8/tooltip">123</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[123](https://exercism.org/tracks/ruby/concepts/1_2-3_🎸)")
end
test "render internal link with exercise containing special characters" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/exercises/bank-%F0%9F%92%B0" data-tooltip-type="exercise" data-endpoint="/tracks/ruby/exercises/bank-%F0%9F%92%B0/tooltip">bank</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[bank](https://exercism.org/tracks/ruby/exercises/bank-💰)")
end
test "skip unsupported internal links" do
expected = %(<p><a href="https://exercism.org/tracks/ruby/contributors/iliketohelp">iliketohelp</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[iliketohelp](https://exercism.org/tracks/ruby/contributors/iliketohelp)")
end
test "render concept widget link without link" do
# TODO: render concept widget instead of link
expected = %(<p><a href="https://exercism.org/tracks/julia/concepts/if-statements" data-tooltip-type="concept" data-endpoint="/tracks/julia/concepts/if-statements/tooltip">if-statements</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[concept:julia/if-statements]()")
end
test "render concept widget link with link" do
# TODO: render concept widget instead of link
expected = %(<p><a href="https://exercism.org/tracks/julia/concepts/if-statements" data-tooltip-type="concept" data-endpoint="/tracks/julia/concepts/if-statements/tooltip">if-statements</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected,
Markdown::Parse.("[concept:julia/if-statements](https://exercism.org/tracks/julia/concepts/if-statements)")
end
test "render exercise widget link without link" do
# TODO: render exercise widget instead of link
expected = %(<p><a href="https://exercism.org/tracks/julia/exercises/two-fer" data-tooltip-type="exercise" data-endpoint="/tracks/julia/exercises/two-fer/tooltip">two-fer</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[exercise:julia/two-fer]()")
end
test "render exercise widget link with link" do
# TODO: render exercise widget instead of link
expected = %(<p><a href="https://exercism.org/tracks/julia/exercises/two-fer" data-tooltip-type="exercise" data-endpoint="/tracks/julia/exercises/two-fer/tooltip">two-fer</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[exercise:julia/two-fer](https://exercism.org/tracks/julia/exercises/two-fer)")
end
test "copes with a bad link uri scheme" do
# TODO: render exercise widget instead of link
expected = %(<p><a href=\"https://exercism.org/tracks/julia/exercises/two-fer\" data-tooltip-type=\"exercise\" data-endpoint=\"/tracks/julia/exercises/two-fer/tooltip\">two-fer</a></p>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[exercise:julia/two-fer](+https://exercism.org/tracks/julia/exercises/two-fer)")
end
test "render vimeo video without link" do
expected = %(<div style="padding:56.25% 0 0 0;position:relative;"><iframe src="https://player.vimeo.com/video/595885125?badge=0&autopause=0&player_id=0&app_id=58479" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen style="position:absolute;top:0;left:0;width:100%;height:100%;" title="X Exercism_ Tutorial Your first mentoring session 1.m4v"></iframe></div><script src="https://player.vimeo.com/api/player.js"></script>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[video:vimeo/595885125]()")
end
test "render vimeo video with link" do
expected = %(<div style="padding:56.25% 0 0 0;position:relative;"><iframe src="https://player.vimeo.com/video/595885125?badge=0&autopause=0&player_id=0&app_id=58479" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen style="position:absolute;top:0;left:0;width:100%;height:100%;" title="X Exercism_ Tutorial Your first mentoring session 1.m4v"></iframe></div><script src="https://player.vimeo.com/api/player.js"></script>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("[video:vimeo/595885125](https://player.vimeo.com/video/595885125)")
end
test "render note code block" do
expected = %(<div class="c-textblock-note">\n<div class="c-textblock-header">Note</div>\n<div class="c-textblock-content">\n<p>Note this</p>\n</div>\n</div>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("```exercism/note\nNote this\n```")
assert_equal expected, Markdown::Parse.("`````exercism/note\nNote this\n`````")
assert_equal expected, Markdown::Parse.("~~~~~exercism/note\nNote this\n~~~~~")
end
test "render caution code block" do
expected = %(<div class="c-textblock-caution">\n<div class="c-textblock-header">Caution</div>\n<div class="c-textblock-content">\n<p>Here be dragons</p>\n</div>\n</div>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("```exercism/caution\nHere be dragons\n```")
assert_equal expected, Markdown::Parse.("`````exercism/caution\nHere be dragons\n`````")
assert_equal expected, Markdown::Parse.("~~~~~exercism/caution\nHere be dragons\n~~~~~")
end
test "render advanced code block" do
expected = %(<div class="c-textblock-advanced">\n<div class="c-textblock-header">Advanced</div>\n<div class="c-textblock-content">\n<p>Pointer arithmetic</p>\n</div>\n</div>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("```exercism/advanced\nPointer arithmetic\n```")
assert_equal expected, Markdown::Parse.("`````exercism/advanced\nPointer arithmetic\n`````")
assert_equal expected, Markdown::Parse.("~~~~~exercism/advanced\nPointer arithmetic\n~~~~~")
end
test "render note block with markdown note" do
expected = %(<div class="c-textblock-note">\n<div class="c-textblock-header">Note</div>\n<div class="c-textblock-content">\n<p>There is <strong>markdown</strong> within <em>these</em> notes.</p>\n</div>\n</div>\n) # rubocop:disable Layout/LineLength
assert_equal expected, Markdown::Parse.("```exercism/note\nThere is **markdown** within _these_ notes.\n```")
assert_equal expected, Markdown::Parse.("`````exercism/note\nThere is **markdown** within _these_ notes.\n`````")
assert_equal expected, Markdown::Parse.("~~~~~exercism/note\nThere is **markdown** within _these_ notes.\n~~~~~")
end
test "render footnote" do
expected = %(<p>Hello<sup class=\"footnote-ref\"><a href=\"#fn1\" id=\"fnref1\">1</a></sup>.</p>\n<section class=\"footnotes\">\n<ol>\n<li id=\"fn1\">\n<p>Hey! <a href=\"#fnref1\" class=\"footnote-backref\">↩</a></p>\n</li>\n</ol>\n</section>\n) # rubocop:disable Layout/LineLength
assert_equal expected,
Markdown::Parse.("Hello[^hi].\n\n[^hi]: Hey!\n")
end
test "heading id for lowercase letters title" do
expected = %(<h2 id="h-lowerletters">lowerletters</h2>\n)
assert_equal expected, Markdown::Parse.("## lowerletters", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id for uppercase letters title is converted to lowercase" do
expected = %(<h2 id="h-upperletters">UPPERLETTERS</h2>\n)
assert_equal expected, Markdown::Parse.("## UPPERLETTERS", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id for mixed-case letters title is converted to lowercase" do
expected = %(<h2 id="h-pascalcase">PascalCase</h2>\n)
assert_equal expected, Markdown::Parse.("## PascalCase", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id for diacritic letters title are converted to regular letters or dashes" do
expected = %(<h2 id="h-o-a-eeee">Òǒốáȧëèéê</h2>\n)
assert_equal expected, Markdown::Parse.("## Òǒốáȧëèéê", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id for numbers title" do
expected = %(<h2 id="h-123456">123456</h2>\n)
assert_equal expected, Markdown::Parse.("## 123456", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id with non-letter/digit characters title are replaced with dashes" do
expected = %(<h2 id="h-this-is-m">this is m*</h2>\n)
assert_equal expected, Markdown::Parse.("## this is m*", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id for code fence title" do
expected = %(<h2 id="h-summary"><code>summary</code></h2>\n)
assert_equal expected, Markdown::Parse.("## `summary`", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id for markdown title" do
expected = %(<h2 id="h-why-is-it-not-e-g-must">Why is it not ... (e.g. <strong>MUST</strong>)?</h2>\n)
assert_equal expected, Markdown::Parse.("## Why is it not ... (e.g. **MUST**)?", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading id with consecutive non-letter/digit characters title are replaced with dashes" do
expected = %(<h2 id="h-this-is-m">this % is @@ m</h2>\n)
assert_equal expected, Markdown::Parse.("## this % is @@ m", heading_ids: true, lower_heading_levels_by: 0)
end
test "heading ids support all heading levels" do
expected = %(<h1 id="h-one">one</h1>\n<h2 id="h-two">two</h2>\n<h3 id="h-three">three</h3>\n<h4 id="h-four">four</h4>\n<h5 id="h-five">five</h5>\n<h6 id="h-six">six</h6>\n) # rubocop:disable Layout/LineLength
assert_equal expected,
Markdown::Parse.("# one\n\n## two\n\n### three\n\n#### four\n\n##### five\n\n###### six", heading_ids: true,
lower_heading_levels_by: 0, strip_h1: false)
end
test "heading id for same titles uses sequential numbering" do
expected = %(<h2 id="h-my-title">my title</h2>\n<h2 id="h-my-title-1">my title</h2>\n<h2 id="h-my-title-2">my title</h2>\n)
assert_equal expected, Markdown::Parse.("## my title\n\n## my title\n\n## my title", heading_ids: true, lower_heading_levels_by: 0)
end
end