forked from remarkjs/react-markdown
/
test.jsx
1408 lines (1239 loc) · 44 KB
/
test.jsx
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Position} Position
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Text} Text
* @typedef {import('react').ReactNode} ReactNode
* @typedef {import('../index.js').Components} Components
*/
import fs from 'node:fs'
import path from 'node:path'
import {test} from 'uvu'
import * as assert from 'uvu/assert'
import React from 'react'
import gfm from 'remark-gfm'
import {visit} from 'unist-util-visit'
import raw from 'rehype-raw'
import toc from 'remark-toc'
import ReactDom from 'react-dom/server.js'
import Markdown from '../index.js'
const own = {}.hasOwnProperty
/**
* @param {ReturnType<Markdown>} input
* @returns {string}
*/
function asHtml(input) {
return ReactDom.renderToStaticMarkup(input)
}
test('can render the most basic of documents (single paragraph)', () => {
assert.equal(asHtml(<Markdown>Test</Markdown>), '<p>Test</p>')
})
test('should warn when passed `source`', () => {
const warn = console.warn
/** @type {unknown} */
let message
console.warn = (/** @type {unknown} */ d) => {
message = d
}
// @ts-expect-error runtime
assert.equal(asHtml(<Markdown source="a">b</Markdown>), '<p>b</p>')
assert.equal(
message,
'[react-markdown] Warning: please use `children` instead of `source` (see <https://github.com/remarkjs/react-markdown/blob/main/changelog.md#change-source-to-children> for more info)'
)
console.warn = warn
})
test('should warn when passed non-string children (number)', () => {
const {error, warn} = console
/** @type {unknown} */
let message
console.error = () => {}
console.warn = (/** @type {unknown} */ d) => {
message = d
}
// @ts-expect-error runtime
assert.equal(asHtml(<Markdown children={1} />), '')
assert.equal(
message,
'[react-markdown] Warning: please pass a string as `children` (not: `1`)'
)
console.error = error
console.warn = warn
})
test('should warn when passed non-string children (boolean)', () => {
const {error, warn} = console
/** @type {unknown} */
let message
console.error = () => {}
console.warn = (/** @type {unknown} */ d) => {
message = d
}
// @ts-expect-error runtime
assert.equal(asHtml(<Markdown children={false} />), '')
assert.equal(
message,
'[react-markdown] Warning: please pass a string as `children` (not: `false`)'
)
console.error = error
console.warn = warn
})
test('should not warn when passed `null` as children', () => {
// @ts-expect-error: types do not allow `null`.
assert.equal(asHtml(<Markdown children={null} />), '')
})
test('should not warn when passed `undefined` as children', () => {
// @ts-expect-error: types do not allow `undefined`.
assert.equal(asHtml(<Markdown children={undefined} />), '')
})
test('should warn when passed `allowDangerousHtml`', () => {
const warn = console.warn
/** @type {unknown} */
let message
console.warn = (/** @type {unknown} */ d) => {
message = d
}
// @ts-expect-error runtime
assert.equal(asHtml(<Markdown allowDangerousHtml>a</Markdown>), '<p>a</p>')
assert.equal(
message,
'[react-markdown] Warning: please remove `allowDangerousHtml` (see <https://github.com/remarkjs/react-markdown/blob/main/changelog.md#remove-buggy-html-in-markdown-parser> for more info)'
)
console.warn = warn
})
test('uses passed classname for root component', () => {
assert.equal(
asHtml(<Markdown className="md">Test</Markdown>),
'<div class="md"><p>Test</p></div>'
)
})
test('should handle multiple paragraphs properly', () => {
const input = 'React is awesome\nAnd so is markdown\n\nCombining = epic'
assert.equal(
asHtml(<Markdown children={input} />),
'<p>React is awesome\nAnd so is markdown</p>\n<p>Combining = epic</p>'
)
})
test('should handle multiline paragraphs properly (softbreak, paragraphs)', () => {
const input = 'React is awesome\nAnd so is markdown \nCombining = epic'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>React is awesome\nAnd so is markdown<br/>\nCombining = epic</p>'
)
})
test('should handle emphasis', () => {
const input = 'React is _totally_ *awesome*'
const actual = asHtml(<Markdown children={input} />)
assert.equal(actual, '<p>React is <em>totally</em> <em>awesome</em></p>')
})
test('should handle bold/strong text', () => {
const input = 'React is __totally__ **awesome**'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>React is <strong>totally</strong> <strong>awesome</strong></p>'
)
})
test('should handle links without title attribute', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/">a link</a> to Espen.Codes.</p>'
)
})
test('should handle links with title attribute', () => {
const input =
'This is [a link](https://espen.codes/ "some title") to Espen.Codes.'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" title="some title">a link</a> to Espen.Codes.</p>'
)
})
test('should handle links with uppercase protocol', () => {
const input = 'This is [a link](HTTPS://ESPEN.CODES/) to Espen.Codes.'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>This is <a href="HTTPS://ESPEN.CODES/">a link</a> to Espen.Codes.</p>'
)
})
test('should handle links with custom uri transformer', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const actual = asHtml(
<Markdown
children={input}
transformLinkUri={(uri) => uri.replace(/^https?:/, '')}
/>
)
assert.equal(
actual,
'<p>This is <a href="//espen.codes/">a link</a> to Espen.Codes.</p>'
)
})
test('should handle empty links with custom uri transformer', () => {
const input = 'Empty: []()'
const actual = asHtml(
<Markdown
children={input}
transformLinkUri={(uri, _, title) => {
assert.equal(uri, '', '`uri` should be an empty string')
assert.equal(title, null, '`title` should be null')
return ''
}}
/>
)
assert.equal(actual, '<p>Empty: <a href=""></a></p>')
})
test('should handle titles of links', () => {
const input = 'Empty: [](# "x")'
const actual = asHtml(<Markdown children={input} />)
assert.equal(actual, '<p>Empty: <a href="#" title="x"></a></p>')
})
test('should use target attribute for links if specified', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const actual = asHtml(<Markdown children={input} linkTarget="_blank" />)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" target="_blank">a link</a> to Espen.Codes.</p>'
)
})
test('should call function to get target attribute for links if specified', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const actual = asHtml(
<Markdown
children={input}
linkTarget={(uri) => (uri.startsWith('http') ? '_blank' : undefined)}
/>
)
assert.equal(
actual,
'<p>This is <a href="https://espen.codes/" target="_blank">a link</a> to Espen.Codes.</p>'
)
})
test('should handle links with custom target transformer', () => {
const input = 'Empty: []()'
const actual = asHtml(
<Markdown
children={input}
linkTarget={(uri, _, title) => {
assert.equal(uri, '', '`uri` should be an empty string')
assert.equal(title, null, '`title` should be null')
return undefined
}}
/>
)
assert.equal(actual, '<p>Empty: <a href=""></a></p>')
})
test('should handle links w/ titles with custom target transformer', () => {
const input = 'Empty: [](a "b")'
const actual = asHtml(
<Markdown
children={input}
linkTarget={(_, _1, title) => {
assert.equal(title, 'b', '`title` should be given')
return undefined
}}
/>
)
assert.equal(actual, '<p>Empty: <a href="a" title="b"></a></p>')
})
test('should support images without alt, url, or title', () => {
const input = '![]()'
const actual = asHtml(<Markdown children={input} transformLinkUri={null} />)
const expected = '<p><img src="" alt=""/></p>'
assert.equal(actual, expected)
})
test('should handle images without title attribute', () => {
const input = 'This is ![an image](/ninja.png).'
const actual = asHtml(<Markdown children={input} />)
assert.equal(actual, '<p>This is <img src="/ninja.png" alt="an image"/>.</p>')
})
test('should handle images with title attribute', () => {
const input = 'This is ![an image](/ninja.png "foo bar").'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>This is <img src="/ninja.png" alt="an image" title="foo bar"/>.</p>'
)
})
test('should handle images with custom uri transformer', () => {
const input = 'This is ![an image](/ninja.png).'
const actual = asHtml(
<Markdown
children={input}
transformImageUri={(uri) => uri.replace(/\.png$/, '.jpg')}
/>
)
assert.equal(actual, '<p>This is <img src="/ninja.jpg" alt="an image"/>.</p>')
})
test('should handle images with custom uri transformer', () => {
const input = 'Empty: ![]()'
const actual = asHtml(
<Markdown
children={input}
transformImageUri={(uri, alt, title) => {
assert.equal(uri, '', '`uri` should be an empty string')
assert.equal(alt, '', '`alt` should be an empty string')
assert.equal(title, null, '`title` should be null')
return ''
}}
/>
)
assert.equal(actual, '<p>Empty: <img src="" alt=""/></p>')
})
test('should handle images w/ titles with custom uri transformer', () => {
const input = 'Empty: ![](a "b")'
const actual = asHtml(
<Markdown
children={input}
transformImageUri={(src, _1, title) => {
assert.equal(title, 'b', '`title` should be passed')
return src
}}
/>
)
assert.equal(actual, '<p>Empty: <img src="a" alt="" title="b"/></p>')
})
test('should handle image references with custom uri transformer', () => {
const input =
'This is ![The Waffle Ninja][ninja].\n\n[ninja]: https://some.host/img.png'
const actual = asHtml(
<Markdown
children={input}
transformImageUri={(uri) => uri.replace(/\.png$/, '.jpg')}
/>
)
assert.equal(
actual,
'<p>This is <img src="https://some.host/img.jpg" alt="The Waffle Ninja"/>.</p>'
)
})
test('should support images references without alt, url, or title', () => {
const input = '![][a]\n\n[a]: <>'
const actual = asHtml(<Markdown children={input} transformLinkUri={null} />)
const expected = '<p><img src="" alt=""/></p>'
assert.equal(actual, expected)
})
test('should handle images with special characters in alternative text', () => {
const input = "This is ![a ninja's image](/ninja.png)."
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>This is <img src="/ninja.png" alt="a ninja's image"/>.</p>'
)
})
test('should be able to render headers', () => {
assert.equal(asHtml(<Markdown children="# Awesome" />), '<h1>Awesome</h1>')
assert.equal(asHtml(<Markdown children="## Awesome" />), '<h2>Awesome</h2>')
assert.equal(asHtml(<Markdown children="### Awesome" />), '<h3>Awesome</h3>')
assert.equal(asHtml(<Markdown children="#### Awesome" />), '<h4>Awesome</h4>')
assert.equal(
asHtml(<Markdown children="##### Awesome" />),
'<h5>Awesome</h5>'
)
})
test('should be able to render inline code', () => {
const input = 'Just call `renderToStaticMarkup()`, already'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>Just call <code>renderToStaticMarkup()</code>, already</p>'
)
})
test('should handle code tags without any language specification', () => {
const input = "```\nvar foo = require('bar');\nfoo();\n```"
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<pre><code>var foo = require('bar');\nfoo();\n</code></pre>'
)
})
test('should handle code tags with language specification', () => {
const input = "```js\nvar foo = require('bar');\nfoo();\n```"
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<pre><code class="language-js">var foo = require('bar');\nfoo();\n</code></pre>'
)
})
test('should only use first language definition on code blocks', () => {
const input = "```js foo bar\nvar foo = require('bar');\nfoo();\n```"
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<pre><code class="language-js">var foo = require('bar');\nfoo();\n</code></pre>'
)
})
test('should support character references in code blocks', () => {
const input = `~~~js
ololo
i
can
haz
class
names
!@#$%^&*()_
woop
~~~`
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<pre><code class="language-js\nololo\ni\ncan\nhaz\nclass\nnames\n!@#$%^&*()_"> woop\n</code></pre>'
)
})
test('should handle code blocks by indentation', () => {
const input = [
'',
'<footer class="footer">\n',
'',
'© 2014 Foo Bar\n',
'</footer>'
].join(' ')
assert.equal(
asHtml(<Markdown children={input} />),
'<pre><code><footer class="footer">\n &copy; 2014 Foo Bar\n</footer>\n</code></pre>'
)
})
test('should handle blockquotes', () => {
const input = '> Moo\n> Tools\n> FTW\n'
const actual = asHtml(<Markdown children={input} />)
assert.equal(actual, '<blockquote>\n<p>Moo\nTools\nFTW</p>\n</blockquote>')
})
test('should handle nested blockquotes', () => {
const input = [
'> > Lots of ex-Mootoolers on the React team\n>\n',
"> Totally didn't know that.\n>\n",
"> > There's a reason why it turned out so awesome\n>\n",
"> Haha I guess you're right!"
].join('')
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<blockquote>\n<blockquote>\n<p>Lots of ex-Mootoolers on the React team</p>\n</blockquote>\n<p>Totally didn't know that.</p>\n<blockquote>\n<p>There's a reason why it turned out so awesome</p>\n</blockquote>\n<p>Haha I guess you're right!</p>\n</blockquote>'
)
})
test('should handle tight, unordered lists', () => {
const input = '* Unordered\n* Lists\n* Are cool\n'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<ul>\n<li>Unordered</li>\n<li>Lists</li>\n<li>Are cool</li>\n</ul>'
)
})
test('should handle loose, unordered lists', () => {
const input = '- foo\n\n- bar'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<ul>\n<li>\n<p>foo</p>\n</li>\n<li>\n<p>bar</p>\n</li>\n</ul>'
)
})
test('should handle tight, unordered lists with sublists', () => {
const input = '* Unordered\n * Lists\n * Are cool\n'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<ul>\n<li>Unordered\n<ul>\n<li>Lists\n<ul>\n<li>Are cool</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>'
)
})
test('should handle loose, unordered lists with sublists', () => {
const input = '- foo\n\n - bar'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<ul>\n<li>\n<p>foo</p>\n<ul>\n<li>bar</li>\n</ul>\n</li>\n</ul>'
)
})
test('should handle ordered lists', () => {
const input = '1. Ordered\n2. Lists\n3. Are cool\n'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<ol>\n<li>Ordered</li>\n<li>Lists</li>\n<li>Are cool</li>\n</ol>'
)
})
test('should handle ordered lists with a start index', () => {
const input = '7. Ordered\n8. Lists\n9. Are cool\n'
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<ol start="7">\n<li>Ordered</li>\n<li>Lists</li>\n<li>Are cool</li>\n</ol>'
)
})
test('should pass `ordered`, `depth`, `checked`, `index` to list/listItem', () => {
const input = '- foo\n\n 2. bar\n 3. baz\n\n- root\n'
const actual = asHtml(
<Markdown
children={input}
components={{
li({node, ordered, checked, index, ...props}) {
assert.equal(typeof ordered, 'boolean')
assert.equal(checked, null)
assert.equal(index >= 0, true)
return React.createElement('li', props)
},
ol({node, ordered, depth, ...props}) {
assert.equal(ordered, true)
assert.equal(depth >= 0, true)
return React.createElement('ol', props)
},
ul({node, ordered, depth, ...props}) {
assert.equal(ordered, false)
assert.equal(depth >= 0, true)
return React.createElement('ul', props)
}
}}
/>
)
assert.equal(
actual,
'<ul>\n<li>\n<p>foo</p>\n<ol start="2">\n<li>bar</li>\n<li>baz</li>\n</ol>\n</li>\n<li>\n<p>root</p>\n</li>\n</ul>'
)
})
test('should pass `inline: true` to inline code', () => {
const input = '```\na\n```\n\n\tb\n\n`c`'
const actual = asHtml(
<Markdown
children={input}
components={{
code({node, inline, ...props}) {
assert.equal(inline === undefined || inline === true, true)
return React.createElement('code', props)
}
}}
/>
)
const expected =
'<pre><code>a\n</code></pre>\n<pre><code>b\n</code></pre>\n<p><code>c</code></p>'
assert.equal(actual, expected)
})
test('should pass `isHeader: boolean` to `tr`s', () => {
const input = '| a |\n| - |\n| b |\n| c |'
const actual = asHtml(
<Markdown
children={input}
remarkPlugins={[gfm]}
components={{
tr({node, isHeader, ...props}) {
assert.equal(typeof isHeader === 'boolean', true)
return React.createElement('tr', props)
}
}}
/>
)
const expected =
'<table><thead><tr><th>a</th></tr></thead><tbody><tr><td>b</td></tr><tr><td>c</td></tr></tbody></table>'
assert.equal(actual, expected)
})
test('should pass `isHeader: true` to `th`s, `isHeader: false` to `td`s', () => {
const input = '| a |\n| - |\n| b |\n| c |'
const actual = asHtml(
<Markdown
children={input}
remarkPlugins={[gfm]}
components={{
th({node, isHeader, ...props}) {
assert.equal(isHeader, true)
return React.createElement('th', props)
},
td({node, isHeader, ...props}) {
assert.equal(isHeader, false)
return React.createElement('td', props)
}
}}
/>
)
const expected =
'<table><thead><tr><th>a</th></tr></thead><tbody><tr><td>b</td></tr><tr><td>c</td></tr></tbody></table>'
assert.equal(actual, expected)
})
test('should pass `index: number`, `ordered: boolean`, `checked: boolean | null` to `li`s', () => {
const input = '* [x] a\n* [ ] b\n* c'
let count = 0
const actual = asHtml(
<Markdown
children={input}
remarkPlugins={[gfm]}
components={{
li({node, checked, index, ordered, ...props}) {
assert.equal(index, count)
assert.equal(ordered, false)
assert.equal(checked, count === 0 ? true : count === 1 ? false : null)
count++
return React.createElement('li', props)
}
}}
/>
)
const expected =
'<ul class="contains-task-list">\n<li class="task-list-item"><input type="checkbox" checked="" disabled=""/> a</li>\n<li class="task-list-item"><input type="checkbox" disabled=""/> b</li>\n<li>c</li>\n</ul>'
assert.equal(actual, expected)
})
test('should pass `level: number` to `h1`, `h2`, ...', () => {
const input = '#\n##\n###'
/**
* @param {object} props
* @param {Element} props.node
* @param {number} props.level
*/
function heading({node, level, ...props}) {
return React.createElement(`h${level}`, props)
}
const actual = asHtml(
<Markdown
children={input}
components={{h1: heading, h2: heading, h3: heading}}
/>
)
const expected = '<h1></h1>\n<h2></h2>\n<h3></h3>'
assert.equal(actual, expected)
})
test('should skip inline html with skipHtml option enabled', () => {
const input = 'I am having <strong>so</strong> much fun'
const actual = asHtml(<Markdown children={input} skipHtml />)
assert.equal(actual, '<p>I am having so much fun</p>')
})
test('should escape html blocks by default', () => {
const input = [
'This is a regular paragraph.\n\n<table>\n <tr>\n ',
'<td>Foo</td>\n </tr>\n</table>\n\nThis is another',
' regular paragraph.'
].join('')
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>This is a regular paragraph.</p>\n<table>\n <tr>\n <td>Foo</td>\n </tr>\n</table>\n<p>This is another regular paragraph.</p>'
)
})
test('should skip html blocks if skipHtml prop is set', () => {
const input = [
'This is a regular paragraph.\n\n<table>\n <tr>\n ',
'<td>Foo</td>\n </tr>\n</table>\n\nThis is another',
' regular paragraph.'
].join('')
const actual = asHtml(<Markdown children={input} skipHtml />)
assert.equal(
actual,
'<p>This is a regular paragraph.</p>\n\n<p>This is another regular paragraph.</p>'
)
})
test('should escape html blocks by default (with HTML parser plugin)', () => {
const input = [
'This is a regular paragraph.\n\n<table>\n <tr>\n ',
'<td>Foo</td>\n </tr>\n</table>\n\nThis is another',
' regular paragraph.'
].join('')
const actual = asHtml(<Markdown children={input} />)
assert.equal(
actual,
'<p>This is a regular paragraph.</p>\n<table>\n <tr>\n <td>Foo</td>\n </tr>\n</table>\n<p>This is another regular paragraph.</p>'
)
})
test('should handle horizontal rules', () => {
const input = 'Foo\n\n------------\n\nBar'
const actual = asHtml(<Markdown children={input} />)
assert.equal(actual, '<p>Foo</p>\n<hr/>\n<p>Bar</p>')
})
test('should set source position attributes if sourcePos option is enabled', () => {
const input = 'Foo\n\n------------\n\nBar'
const actual = asHtml(<Markdown children={input} sourcePos />)
assert.equal(
actual,
'<p data-sourcepos="1:1-1:4">Foo</p>\n<hr data-sourcepos="3:1-3:13"/>\n<p data-sourcepos="5:1-5:4">Bar</p>'
)
})
test('should pass on raw source position to non-tag components if rawSourcePos option is enabled', () => {
const input = '*Foo*\n\n------------\n\n__Bar__'
const actual = asHtml(
<Markdown
children={input}
rawSourcePos
components={{
em({node, sourcePosition, ...props}) {
assert.equal(sourcePosition, {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 6, offset: 5}
})
return <em className="custom" {...props} />
}
}}
/>
)
assert.equal(
actual,
'<p><em class="custom">Foo</em></p>\n<hr/>\n<p><strong>Bar</strong></p>'
)
})
test('should pass on raw source position to non-tag components if rawSourcePos option is enabled and `rehype-raw` is used', () => {
const input = '*Foo*'
asHtml(
<Markdown
children={input}
rawSourcePos
rehypePlugins={[raw]}
components={{
// @ts-expect-error JSX types currently only handle element returns not string returns
em({sourcePosition}) {
assert.equal(sourcePosition, {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 6, offset: 5}
})
return ''
}
}}
/>
)
})
test('should skip nodes that are not defined as allowed', () => {
const input =
'# Header\n\nParagraph\n## New header\n1. List item\n2. List item 2'
const actual = asHtml(
<Markdown children={input} allowedElements={['p', 'ol', 'li']} />
)
assert.equal(
actual,
'\n<p>Paragraph</p>\n\n<ol>\n<li>List item</li>\n<li>List item 2</li>\n</ol>'
)
})
test('should skip nodes that are defined as disallowed', () => {
const input =
'# Header\n\nParagraph\n## New header\n1. List item\n2. List item 2\n\nFoo'
const actual = asHtml(
<Markdown children={input} disallowedElements={['li']} />
)
assert.equal(
actual,
'<h1>Header</h1>\n<p>Paragraph</p>\n<h2>New header</h2>\n<ol>\n\n\n</ol>\n<p>Foo</p>'
)
})
test('should unwrap child nodes from disallowed nodes, if unwrapDisallowed option is enabled', () => {
const input =
'Espen *~~initiated~~ had the initial commit*, but has had several **contributors**'
const actual = asHtml(
<Markdown
children={input}
unwrapDisallowed
disallowedElements={['em', 'strong']}
remarkPlugins={[gfm]}
/>
)
assert.equal(
actual,
'<p>Espen <del>initiated</del> had the initial commit, but has had several contributors</p>'
)
})
test('should render tables', () => {
const input = [
'Languages are fun, right?',
'',
'| ID | English | Norwegian | Italian |',
'| :-- | :-----: | --------: | ------- |',
'| 1 | one | en | uno |',
'| 2 | two | to | due |',
'| 3 | three | tre | tre |',
''
].join('\n')
assert.equal(
asHtml(<Markdown children={input} remarkPlugins={[gfm]} />),
'<p>Languages are fun, right?</p>\n<table><thead><tr><th style="text-align:left">ID</th><th style="text-align:center">English</th><th style="text-align:right">Norwegian</th><th>Italian</th></tr></thead><tbody><tr><td style="text-align:left">1</td><td style="text-align:center">one</td><td style="text-align:right">en</td><td>uno</td></tr><tr><td style="text-align:left">2</td><td style="text-align:center">two</td><td style="text-align:right">to</td><td>due</td></tr><tr><td style="text-align:left">3</td><td style="text-align:center">three</td><td style="text-align:right">tre</td><td>tre</td></tr></tbody></table>'
)
})
test('should render partial tables', () => {
const input = 'User is writing a table by hand\n\n| Test | Test |\n|-|-|'
assert.equal(
asHtml(<Markdown children={input} remarkPlugins={[gfm]} />),
'<p>User is writing a table by hand</p>\n<table><thead><tr><th>Test</th><th>Test</th></tr></thead></table>'
)
})
test('should render link references', () => {
const input = [
'Stuff were changed in [1.1.4]. Check out the changelog for reference.',
'',
'[1.1.4]: https://github.com/remarkjs/react-markdown/compare/v1.1.3...v1.1.4'
].join('\n')
assert.equal(
asHtml(<Markdown children={input} />),
'<p>Stuff were changed in <a href="https://github.com/remarkjs/react-markdown/compare/v1.1.3...v1.1.4">1.1.4</a>. Check out the changelog for reference.</p>'
)
})
test('should render empty link references', () => {
const input =
'Stuff were changed in [][]. Check out the changelog for reference.'
assert.equal(
asHtml(<Markdown children={input} />),
'<p>Stuff were changed in [][]. Check out the changelog for reference.</p>'
)
})
test('should render image references', () => {
const input = [
'Checkout out this ninja: ![The Waffle Ninja][ninja]. Pretty neat, eh?',
'',
'[ninja]: /assets/ninja.png'
].join('\n')
assert.equal(
asHtml(<Markdown children={input} />),
'<p>Checkout out this ninja: <img src="/assets/ninja.png" alt="The Waffle Ninja"/>. Pretty neat, eh?</p>'
)
})
test('should support definitions with funky keys', () => {
const input =
'[][__proto__] and [][constructor]\n\n[__proto__]: a\n[constructor]: b'
const actual = asHtml(<Markdown children={input} transformLinkUri={null} />)
const expected = '<p><a href="a"></a> and <a href="b"></a></p>'
assert.equal(actual, expected)
})
test('should support duplicate definitions', () => {
const input = '[a][]\n\n[a]: b\n[a]: c'
const actual = asHtml(<Markdown children={input} transformLinkUri={null} />)
const expected = '<p><a href="b">a</a></p>'
assert.equal(actual, expected)
})
test('should skip nodes that are defined as disallowed', () => {
/** @type {Record<string, {input: string, shouldNotContain: string}>} */
const samples = {
p: {input: 'Paragraphs are cool', shouldNotContain: 'Paragraphs are cool'},
h1: {input: '# Headers are neat', shouldNotContain: 'Headers are neat'},
br: {input: 'Text \nHardbreak', shouldNotContain: '<br/>'},
a: {
input: "[Espen's blog](http://espen.codes/) yeh?",
shouldNotContain: '<a'
},
img: {input: 'Holy ![ninja](/ninja.png), batman', shouldNotContain: '<img'},
em: {input: 'Many *contributors*', shouldNotContain: '<em'},
code: {
input: "```\nvar moo = require('bar');\nmoo();\n```",
shouldNotContain: '<pre><code>'
},
blockquote: {
input: '> Moo\n> Tools\n> FTW\n',
shouldNotContain: '<blockquote'
},
ul: {input: '* A list\n*Of things', shouldNotContain: 'Of things'},
li: {input: '* IPA\n*Imperial Stout\n', shouldNotContain: '<li'},
strong: {input: "Don't **give up**, alright?", shouldNotContain: 'give up'},
hr: {input: '\n-----\nAnd with that...', shouldNotContain: '<hr'}
}
/** @type {Array<string>} */
const inputs = []
/** @type {keyof samples} */
let key
for (key in samples) {
if (own.call(samples, key)) {
inputs.push(samples[key].input)
}
}
const fullInput = inputs.join('\n')
for (key in samples) {
if (own.call(samples, key)) {
const sample = samples[key]
// Just to make sure, let ensure that the opposite is true
assert.equal(
asHtml(<Markdown children={fullInput} />).includes(
sample.shouldNotContain
),
true,
'fixture should contain `' +
sample.shouldNotContain +
'` (`' +
key +
'`)'
)
assert.equal(
asHtml(
<Markdown children={fullInput} disallowedElements={[key]} />
).includes(sample.shouldNotContain),
false,
'`' +
key +
'` should not contain `' +
sample.shouldNotContain +
'` when disallowed'
)
}
}
})
test('should throw if both allowed and disallowed types is specified', () => {
assert.throws(() => {
asHtml(
<Markdown
children=""
allowedElements={['p']}
disallowedElements={['a']}
/>
)
}, /only one of/i)
})
test('should be able to use a custom function to determine if the node should be allowed', () => {
const input = [
'# Header',
'[react-markdown](https://github.com/remarkjs/react-markdown/) is a nice helper',
'Also check out [my website](https://espen.codes/)'
].join('\n\n')
assert.equal(
asHtml(
<Markdown
children={input}
allowElement={(element) =>
element.tagName !== 'a' ||
(element.properties &&
typeof element.properties.href === 'string' &&
element.properties.href.indexOf('https://github.com/') === 0)
}
/>
),
[
'<h1>Header</h1>',
'<p><a href="https://github.com/remarkjs/react-markdown/">react-markdown</a> is a nice helper</p>',
'<p>Also check out </p>'
].join('\n')
)
})