-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathExpensiMark-HTML-test.js
2401 lines (2048 loc) · 144 KB
/
ExpensiMark-HTML-test.js
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
/* eslint-disable max-len,no-useless-concat */
import ExpensiMark from '../lib/ExpensiMark';
const parser = new ExpensiMark();
// Words wrapped in * successfully replaced with <strong></strong>
test('Test bold markdown replacement', () => {
const boldTestStartString = 'This is a *sentence,* and it has some *punctuation, words, and spaces*. ' + '*test* * testing* test*test*test. * testing * *testing *';
const boldTestReplacedString =
'This is a <strong>sentence,</strong> and it has some <strong>punctuation, words, and spaces</strong>. ' + '<strong>test</strong> * testing* test*test*test. * testing * *testing *';
expect(parser.replace(boldTestStartString)).toBe(boldTestReplacedString);
});
// Multi-line text wrapped in * is successfully replaced with <strong></strong>
test('Test multi-line bold markdown replacement', () => {
const testString = '*Here is a multi-line\ncomment that should\nbe bold*';
const replacedString = '<strong>Here is a multi-line<br />comment that should<br />be bold</strong>';
expect(parser.replace(testString)).toBe(replacedString);
});
test('Test bold within code blocks is skipped', () => {
const testString = 'bold\n```\n*not a bold*\n```\nThis is *bold*';
const replacedString = 'bold<br /><pre>*not a bold*<br /></pre>This is <strong>bold</strong>';
expect(parser.replace(testString)).toBe(replacedString);
});
test('Test special character plus _ ahead asterisk still result in bold', () => {
const testString = 'This is a !_*bold*';
const replacedString = 'This is a !_<strong>bold</strong>';
expect(parser.replace(testString)).toBe(replacedString);
});
test('Test a word plus _ ahead asterisk not result in bold', () => {
const testString = 'This is not a_*bold*';
const replacedString = 'This is not a_*bold*';
expect(parser.replace(testString)).toBe(replacedString);
});
test('Test _ ahead asterisk still result in bold', () => {
const testString = 'This is a ~_*bold*';
const replacedString = 'This is a ~_<strong>bold</strong>';
expect(parser.replace(testString)).toBe(replacedString);
});
test('Test heading markdown replacement', () => {
let testString = '# Heading should not have new line after it.\n';
expect(parser.replace(testString)).toBe('<h1>Heading should not have new line after it.</h1>');
testString = '# Heading should have only one new line after it.\n\n';
expect(parser.replace(testString)).toBe('<h1>Heading should have only one new line after it.</h1><br />');
testString = '# hello test.com';
expect(parser.replace(testString)).toBe('<h1>hello <a href="https://test.com" target="_blank" rel="noreferrer noopener">test.com</a></h1>');
testString = '# hello test@gmail.com';
expect(parser.replace(testString)).toBe('<h1>hello <a href="mailto:test@gmail.com">test@gmail.com</a></h1>');
});
// Sections starting with > are successfully wrapped with <blockquote></blockquote>
test('Test quote markdown replacement', () => {
const quoteTestStartString = '> This is a *quote* that started on a new line.\nHere is a >quote that did not\n```\nhere is a codefenced quote\n>it should not be quoted\n```';
const quoteTestReplacedString =
'<blockquote>This is a <strong>quote</strong> that started on a new line.</blockquote>Here is a >quote that did not<br /><pre>here is a codefenced quote<br />>it should not be quoted<br /></pre>';
expect(parser.replace(quoteTestStartString)).toBe(quoteTestReplacedString);
});
// Words wrapped in _ successfully replaced with <em></em>
test('Test italic markdown replacement', () => {
const italicTestStartString = 'This is a _sentence,_ and it has some _punctuation, words, and spaces_. ___ _italic__ _test_ _ testing_ test_test_test. _ test _ _test _';
const italicTestReplacedString =
'This is a <em>sentence,</em> and it has some <em>punctuation, words, and spaces</em>. ___ <em>italic</em>_ <em>test</em> _ testing_ test_test_test. _ test _ _test _';
expect(parser.replace(italicTestStartString)).toBe(italicTestReplacedString);
});
test('Test italic markdown replacement', () => {
const italicTestStartString = 'Note that _this is correctly italicized_\n' + 'Note that `_this is correctly not italicized_` and _following inline contents are italicized_';
const italicTestReplacedString =
'Note that <em>this is correctly italicized</em><br />' + 'Note that <code>_this is correctly not italicized_</code> and <em>following inline contents are italicized</em>';
expect(parser.replace(italicTestStartString)).toBe(italicTestReplacedString);
});
// Multi-line text wrapped in _ is successfully replaced with <em></em>
test('Test multi-line italic markdown replacement', () => {
const testString = '_Here is a multi-line\ncomment that should\nbe italic_ \n_\n_test\n_';
const replacedString = '<em>Here is a multi-line<br />comment that should<br />be italic</em> <br />_<br />_test<br />_';
expect(parser.replace(testString)).toBe(replacedString);
});
test('Test italic markdown replacement with word boundary and undercores', () => {
const testString = 'hello_world ' + 'hello__world ' + 'h_ello_ ' + '_hello_ ' + '___hello_ ' + '___hello______';
const replacedString = 'hello_world ' + 'hello__world ' + 'h_ello_ ' + '<em>hello</em> ' + '__<em>hello</em> ' + '__<em>hello</em>_____';
expect(parser.replace(testString)).toBe(replacedString);
});
test('Test quote markdown combined multi-line italic/bold/strikethrough markdown replacement', () => {
let testString = '_test\n> test_';
let replacedString = '_test<br /><blockquote>test_</blockquote>';
expect(parser.replace(testString)).toBe(replacedString);
testString = '*test\n> test*';
replacedString = '*test<br /><blockquote>test*</blockquote>';
expect(parser.replace(testString)).toBe(replacedString);
testString = '~test\n> test~';
replacedString = '~test<br /><blockquote>test~</blockquote>';
expect(parser.replace(testString)).toBe(replacedString);
});
// Words wrapped in ~ successfully replaced with <del></del>
test('Test strikethrough markdown replacement', () => {
const strikethroughTestStartString = 'This is a ~sentence,~ and it has some ~punctuation, words, and spaces~. ~test~ ~ testing~ test~test~test. ~ testing ~ ~testing ~';
const strikethroughTestReplacedString =
'This is a <del>sentence,</del> and it has some <del>punctuation, words, and spaces</del>. <del>test</del> ~ testing~ test~test~test. ~ testing ~ ~testing ~';
expect(parser.replace(strikethroughTestStartString)).toBe(strikethroughTestReplacedString);
});
// Multi-line text wrapped in ~ is successfully replaced with <del></del>
test('Test multi-line strikethrough markdown replacement', () => {
const testString = '~Here is a multi-line\ncomment that should\nhave strikethrough applied~';
const replacedString = '<del>Here is a multi-line<br />comment that should<br />have strikethrough applied</del>';
expect(parser.replace(testString)).toBe(replacedString);
});
// Emails containing _ are successfully wrapped in a mailto anchor tag
test('Test markdown replacement for emails and email links containing bold/strikethrough/italic', () => {
let testInput = 'a_b@gmail.com';
expect(parser.replace(testInput)).toBe('<a href="mailto:a_b@gmail.com">a_b@gmail.com</a>');
testInput = '[text](a_b@gmail.com)';
expect(parser.replace(testInput)).toBe('<a href="mailto:a_b@gmail.com">text</a>');
});
// Single-line emails wrapped in *_~ are successfully wrapped in a mailto anchor tag
test('Test markdown replacement for emails wrapped in bold/strikethrough/italic in a single line', () => {
let testInput = '~abc@gmail.com~';
expect(parser.replace(testInput)).toBe('<del><a href="mailto:abc@gmail.com">abc@gmail.com</a></del>');
testInput = '*abc@gmail.com*';
expect(parser.replace(testInput)).toBe('<strong><a href="mailto:abc@gmail.com">abc@gmail.com</a></strong>');
testInput = '_abc@gmail.com_';
expect(parser.replace(testInput)).toBe('<em><a href="mailto:abc@gmail.com">abc@gmail.com</a></em>');
testInput = '~*_abc@gmail.com_*~';
expect(parser.replace(testInput)).toBe('<del><strong><em><a href="mailto:abc@gmail.com">abc@gmail.com</a></em></strong></del>');
testInput = '[text](~abc@gmail.com~)';
expect(parser.replace(testInput)).toBe('[text](<del><a href="mailto:abc@gmail.com">abc@gmail.com</a></del>)');
testInput = '[text](*abc@gmail.com*)';
expect(parser.replace(testInput)).toBe('[text](<strong><a href="mailto:abc@gmail.com">abc@gmail.com</a></strong>)');
testInput = '[text](_abc@gmail.com_)';
expect(parser.replace(testInput)).toBe('[text](<em><a href="mailto:abc@gmail.com">abc@gmail.com</a></em>)');
testInput = '[text](~*_abc@gmail.com_*~)';
expect(parser.replace(testInput)).toBe('[text](<del><strong><em><a href="mailto:abc@gmail.com">abc@gmail.com</a></em></strong></del>)');
});
// Multi-line emails wrapped in *_~ are successfully wrapped in a mailto anchor tag
test('Test markdown replacement for emails wrapped in bold/strikethrough/italic in multi-line', () => {
let testInput = '~abc@gmail.com\ndef@gmail.com~';
let result = '<del><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />' + '<a href="mailto:def@gmail.com">def@gmail.com</a></del>';
expect(parser.replace(testInput)).toBe(result);
testInput = '*abc@gmail.com\ndef@gmail.com*';
result = '<strong><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />' + '<a href="mailto:def@gmail.com">def@gmail.com</a></strong>';
expect(parser.replace(testInput)).toBe(result);
testInput = '_abc@gmail.com\ndef@gmail.com_';
result = '<em><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />' + '<a href="mailto:def@gmail.com">def@gmail.com</a></em>';
expect(parser.replace(testInput)).toBe(result);
testInput = '~*_abc@gmail.com\ndef@gmail.com_*~';
result = '<del><strong><em><a href="mailto:abc@gmail.com">abc@gmail.com</a><br />' + '<a href="mailto:def@gmail.com">def@gmail.com</a></em></strong></del>';
expect(parser.replace(testInput)).toBe(result);
testInput = '_email@test.com\n_email2@test.com\n\nemail3@test.com_';
result =
'<em><a href="mailto:email@test.com">email@test.com</a><br />' +
'<a href="mailto:_email2@test.com">_email2@test.com</a><br /><br />' +
'<a href="mailto:email3@test.com">email3@test.com</a></em>';
expect(parser.replace(testInput)).toBe(result);
testInput = '[text](~abc@gmail.com)\n[text](def@gmail.com~)';
result = '[text](<del><a href="mailto:abc@gmail.com">abc@gmail.com</a>)<br />' + '[text](<a href="mailto:def@gmail.com">def@gmail.com</a></del>)';
expect(parser.replace(testInput)).toBe(result);
testInput = '[text](*abc@gmail.com)\n[text](def@gmail.com*)';
result = '[text](<strong><a href="mailto:abc@gmail.com">abc@gmail.com</a>)<br />' + '[text](<a href="mailto:def@gmail.com">def@gmail.com</a></strong>)';
expect(parser.replace(testInput)).toBe(result);
testInput = '[text](_abc@gmail.com)\n[text](def@gmail.com_)';
result = '<a href="mailto:_abc@gmail.com">text</a><br />' + '[text](<a href="mailto:def@gmail.com">def@gmail.com</a>_)';
expect(parser.replace(testInput)).toBe(result);
testInput = '[text](~*_abc@gmail.com)\n[text](def@gmail.com_*~)';
result = '[text](<del><strong><em><a href="mailto:abc@gmail.com">abc@gmail.com</a>)<br />' + '[text](<a href="mailto:def@gmail.com">def@gmail.com</a></em></strong></del>)';
expect(parser.replace(testInput)).toBe(result);
});
test('Test markdown replacement for multiline email hyperlinks', () => {
let testInput = '[test\ntest](test@test.com)';
let result = '<a href="mailto:test@test.com">test<br />test</a>';
expect(parser.replace(testInput)).toBe(result);
testInput = '[test\n\n\n](test@test.com)';
result = '<a href="mailto:test@test.com">test</a>';
expect(parser.replace(testInput)).toBe(result);
testInput = '[test\ntest](test.com)';
result = '<a href="https://test.com" target="_blank" rel="noreferrer noopener">test<br />test</a>';
expect(parser.replace(testInput)).toBe(result);
testInput = 'test@gmail.com';
result = '<a href="mailto:test@gmail.com">test@gmail.com</a>';
expect(parser.replace(testInput)).toBe(result);
});
// Check emails within other markdown
test('Test emails within other markdown', () => {
const testString =
'> test@example.com\n' + '```\ntest@example.com\n```\n' + '`test@example.com`\n' + '_test@example.com_ ' + '_test@example.com__ ' + '__test@example.com__ ' + '__test@example.com_';
const result =
'<blockquote><a href="mailto:test@example.com">test@example.com</a></blockquote>' +
'<pre>test@example.com<br /></pre>' +
'<code>test@example.com</code><br />' +
'<em><a href="mailto:test@example.com">test@example.com</a></em> ' +
'<em><a href="mailto:test@example.com">test@example.com</a></em>_ ' +
'<em><a href="mailto:_test@example.com">_test@example.com</a></em>_ ' +
'<em><a href="mailto:_test@example.com">_test@example.com</a></em>';
expect(parser.replace(testString)).toBe(result);
});
// Check email regex's validity at various limits
test('Test markdown replacement for valid emails', () => {
const testString =
'A simple email: abc.new@gmail.com, ' +
'or a very short one a@example.com ' +
'hitting the maximum domain length (63 chars) test@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com ' +
'or the maximum address length (64 chars) sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@test.com ' +
'overall length of 254 averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com ' +
'domain with many dashes sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asj-j-s-sjdjdjdjd-jdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdke.com.ab.net.aa.bb.cc.dd.ee ' +
' how about a domain with repeated labels of 63 chars test@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.com ' +
'max length email with italics ' +
'_averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com_ ' +
' xn-- style domain test@xn--diseolatinoamericano-76b.com ' +
'or a more complex case where we need to determine where to apply italics markdown ' +
'_email@test.com\n_email2@test.com\n\nemail3@test.com_ ' +
'some unusual, but valid prefixes -test@example.com ' +
' and _test@example.com ' +
'a max length email enclosed in brackets ' +
'(averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com) ' +
'max length email with ellipsis ending ' +
'averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com... ' +
'try a markdown link with a valid max-length email ' +
'[text](sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com.a.aa.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjasfff)' +
'$--test@gmail.com';
const result =
'A simple email: <a href="mailto:abc.new@gmail.com">abc.new@gmail.com</a>, ' +
'or a very short one <a href="mailto:a@example.com">a@example.com</a> ' +
'hitting the maximum domain length (63 chars) <a href="mailto:test@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com">test@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com</a> ' +
'or the maximum address length (64 chars) <a href="mailto:sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@test.com">sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@test.com</a> ' +
'overall length of 254 <a href="mailto:averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com">averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com</a> ' +
'domain with many dashes <a href="mailto:sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asj-j-s-sjdjdjdjd-jdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdke.com.ab.net.aa.bb.cc.dd.ee">sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asj-j-s-sjdjdjdjd-jdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdke.com.ab.net.aa.bb.cc.dd.ee</a> ' +
' how about a domain with repeated labels of 63 chars <a href="mailto:test@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.com">test@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekasgasgasgasgashfnfn.com</a> ' +
'max length email with italics ' +
'<em><a href="mailto:averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com">averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com</a></em> ' +
' xn-- style domain <a href="mailto:test@xn--diseolatinoamericano-76b.com">test@xn--diseolatinoamericano-76b.com</a> ' +
'or a more complex case where we need to determine where to apply italics markdown ' +
'<em><a href="mailto:email@test.com">email@test.com</a><br />' +
'<a href="mailto:_email2@test.com">_email2@test.com</a><br /><br />' +
'<a href="mailto:email3@test.com">email3@test.com</a></em> ' +
'some unusual, but valid prefixes <a href="mailto:-test@example.com">-test@example.com</a> ' +
' and <a href="mailto:_test@example.com">_test@example.com</a> ' +
'a max length email enclosed in brackets ' +
'(<a href="mailto:averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com">averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com</a>) ' +
'max length email with ellipsis ending ' +
'<a href="mailto:averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com">averylongaddresspartthatalmostwillreachthelimitofcharsperaddress@nowwejustneedaverylongdomainpartthatwill.reachthetotallengthlimitforthewholeemailaddress.whichis254charsaccordingtothePHPvalidate-email-filter.extendingthetestlongeruntilwereachtheright.com</a>... ' +
'try a markdown link with a valid max-length email ' +
'<a href="mailto:sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com.a.aa.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjasfff">text</a>' +
'$<a href="mailto:--test@gmail.com">--test@gmail.com</a>';
expect(parser.replace(testString)).toBe(result);
});
test('Test markdown replacement for invalid emails', () => {
const testString =
'Replace the valid email part within a string ' +
'.test@example.com ' +
'$test@gmail.com ' +
'test..new@example.com ' +
'Some chars that are not allowed within emails ' +
'test{@example.com ' +
'domain length over limit test@averylongdomainpartoftheemailthatwillgooverthelimitasitismorethan63chars.com ' +
'address over limit averylongaddresspartoftheemailthatwillgovoerthelimitasitismorethan64chars@example.com ' +
'overall length too long ' +
'sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com.a.aa.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjasfffa ' +
'invalid domain start/end ' +
'test@example-.com ' +
'test@-example-.com ' +
'test@example.a ' +
'[text](sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com.a.aa.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjasfffa)';
const result =
'Replace the valid email part within a string ' +
'.<a href="mailto:test@example.com">test@example.com</a> ' +
'$<a href="mailto:test@gmail.com">test@gmail.com</a> ' +
'test..<a href="mailto:new@example.com">new@example.com</a> ' +
'Some chars that are not allowed within emails ' +
'test{@example.com ' +
'domain length over limit test@averylongdomainpartoftheemailthatwillgooverthelimitasitismorethan63chars.com ' +
'address over limit averylongaddresspartoftheemailthatwillgovoerthelimitasitismorethan64chars@example.com ' +
'overall length too long ' +
'sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com.a.aa.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjasfffa ' +
'invalid domain start/end ' +
'test@example-.com ' +
'test@-example-.com ' +
'test@example.a ' +
'[text](sjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcjab@asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.com.a.aa.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjdkekejdcjdkeekcj.asjjssjdjdjdjdjdjjeiwiwiwowkdjdjdieikdjfidekjcjasfffa)';
expect(parser.replace(testString)).toBe(result);
});
test('Test markdown replacement for emojis with emails', () => {
const testString =
'Replace the emoji with link ' +
'[😄](abc@gmail.com) ' +
'[😄]( abc@gmail.com) ' +
'[😄] abc@gmail.com ' +
'[😄]((abc@gmail.com)) ' +
'[😄abc@gmail.com](abc@gmail.com) ' +
'[😄 abc@gmail.com ](abc@gmail.com) ';
const result =
'Replace the emoji with link ' +
'<a href=\"mailto:abc@gmail.com\"><emoji>😄</emoji></a> ' +
'[<emoji>😄</emoji>]( <a href="mailto:abc@gmail.com">abc@gmail.com</a>) ' +
'[<emoji>😄</emoji>] <a href="mailto:abc@gmail.com">abc@gmail.com</a> ' +
'[<emoji>😄</emoji>]((<a href="mailto:abc@gmail.com">abc@gmail.com</a>)) ' +
'<a href="mailto:abc@gmail.com"><emoji>😄</emoji>abc@gmail.com</a> ' +
'<a href="mailto:abc@gmail.com"><emoji>😄</emoji> abc@gmail.com</a> ';
expect(parser.replace(testString)).toBe(result);
});
test('Test markdown replacement for composite emoji', () => {
const testString = 'Replace composite emoji with only one emoji tag ' + '😶🌫️ ' + '🧑🔧 ' + '👨🏫 ' + '👨🏾❤️👨🏽 ';
const result = 'Replace composite emoji with only one emoji tag ' + '<emoji>😶🌫️</emoji> ' + '<emoji>🧑🔧</emoji> ' + '<emoji>👨🏫</emoji> ' + '<emoji>👨🏾❤️👨🏽</emoji> ';
expect(parser.replace(testString)).toBe(result);
});
// Markdown style links replaced successfully
test('Test markdown style links', () => {
let testString =
"Go to [Expensify](https://www.expensify.com) to learn more. [Expensify](www.expensify.com) [Expensify](expensify.com) [It's really the coolest](expensify.com) [`Some` Special cases - + . = , '](expensify.com/some?query=par|am)";
let resultString =
'Go to <a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a> to learn more. <a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a> <a href="https://expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a> <a href="https://expensify.com" target="_blank" rel="noreferrer noopener">It's really the coolest</a> <a href="https://expensify.com/some?query=par|am" target="_blank" rel="noreferrer noopener"><code>Some</code> Special cases - + . = , '</a>';
expect(parser.replace(testString)).toBe(resultString);
testString =
"Go to [Localhost](http://localhost:3000) to learn more. [Expensify](www.expensify.com) [Expensify](expensify.com) [It's really the coolest](expensify.com) [`Some` Special cases - + . = , '](expensify.com/some?query=par|am)";
resultString =
'Go to <a href="http://localhost:3000" target="_blank" rel="noreferrer noopener">Localhost</a> to learn more. <a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a> <a href="https://expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a> <a href="https://expensify.com" target="_blank" rel="noreferrer noopener">It's really the coolest</a> <a href="https://expensify.com/some?query=par|am" target="_blank" rel="noreferrer noopener"><code>Some</code> Special cases - + . = , '</a>';
expect(parser.replace(testString)).toBe(resultString);
});
// Critical Markdown style links replaced successfully
test('Test critical markdown style links', () => {
const testString =
'Testing ' +
'[~strikethrough~ *bold* _italic_](expensify.com) ' +
'[~strikethrough~ *bold* _italic_ test.com](expensify.com) ' +
'[~strikethrough~ *bold* _italic_ https://test.com](https://expensify.com) ' +
'[https://www.text.com/_root_folder/1](https://www.text.com/_root_folder/1) ' +
'[first](https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878) ' +
'[first no https://](www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878) ' +
'[second](http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled) ' +
'[second no http://](necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled) ' +
'[third](https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash) ' +
'[third no https://](github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash) ' +
'[link `[inside another link](https://google.com)`](https://google.com) ' +
'[link with an @ in it](https://google.com) ' +
'[link with [brackets] inside of it](https://google.com) ' +
'[link with smart quotes ‘’“”](https://google.com) ' +
'[link with someone@expensify.com email in it](https://google.com)' +
'[Localhost](http://a:3030)';
const resultString =
'Testing ' +
'<a href="https://expensify.com" target="_blank" rel="noreferrer noopener"><del>strikethrough</del> <strong>bold</strong> <em>italic</em></a> ' +
'<a href="https://expensify.com" target="_blank" rel="noreferrer noopener"><del>strikethrough</del> <strong>bold</strong> <em>italic</em> test.com</a> ' +
'<a href="https://expensify.com" target="_blank" rel="noreferrer noopener"><del>strikethrough</del> <strong>bold</strong> <em>italic</em> https://test.com</a> ' +
'<a href="https://www.text.com/_root_folder/1" target="_blank" rel="noreferrer noopener">https://www.text.com/_root_folder/1</a> ' +
'<a href="https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878" target="_blank" rel="noreferrer noopener">first</a> ' +
'<a href="https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878" target="_blank" rel="noreferrer noopener">first no https://</a> ' +
'<a href="http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled" target="_blank" rel="noreferrer noopener">second</a> ' +
'<a href="https://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled" target="_blank" rel="noreferrer noopener">second no http://</a> ' +
'<a href="https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash" target="_blank" rel="noreferrer noopener">third</a> ' +
'<a href="https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash" target="_blank" rel="noreferrer noopener">third no https://</a> ' +
'<a href="https://google.com" target="_blank" rel="noreferrer noopener">link <code>[inside another link](https://google.com)</code></a> ' +
'<a href="https://google.com" target="_blank" rel="noreferrer noopener">link with an @ in it</a> ' +
'<a href="https://google.com" target="_blank" rel="noreferrer noopener">link with [brackets] inside of it</a> ' +
'<a href="https://google.com" target="_blank" rel="noreferrer noopener">link with smart quotes ‘’“”</a> ' +
'<a href="https://google.com" target="_blank" rel="noreferrer noopener">link with someone@expensify.com email in it</a>' +
'<a href="http://a:3030" target="_blank" rel="noreferrer noopener">Localhost</a>';
expect(parser.replace(testString)).toBe(resultString);
});
// HTML encoded strings unaffected by parser
test('Test HTML encoded strings', () => {
const rawHTMLTestStartString = '<em>This is</em> a <strong>test</strong>. None of <h1>these strings</h1> should display <del>as</del> <div>HTML</div>.';
const rawHTMLTestReplacedString =
'<em>This is</em> a <strong>test</strong>. None of <h1>these strings</h1> should display <del>as</del> <div>HTML</div>.';
expect(parser.replace(rawHTMLTestStartString)).toBe(rawHTMLTestReplacedString);
});
// New lines characters \n or \r\n were successfully replaced with <br>
test('Test newline markdown replacement', () => {
let newLineTestStartString = 'This sentence has a newline \n Yep just had one \n Oh there it is another one';
let newLineReplacedString = 'This sentence has a newline <br /> Yep just had one <br /> Oh there it is another one';
expect(parser.replace(newLineTestStartString)).toBe(newLineReplacedString);
newLineTestStartString = 'This sentence has a windows-style newline \r\n Yep just had one \r\n Oh there it is another one';
newLineReplacedString = 'This sentence has a windows-style newline <br /> Yep just had one <br /> Oh there it is another one';
expect(parser.replace(newLineTestStartString)).toBe(newLineReplacedString);
});
// Period replacement test
test('Test period replacements', () => {
const periodTestStartString = 'This test ensures that words with trailing... periods.. are. not converted to links.';
expect(parser.replace(periodTestStartString)).toBe(periodTestStartString);
});
test('Test code fencing', () => {
const codeFenceExampleMarkdown = "```\nconst javaScript = 'javaScript'\n```";
expect(parser.replace(codeFenceExampleMarkdown)).toBe('<pre>const javaScript = 'javaScript'<br /></pre>');
});
test('Test code fencing with spaces and new lines', () => {
let codeFenceExample = "```\nconst javaScript = 'javaScript'\n const php = 'php'\n```";
expect(parser.replace(codeFenceExample)).toBe('<pre>const javaScript = 'javaScript'<br />    const php = 'php'<br /></pre>');
codeFenceExample = '```\n\n# test\n\n```';
expect(parser.replace(codeFenceExample)).toBe('<pre><br /># test<br /><br /></pre>');
codeFenceExample = '```\n\n\n# test\n\n```';
expect(parser.replace(codeFenceExample)).toBe('<pre><br /><br /># test<br /><br /></pre>');
codeFenceExample = '```\r\n# test\r\n```';
expect(parser.replace(codeFenceExample)).toBe('<pre># test<br /></pre>');
codeFenceExample = '``` \r\n# test\r\n```';
expect(parser.replace(codeFenceExample)).toBe('<pre># test<br /></pre>');
codeFenceExample = '```test\r\n# test\r\n```';
expect(parser.replace(codeFenceExample)).toBe('<pre># test<br /></pre>');
codeFenceExample = '```\r\n\r\n# test\r\n\r\n```';
expect(parser.replace(codeFenceExample)).toBe('<pre><br /># test<br /><br /></pre>');
codeFenceExample = '```\r\n\r\n\r\n# test\r\n\r\n```';
expect(parser.replace(codeFenceExample)).toBe('<pre><br /><br /># test<br /><br /></pre>');
});
test('Test inline code blocks', () => {
const inlineCodeStartString = 'My favorite language is `JavaScript`. How about you?';
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is <code>JavaScript</code>. How about you?');
});
test('Test double backticks', () => {
const testString = 'My favorite language is ``JavaScript``. How about you?';
expect(parser.replace(testString)).toBe('My favorite language is ``JavaScript``. How about you?');
});
test('Test inline code blocks with triple backticks', () => {
const inlineCodeStartString = 'My favorite language is ```JavaScript```. How about you?';
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is ``<code>JavaScript</code>``. How about you?');
});
test('Test multiple inline codes in one line', () => {
const inlineCodeStartString = 'My favorite language is `JavaScript`. How about you? I also like `Python`.';
expect(parser.replace(inlineCodeStartString)).toBe('My favorite language is <code>JavaScript</code>. How about you? I also like <code>Python</code>.');
});
test('Test triple backticks', () => {
const testString = '```';
expect(parser.replace(testString)).toBe('```');
});
test('Test multiple backtick symbols', () => {
const testString = '``````';
expect(parser.replace(testString)).toBe('``````');
});
test('Test inline code blocks with ExpensiMark syntax inside', () => {
const inlineCodeStartString = '`This is how you can write ~strikethrough~, *bold*, and _italics_`';
expect(parser.replace(inlineCodeStartString)).toBe('<code>This is how you can write ~strikethrough~, *bold*, and _italics_</code>');
});
test('Test inline code blocks inside ExpensiMark', () => {
const testString = '_`test`_' + '*`test`*' + '~`test`~';
const resultString = '<em><code>test</code></em>' + '<strong><code>test</code></strong>' + '<del><code>test</code></del>';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test words enclosed in double backticks', () => {
const testString = '``JavaScript``';
expect(parser.replace(testString)).toBe('``JavaScript``');
});
test('Test code fencing with ExpensiMark syntax inside', () => {
const codeFenceExample = '```\nThis is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)\n```';
expect(parser.replace(codeFenceExample)).toBe(
'<pre>This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)<br /></pre>',
);
});
test('Test code fencing with ExpensiMark syntax outside', () => {
let codeFenceExample = '# Test1 ```\ncode\n``` Test2';
expect(parser.replace(codeFenceExample)).toBe('<h1>Test1 </h1><pre>code<br /></pre> Test2');
codeFenceExample = '*Test1 ```\ncode\n``` Test2*';
expect(parser.replace(codeFenceExample)).toBe('*Test1 <pre>code<br /></pre> Test2*');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('*Test1 <pre>\ncode\n</pre> Test2*');
codeFenceExample = '_Test1 ```\ncode\n``` Test2_';
expect(parser.replace(codeFenceExample)).toBe('_Test1 <pre>code<br /></pre> Test2_');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('_Test1 <pre>\ncode\n</pre> Test2_');
codeFenceExample = '~Test1 ```\ncode\n``` Test2~';
expect(parser.replace(codeFenceExample)).toBe('~Test1 <pre>code<br /></pre> Test2~');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe('~Test1 <pre>\ncode\n</pre> Test2~');
codeFenceExample = '[```\ncode\n```](google.com)';
expect(parser.replace(codeFenceExample)).toBe('[<pre>code<br /></pre>](<a href="https://google.com" target="_blank" rel="noreferrer noopener">google.com</a>)');
expect(parser.replace(codeFenceExample, {shouldKeepRawInput: true})).toBe(
'[<pre>\ncode\n</pre>](<a href="https://google.com" data-raw-href="google.com" data-link-variant="auto" target="_blank" rel="noreferrer noopener">google.com</a>)',
);
});
test('Test code fencing with additional backticks inside', () => {
let nestedBackticks = '```\n`test`\n```';
expect(parser.replace(nestedBackticks)).toBe('<pre>`test`<br /></pre>');
nestedBackticks = '```\n`\ntest\n`\n```';
expect(parser.replace(nestedBackticks)).toBe('<pre>`<br />test<br />`<br /></pre>');
nestedBackticks = '```\n``\n```';
expect(parser.replace(nestedBackticks)).toBe('<pre>``<br /></pre>');
nestedBackticks = '```\n`\n`\n```';
expect(parser.replace(nestedBackticks)).toBe('<pre>`<br />`<br /></pre>');
nestedBackticks = '```\n`This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)`\n```';
expect(parser.replace(nestedBackticks)).toBe(
'<pre>`This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)`<br /></pre>',
);
nestedBackticks = '```\n`test`👋\n```';
expect(parser.replace(nestedBackticks)).toBe('<pre>`test`<emoji>👋</emoji><br /></pre>');
});
test('Test combination replacements', () => {
const urlTestStartString =
'<em>Here</em> is a _combination test_ that <marquee>sees</marquee> if ~https://www.example.com~ https://otherexample.com links get rendered first followed by *other markup* or if _*two work together*_ as well. This sentence also has a newline \n Yep just had one.';
const urlTestReplacedString =
'<em>Here</em> is a <em>combination test</em> that <marquee>sees</marquee> if <del><a href="https://www.example.com" target="_blank" rel="noreferrer noopener">https://www.example.com</a></del> <a href="https://otherexample.com"' +
' target="_blank" rel="noreferrer noopener">https://otherexample.com</a> links get rendered first followed by <strong>other markup</strong> or if <em><strong>two work together</strong></em> as well. This sentence also has a newline <br /> Yep just had one.';
expect(parser.replace(urlTestStartString)).toBe(urlTestReplacedString);
});
test('Test wrapped URLs', () => {
const wrappedUrlTestStartString = '~https://www.example.com~ _http://www.test.com_ *http://www.asdf.com/_test*';
const wrappedUrlTestReplacedString =
'<del><a href="https://www.example.com" target="_blank" rel="noreferrer noopener">https://www.example.com</a></del> <em><a href="http://www.test.com" target="_blank" rel="noreferrer noopener">http://www.test.com</a></em>' +
' <strong><a href="http://www.asdf.com/_test" target="_blank" rel="noreferrer noopener">http://www.asdf.com/_test</a></strong>';
expect(parser.replace(wrappedUrlTestStartString)).toBe(wrappedUrlTestReplacedString);
});
test('Test Url, where double quote is not allowed', () => {
const urlTestStartString =
'"om https://www.she.com/"\n' +
'"om https://www.she.com"\n' +
'"https://www.she.com/ end"\n' +
'"https://www.she.com end"\n' +
'https://www.she.com/path?test="123"\n' +
'https://www.she.com/path?test="123/"\n' +
'"https://www.she.com/path?test="123"\n' +
'"https://www.she.com/path?test="123/"\n' +
'https://www.she.com/path?test=123"\n' +
'https://www.she.com/path?test=123/"\n' +
'https://www.she.com/path?test=/"123"\n' +
'https://www.she.com/path?test=/"123/"';
const urlTestReplacedString =
'"om <a href="https://www.she.com/" target="_blank" rel="noreferrer noopener">https://www.she.com/</a>"<br />' +
'"om <a href="https://www.she.com" target="_blank" rel="noreferrer noopener">https://www.she.com</a>"<br />' +
'"<a href="https://www.she.com/" target="_blank" rel="noreferrer noopener">https://www.she.com/</a> end"<br />' +
'"<a href="https://www.she.com" target="_blank" rel="noreferrer noopener">https://www.she.com</a> end"<br />' +
'<a href="https://www.she.com/path?test=" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=</a>"123"<br />' +
'<a href="https://www.she.com/path?test=" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=</a>"123/"<br />' +
'"<a href="https://www.she.com/path?test=" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=</a>"123"<br />' +
'"<a href="https://www.she.com/path?test=" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=</a>"123/"<br />' +
'<a href="https://www.she.com/path?test=123" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=123</a>"<br />' +
'<a href="https://www.she.com/path?test=123/" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=123/</a>"<br />' +
'<a href="https://www.she.com/path?test=/" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=/</a>"123"<br />' +
'<a href="https://www.she.com/path?test=/" target="_blank" rel="noreferrer noopener">https://www.she.com/path?test=/</a>"123/"';
expect(parser.replace(urlTestStartString)).toBe(urlTestReplacedString);
});
test('Test url replacements', () => {
const urlTestStartString =
'Testing ' +
'foo.com ' +
'www.foo.com ' +
'http://www.foo.com ' +
'http://foo.com/blah_blah ' +
'http://foo.com/blah_blah/ ' +
'http://foo.com/blah_blah_(wikipedia) ' +
'http://www.example.com/wpstyle/?p=364 ' +
'https://www.example.com/foo/?bar=baz&inga=42&quux ' +
'http://foo.com/(something)?after=parens ' +
'http://code.google.com/events/#&product=browser ' +
'http://foo.bar/?q=Test%20URL-encoded%20stuff ' +
'http://www.test.com/path?param=123#123 ' +
'http://1337.net ' +
'http://a.b-c.de/ ' +
'https://sd1.sd2.docs.google.com/ ' +
'https://expensify.cash/#/r/1234 ' +
'https://github.com/Expensify/ReactNativeChat/pull/6.45 ' +
'https://github.com/Expensify/Expensify/issues/143,231 ' +
'testRareTLDs.beer ' +
'test@expensify.com ' +
'test.completelyFakeTLD ' +
'https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878) ' +
'http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled ' +
'https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash ' +
'https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash ' +
'mm..food ' +
'upwork.com/jobs/~016781e062ce860b84 ' +
"https://bastion1.sjc/logs/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'2125cbe0-28a9-11e9-a79c-3de0157ed580',interval:auto,query:(language:lucene,query:''),sort:!(timestamp,desc)) " +
"google.com/maps/place/The+Flying'+Saucer/@42.4043314,-86.2742418,15z/data=!4m5!3m4!1s0x0:0xe28f6108670216bc!8m2!3d42.4043316!4d-86.2742121 " +
'google.com/maps/place/%E9%9D%92%E5%B3%B6%E9%80%A3%E7%B5%A1%E8%88%B9%E4%B9%97%E5%A0%B4/@33.7363156,132.4877213,17.78z/data=!4m5!3m4!1s0x3545615c8c65bf7f:0xb89272c1a705a33f!8m2!3d33.7366776!4d132.4878843 ' +
'https://www.google.com/maps/place/Taj+Mahal+@is~%22Awesome%22/@27.1751496,78.0399535,17z/data=!4m12!1m6!3m5!1s0x39747121d702ff6d:0xdd2ae4803f767dde!2sTaj+Mahal!8m2!3d27.1751448!4d78.0421422!3m4!1s0x39747121d702ff6d:0xdd2ae4803f767dde!8m2!3d27.1751448!4d78.0421422 ' +
'https://www.facebook.com/hashtag/__main/?__eep__=6 ' +
'https://example.com/~username/foo~bar.txt ' +
'http://example.com/foo/*/bar/*/test.txt ' +
'test-.com ' +
'-test.com ' +
'@test.com ' +
'@test.com test.com ' +
'@test.com @test.com ';
const urlTestReplacedString =
'Testing ' +
'<a href="https://foo.com" target="_blank" rel="noreferrer noopener">foo.com</a> ' +
'<a href="https://www.foo.com" target="_blank" rel="noreferrer noopener">www.foo.com</a> ' +
'<a href="http://www.foo.com" target="_blank" rel="noreferrer noopener">http://www.foo.com</a> ' +
'<a href="http://foo.com/blah_blah" target="_blank" rel="noreferrer noopener">http://foo.com/blah_blah</a> ' +
'<a href="http://foo.com/blah_blah/" target="_blank" rel="noreferrer noopener">http://foo.com/blah_blah/</a> ' +
'<a href="http://foo.com/blah_blah_(wikipedia)" target="_blank" rel="noreferrer noopener">http://foo.com/blah_blah_(wikipedia)</a> ' +
'<a href="http://www.example.com/wpstyle/?p=364" target="_blank" rel="noreferrer noopener">http://www.example.com/wpstyle/?p=364</a> ' +
'<a href="https://www.example.com/foo/?bar=baz&inga=42&quux" target="_blank" rel="noreferrer noopener">https://www.example.com/foo/?bar=baz&inga=42&quux</a> ' +
'<a href="http://foo.com/(something)?after=parens" target="_blank" rel="noreferrer noopener">http://foo.com/(something)?after=parens</a> ' +
'<a href="http://code.google.com/events/#&product=browser" target="_blank" rel="noreferrer noopener">http://code.google.com/events/#&product=browser</a> ' +
'<a href="http://foo.bar/?q=Test%20URL-encoded%20stuff" target="_blank" rel="noreferrer noopener">http://foo.bar/?q=Test%20URL-encoded%20stuff</a> ' +
'<a href="http://www.test.com/path?param=123#123" target="_blank" rel="noreferrer noopener">http://www.test.com/path?param=123#123</a> ' +
'<a href="http://1337.net" target="_blank" rel="noreferrer noopener">http://1337.net</a> ' +
'<a href="http://a.b-c.de/" target="_blank" rel="noreferrer noopener">http://a.b-c.de/</a> ' +
'<a href="https://sd1.sd2.docs.google.com/" target="_blank" rel="noreferrer noopener">https://sd1.sd2.docs.google.com/</a> ' +
'<a href="https://expensify.cash/#/r/1234" target="_blank" rel="noreferrer noopener">https://expensify.cash/#/r/1234</a> ' +
'<a href="https://github.com/Expensify/ReactNativeChat/pull/6.45" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/6.45</a> ' +
'<a href="https://github.com/Expensify/Expensify/issues/143,231" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/Expensify/issues/143,231</a> ' +
'<a href="https://testraretlds.beer" target="_blank" rel="noreferrer noopener">testRareTLDs.beer</a> ' +
'<a href="mailto:test@expensify.com">test@expensify.com</a> ' +
'test.completelyFakeTLD ' +
'<a href="https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878" target="_blank" rel="noreferrer noopener">https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878</a>) ' +
'<a href="http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled" target="_blank" rel="noreferrer noopener">http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled</a> ' +
'<a href="https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash</a> ' +
'<a href="https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash</a> ' +
'mm..food ' +
'<a href="https://upwork.com/jobs/~016781e062ce860b84" target="_blank" rel="noreferrer noopener">upwork.com/jobs/~016781e062ce860b84</a> ' +
'<a href="https://bastion1.sjc/logs/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'2125cbe0-28a9-11e9-a79c-3de0157ed580',interval:auto,query:(language:lucene,query:''),sort:!(timestamp,desc))" target="_blank" rel="noreferrer noopener">https://bastion1.sjc/logs/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'2125cbe0-28a9-11e9-a79c-3de0157ed580',interval:auto,query:(language:lucene,query:''),sort:!(timestamp,desc))</a> ' +
'<a href="https://google.com/maps/place/The+Flying'+Saucer/@42.4043314,-86.2742418,15z/data=!4m5!3m4!1s0x0:0xe28f6108670216bc!8m2!3d42.4043316!4d-86.2742121" target="_blank" rel="noreferrer noopener">google.com/maps/place/The+Flying'+Saucer/@42.4043314,-86.2742418,15z/data=!4m5!3m4!1s0x0:0xe28f6108670216bc!8m2!3d42.4043316!4d-86.2742121</a> ' +
'<a href="https://google.com/maps/place/%E9%9D%92%E5%B3%B6%E9%80%A3%E7%B5%A1%E8%88%B9%E4%B9%97%E5%A0%B4/@33.7363156,132.4877213,17.78z/data=!4m5!3m4!1s0x3545615c8c65bf7f:0xb89272c1a705a33f!8m2!3d33.7366776!4d132.4878843" target="_blank" rel="noreferrer noopener">google.com/maps/place/%E9%9D%92%E5%B3%B6%E9%80%A3%E7%B5%A1%E8%88%B9%E4%B9%97%E5%A0%B4/@33.7363156,132.4877213,17.78z/data=!4m5!3m4!1s0x3545615c8c65bf7f:0xb89272c1a705a33f!8m2!3d33.7366776!4d132.4878843</a> ' +
'<a href="https://www.google.com/maps/place/Taj+Mahal+@is~%22Awesome%22/@27.1751496,78.0399535,17z/data=!4m12!1m6!3m5!1s0x39747121d702ff6d:0xdd2ae4803f767dde!2sTaj+Mahal!8m2!3d27.1751448!4d78.0421422!3m4!1s0x39747121d702ff6d:0xdd2ae4803f767dde!8m2!3d27.1751448!4d78.0421422" target="_blank" rel="noreferrer noopener">https://www.google.com/maps/place/Taj+Mahal+@is~%22Awesome%22/@27.1751496,78.0399535,17z/data=!4m12!1m6!3m5!1s0x39747121d702ff6d:0xdd2ae4803f767dde!2sTaj+Mahal!8m2!3d27.1751448!4d78.0421422!3m4!1s0x39747121d702ff6d:0xdd2ae4803f767dde!8m2!3d27.1751448!4d78.0421422</a> ' +
'<a href="https://www.facebook.com/hashtag/__main/?__eep__=6" target="_blank" rel="noreferrer noopener">https://www.facebook.com/hashtag/__main/?__eep__=6</a> ' +
'<a href="https://example.com/~username/foo~bar.txt" target="_blank" rel="noreferrer noopener">https://example.com/~username/foo~bar.txt</a> ' +
'<a href="http://example.com/foo/*/bar/*/test.txt" target="_blank" rel="noreferrer noopener">http://example.com/foo/*/bar/*/test.txt</a> ' +
'test-.com ' +
'-<a href="https://test.com" target="_blank" rel="noreferrer noopener">test.com</a> ' +
'<mention-short>@test.com</mention-short> ' +
'<mention-short>@test.com</mention-short> <a href="https://test.com" target="_blank" rel="noreferrer noopener">test.com</a> ' +
'<mention-short>@test.com</mention-short> <mention-short>@test.com</mention-short> ';
expect(parser.replace(urlTestStartString)).toBe(urlTestReplacedString);
});
test('Test markdown style link with various styles', () => {
const testString =
'Go to ~[Expensify](https://www.expensify.com)~ ' +
'_[Expensify](https://www.expensify.com)_ ' +
'*[Expensify](https://www.expensify.com)* ' +
'[Expensify!](https://www.expensify.com) ' +
'[Expensify?](https://www.expensify.com) ' +
'[Expensify](https://www.expensify-test.com) ' +
'[Expensify](https://www.expensify.com/settings?param={%22section%22:%22account%22}) ' +
'[Expensify](https://www.expensify.com/settings?param=(%22section%22+%22account%22)) ' +
'[Expensify](https://www.expensify.com/settings?param=[%22section%22:%22account%22]) ' +
'[https://www.facebook.com/hashtag/__main/?__eep__=6](https://www.facebook.com/hashtag/__main/?__eep__=6) ' +
'[https://example.com/~username/foo~bar.txt](https://example.com/~username/foo~bar.txt) ' +
'[http://example.com/foo/*/bar/*/test.txt](http://example.com/foo/*/bar/*/test.txt)';
const resultString =
'Go to <del><a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a></del> ' +
'<em><a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a></em> ' +
'<strong><a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify</a></strong> ' +
'<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify!</a> ' +
'<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">Expensify?</a> ' +
'<a href="https://www.expensify-test.com" target="_blank" rel="noreferrer noopener">Expensify</a> ' +
'<a href="https://www.expensify.com/settings?param={%22section%22:%22account%22}" target="_blank" rel="noreferrer noopener">Expensify</a> ' +
'<a href="https://www.expensify.com/settings?param=(%22section%22+%22account%22)" target="_blank" rel="noreferrer noopener">Expensify</a> ' +
'<a href="https://www.expensify.com/settings?param=[%22section%22:%22account%22]" target="_blank" rel="noreferrer noopener">Expensify</a> ' +
'<a href="https://www.facebook.com/hashtag/__main/?__eep__=6" target="_blank" rel="noreferrer noopener">https://www.facebook.com/hashtag/__main/?__eep__=6</a> ' +
'<a href="https://example.com/~username/foo~bar.txt" target="_blank" rel="noreferrer noopener">https://example.com/~username/foo~bar.txt</a> ' +
'<a href="http://example.com/foo/*/bar/*/test.txt" target="_blank" rel="noreferrer noopener">http://example.com/foo/*/bar/*/test.txt</a>';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test links that end in a comma autolink correctly', () => {
const testString = 'https://github.com/Expensify/Expensify/issues/143231,';
const resultString = '<a href="https://github.com/Expensify/Expensify/issues/143231" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/Expensify/issues/143231</a>,';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test links inside two backticks are not autolinked', () => {
const testString = '`https://github.com/Expensify/Expensify/issues/143231`';
const resultString = '<code>https://github.com/Expensify/Expensify/issues/143231</code>';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test a period at the end of a link autolinks correctly', () => {
const testString = 'https://github.com/Expensify/ReactNativeChat/pull/645.';
const resultString =
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>.';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test a url ending with a question mark autolinks correctly', () => {
const testString = 'https://github.com/Expensify/ReactNativeChat/pull/645?';
const resultString =
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645?" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645?</a>';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test urls with unmatched closing parentheses autolinks correctly', () => {
const testCases = [
{
testString: 'https://github.com/Expensify/ReactNativeChat/pull/645)',
resultString:
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>)',
},
{
testString: 'https://github.com/Expensify/ReactNativeChat/pull/test(645))',
resultString:
'<a href="https://github.com/Expensify/ReactNativeChat/pull/test(645)" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/test(645)</a>)',
},
{
testString: 'google.com/(toto))titi)',
resultString: '<a href="https://google.com/(toto)" target="_blank" rel="noreferrer noopener">google.com/(toto)</a>)titi)',
},
];
testCases.forEach((testCase) => {
expect(parser.replace(testCase.testString)).toBe(testCase.resultString);
});
});
test('Test urls ending with special characters followed by unmatched closing parentheses autolinks correctly', () => {
const testString =
'https://github.com/Expensify/ReactNativeChat/pull/645.) ' +
'https://github.com/Expensify/ReactNativeChat/pull/645$) ' +
'https://github.com/Expensify/ReactNativeChat/pull/645*) ' +
'https://github.com/Expensify/ReactNativeChat/pull/645+) ' +
'https://github.com/Expensify/ReactNativeChat/pull/645!) ' +
'https://github.com/Expensify/ReactNativeChat/pull/645,) ' +
'https://github.com/Expensify/ReactNativeChat/pull/645=) ' +
'https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings. ' +
'https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings.. ' +
'https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings..) ' +
'[https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings] ' +
'https://google.com/path?param=) ' +
'https://google.com/path#fragment!) ';
const resultString =
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>.) ' +
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>$) ' +
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>*) ' +
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>+) ' +
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>!) ' +
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>,) ' +
'<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>=) ' +
'<a href="https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings" target="_blank" rel="noreferrer noopener">https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings</a>. ' +
'<a href="https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings" target="_blank" rel="noreferrer noopener">https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings</a>.. ' +
'<a href="https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings" target="_blank" rel="noreferrer noopener">https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings</a>..) ' +
'[<a href="https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings" target="_blank" rel="noreferrer noopener">https://staging.new.expensify.com/get-assistance/WorkspaceGeneralSettings</a>] ' +
'<a href="https://google.com/path?param=" target="_blank" rel="noreferrer noopener">https://google.com/path?param=</a>) ' +
'<a href="https://google.com/path#fragment!" target="_blank" rel="noreferrer noopener">https://google.com/path#fragment!</a>) ';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test urls autolinks correctly', () => {
const testCases = [
{
testString: 'test@expensify.com https://www.expensify.com',
resultString:
'<a href="mailto:test@expensify.com">test@expensify.com</a> <a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com</a>',
},
{
testString: 'test@expensify.com-https://www.expensify.com',
resultString:
'<a href="mailto:test@expensify.com">test@expensify.com</a>-<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com</a>',
},
{
testString: 'test@expensify.com/https://www.expensify.com',
resultString:
'<a href="mailto:test@expensify.com">test@expensify.com</a>/<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com</a>',
},
{
testString: 'test@expensify.com?https://www.expensify.com',
resultString:
'<a href="mailto:test@expensify.com">test@expensify.com</a>?<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com</a>',
},
{
testString: 'test@expensify.com>https://www.expensify.com',
resultString:
'<a href="mailto:test@expensify.com">test@expensify.com</a>><a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com</a>',
},
{
testString: 'https://staging.new.expensify.com/details/test@expensify.com',
resultString:
'<a href="https://staging.new.expensify.com/details/test@expensify.com" target="_blank" rel="noreferrer noopener">https://staging.new.expensify.com/details/test@expensify.com</a>',
},
{
testString: 'staging.new.expensify.com/details',
resultString: '<a href="https://staging.new.expensify.com/details" target="_blank" rel="noreferrer noopener">staging.new.expensify.com/details</a>',
},
{
testString: 'https://www.expensify.com?name=test&email=test@expensify.com',
resultString:
'<a href="https://www.expensify.com?name=test&email=test@expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com?name=test&email=test@expensify.com</a>',
},
{
testString: 'https://staging.new.expensify.com/details?login=testing@gmail.com',
resultString:
'<a href="https://staging.new.expensify.com/details?login=testing@gmail.com" target="_blank" rel="noreferrer noopener">https://staging.new.expensify.com/details?login=testing@gmail.com</a>',
},
{
testString: 'staging.new.expensify.com/details?login=testing@gmail.com',
resultString:
'<a href="https://staging.new.expensify.com/details?login=testing@gmail.com" target="_blank" rel="noreferrer noopener">staging.new.expensify.com/details?login=testing@gmail.com</a>',
},
{
testString: 'http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled',
resultString:
'<a href="http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled" target="_blank" rel="noreferrer noopener">http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled</a>',
},
{
testString: '-https://www.expensify.com /https://www.expensify.com @https://www.expensify.com',
resultString:
'-<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com</a> /<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">https://www.expensify.com</a> @https://www.expensify.com',
},
{
testString: 'expensify.com -expensify.com @expensify.com',
resultString:
'<a href="https://expensify.com" target="_blank" rel="noreferrer noopener">expensify.com</a> -<a href="https://expensify.com" target="_blank" rel="noreferrer noopener">expensify.com</a> <mention-short>@expensify.com</mention-short>',
},
{
testString: 'https//www.expensify.com',
resultString: 'https//<a href="https://www.expensify.com" target="_blank" rel="noreferrer noopener">www.expensify.com</a>',
},
{
testString: '//www.expensify.com?name=test&email=test@expensify.com',
resultString:
'//<a href="https://www.expensify.com?name=test&email=test@expensify.com" target="_blank" rel="noreferrer noopener">www.expensify.com?name=test&email=test@expensify.com</a>',
},
{
testString: '//staging.new.expensify.com/details?login=testing@gmail.com',
resultString:
'//<a href="https://staging.new.expensify.com/details?login=testing@gmail.com" target="_blank" rel="noreferrer noopener">staging.new.expensify.com/details?login=testing@gmail.com</a>',
},
{
testString: '/details?login=testing@gmail.com',
resultString: '/details?login=<a href="mailto:testing@gmail.com">testing@gmail.com</a>',
},
{
testString: '?name=test&email=test@expensify.com',
resultString: '?name=test&email=<a href="mailto:test@expensify.com">test@expensify.com</a>',
},
{
testString: 'example.com/https://www.expensify.com',
resultString: '<a href="https://example.com/https://www.expensify.com" target="_blank" rel="noreferrer noopener">example.com/https://www.expensify.com</a>',
},
{
testString: 'test@gmail.com staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com',
resultString:
'<a href="mailto:test@gmail.com">test@gmail.com</a> <a href="https://staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com" target="_blank" rel="noreferrer noopener">staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com</a>',
},
{
testString: 'test@gmail.com //staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com',
resultString:
'<a href="mailto:test@gmail.com">test@gmail.com</a> //<a href="https://staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com" target="_blank" rel="noreferrer noopener">staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com</a>',
},
{
testString: 'test@gmail.com-https://staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com',
resultString:
'<a href="mailto:test@gmail.com">test@gmail.com</a>-<a href="https://staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com" target="_blank" rel="noreferrer noopener">https://staging.new.expensify.com/details?login=testing@gmail.com&redirectUrl=https://google.com</a>',
},
{
testString: 'test@gmail.com/https://example.com/google@email.com?email=asd@email.com',
resultString:
'<a href="mailto:test@gmail.com">test@gmail.com</a>/<a href="https://example.com/google@email.com?email=asd@email.com" target="_blank" rel="noreferrer noopener">https://example.com/google@email.com?email=asd@email.com</a>',
},
{
testString: 'test@gmail.com/test@gmail.com/https://example.com/google@email.com?email=asd@email.com',
resultString:
'<a href="mailto:test@gmail.com">test@gmail.com</a>/<a href="mailto:test@gmail.com">test@gmail.com</a>/<a href="https://example.com/google@email.com?email=asd@email.com" target="_blank" rel="noreferrer noopener">https://example.com/google@email.com?email=asd@email.com</a>',
},
];
testCases.forEach((testCase) => {
expect(parser.replace(testCase.testString)).toBe(testCase.resultString);
});
});
test('Test markdown style email link with various styles', () => {
const testString =
'Go to ~[Expensify](concierge@expensify.com)~ ' +
'_[Expensify](concierge@expensify.com)_ ' +
'*[Expensify](concierge@expensify.com)* ' +
'[Expensify!](no-concierge1@expensify.com) ' +
'[Applause](applausetester+qaabecciv@applause.expensifail.com) ' +
'[](concierge@expensify.com)' + // only parse autoEmail in ()
'[ ](concierge@expensify.com)' + // only parse autoEmail in () and keep spaces in []
'[ \n ](concierge@expensify.com)' + // only parse autoEmail in () and keep spaces in []
'[ Expensify ](concierge@expensify.com)' + // edge spaces in []
'[ Expensify Email ](concierge@expensify.com)' + // space between words in []
'[concierge@expensify.com](concierge@expensify.com)' + // same email between [] and ()
'[concierge@expensify.com](mailto:concierge@expensify.com)' + // same email between [] and () with mailto: in href
'[mailto:concierge@expensify.com](concierge@expensify.com)' + // same email between [] and () with mailto: in text
'[mailto:concierge@expensify.com](mailto:concierge@expensify.com)' + // same email between [] and () with mailto: in text and href
'[concierge-other@expensify.com](concierge@expensify.com)' + // different emails between [] and ()
'[(Expensify)](concierge@expensify.com)' + // () in []
'[Expensify [Test](test@expensify.com) Test](concierge@expensify.com)' + // []() in []
'[Expensify [Test](concierge@expensify.com)' + // [ in []
'[Expensify ]Test](concierge@expensify.com)'; // ] in []
const resultString =
'Go to <del><a href="mailto:concierge@expensify.com">Expensify</a></del> ' +
'<em><a href="mailto:concierge@expensify.com">Expensify</a></em> ' +
'<strong><a href="mailto:concierge@expensify.com">Expensify</a></strong> ' +
'<a href="mailto:no-concierge1@expensify.com">Expensify!</a> ' +
'<a href="mailto:applausetester+qaabecciv@applause.expensifail.com">Applause</a> ' +
'[](<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>)' +
'[ ](<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>)' +
'[ <br /> ](<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>)' +
'<a href="mailto:concierge@expensify.com">Expensify</a>' +
'<a href="mailto:concierge@expensify.com">Expensify Email</a>' +
'<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>' +
'<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>' +
'<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>' +
'<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>' +
'<a href="mailto:concierge@expensify.com">concierge-other@expensify.com</a>' +
'<a href="mailto:concierge@expensify.com">(Expensify)</a>' +
'[Expensify <a href="mailto:test@expensify.com">Test</a> Test](<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>)' +
'[Expensify <a href="mailto:concierge@expensify.com">Test</a>' +
'[Expensify ]Test](<a href="mailto:concierge@expensify.com">concierge@expensify.com</a>)';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test a url with multiple underscores', () => {
const testString = '[_example link_](https://www.facebook.com/hashtag/__main/?__eep__=6)';
const resultString = '<a href="https://www.facebook.com/hashtag/__main/?__eep__=6" target="_blank" rel="noreferrer noopener"><em>example link</em></a>';
expect(parser.replace(testString)).toBe(resultString);
});
test('Test general email link with various styles', () => {
const testString = 'Go to concierge@expensify.com ' + 'no-concierge@expensify.com ' + 'applausetester+qaabecciv@applause.expensifail.com ';
const resultString =
'Go to <a href="mailto:concierge@expensify.com">concierge@expensify.com</a> ' +
'<a href="mailto:no-concierge@expensify.com">no-concierge@expensify.com</a> ' +
'<a href="mailto:applausetester+qaabecciv@applause.expensifail.com">applausetester+qaabecciv@applause.expensifail.com</a> ';
expect(parser.replace(testString)).toBe(resultString);
});