-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path_markdown_test.v
More file actions
1215 lines (1068 loc) · 38.4 KB
/
Copy path_markdown_test.v
File metadata and controls
1215 lines (1068 loc) · 38.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
module gui
import gmarkdown as markdown
// Tests for markdown parser
fn test_markdown_header_h1() {
t := theme()
rt := markdown_to_rich_text('# Hello', MarkdownStyle{})
assert rt.runs.len >= 1
text_run := rt.runs.filter(it.text == 'Hello')[0] or { panic('no Hello run') }
assert text_run.style.size == t.b1.size
}
fn test_markdown_header_h2() {
t := theme()
rt := markdown_to_rich_text('## World', MarkdownStyle{})
assert rt.runs.len >= 1
text_run := rt.runs.filter(it.text == 'World')[0] or { panic('no World run') }
assert text_run.style.size == t.b2.size
}
fn test_markdown_bold() {
t := theme()
rt := markdown_to_rich_text('Hello **bold** world', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[0].text == 'Hello '
assert rt.runs[1].text == 'bold'
assert rt.runs[1].style.family == t.b3.family
assert rt.runs[2].text == ' world'
}
fn test_markdown_italic() {
t := theme()
rt := markdown_to_rich_text('Hello *italic* world', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[0].text == 'Hello '
assert rt.runs[1].text == 'italic'
assert rt.runs[1].style.family == t.i3.family
assert rt.runs[2].text == ' world'
}
fn test_markdown_inline_code() {
t := theme()
rt := markdown_to_rich_text('Use `code` here', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[0].text == 'Use '
assert rt.runs[1].text == 'code'
assert rt.runs[1].style.family == t.m3.family
assert rt.runs[2].text == ' here'
}
fn test_markdown_inline_code_highlight_tokens() {
style := MarkdownStyle{}
rt := markdown_to_rich_text('Use `x == 10` here', style)
op_run := rt.runs.filter(it.text == '==')[0] or { panic('no operator run') }
num_run := rt.runs.filter(it.text == '10')[0] or { panic('no number run') }
assert op_run.style.color == style.code_operator_color
assert num_run.style.color == style.code_number_color
}
fn test_markdown_link() {
rt := markdown_to_rich_text('Visit [vlang](https://vlang.io)', MarkdownStyle{})
assert rt.runs.len == 2
assert rt.runs[0].text == 'Visit '
assert rt.runs[1].text == 'vlang'
assert rt.runs[1].link == 'https://vlang.io'
assert rt.runs[1].style.underline == true
}
fn test_markdown_unordered_list() {
blocks := markdown_to_blocks('- item one', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_list == true
assert blocks[0].list_prefix == '• '
assert blocks[0].list_indent == 0
assert blocks[0].content.runs[0].text == 'item one'
}
fn test_markdown_ordered_list() {
blocks := markdown_to_blocks('1. first', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_list == true
assert blocks[0].list_prefix == '1. '
assert blocks[0].content.runs[0].text == 'first'
// Support ')' delimiter
blocks2 := markdown_to_blocks('1) second', MarkdownStyle{})
assert blocks2.len == 1
assert blocks2[0].is_list == true
assert blocks2[0].list_prefix == '1) '
assert blocks2[0].content.runs[0].text == 'second'
}
fn test_markdown_code_block() {
source := '```
fn main() {}
```'
rt := markdown_to_rich_text(source, MarkdownStyle{})
assert rt.runs.len >= 1
found_code := rich_text_to_string(rt).contains('fn main()')
assert found_code
}
fn test_markdown_paragraph_break() {
rt := markdown_to_rich_text('para1\n\npara2', MarkdownStyle{})
line_breaks := rt.runs.filter(it.text == '\n')
assert line_breaks.len >= 1
}
fn test_markdown_paragraph_continuation() {
// Single newline within paragraph joins lines with space
rt := markdown_to_rich_text('line one\nline two', MarkdownStyle{})
line_breaks := rt.runs.filter(it.text == '\n')
assert rt.runs[0].text.contains(' ')
assert line_breaks.len == 0
}
fn test_markdown_multiline_link() {
// Links spanning multiple lines should be parsed correctly
rt :=
markdown_to_rich_text('[CommonMark\nSpecification](https://commonmark.org/)', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 1
assert links[0].text == 'CommonMark Specification'
assert links[0].link == 'https://commonmark.org/'
}
fn test_markdown_link_with_backticks() {
// Backticks in link text should not confuse bracket matching
rt := markdown_to_rich_text('[`example`](https://example.com)', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 1
assert links[0].link == 'https://example.com'
}
fn test_markdown_bold_link_with_backticks() {
// Bold wrapped link with backticks in text
rt := markdown_to_rich_text('**[`code`](url)**', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 1
assert links[0].link == 'url'
}
// New tests for added features
fn test_markdown_strikethrough() {
rt := markdown_to_rich_text('Hello ~~strike~~ world', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[0].text == 'Hello '
assert rt.runs[1].text == 'strike'
assert rt.runs[1].style.strikethrough == true
assert rt.runs[2].text == ' world'
}
fn test_markdown_task_list_unchecked() {
blocks := markdown_to_blocks('- [ ] todo item', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_list == true
assert blocks[0].list_prefix == '☐ '
assert blocks[0].content.runs[0].text == 'todo item'
}
fn test_markdown_task_list_checked() {
blocks := markdown_to_blocks('- [x] done item', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_list == true
assert blocks[0].list_prefix == '☑ '
assert blocks[0].content.runs[0].text == 'done item'
}
fn test_markdown_nested_list() {
blocks := markdown_to_blocks('- outer\n - nested', MarkdownStyle{})
assert blocks.len == 2
assert blocks[0].is_list == true
assert blocks[0].list_prefix == '• '
assert blocks[0].list_indent == 0
assert blocks[1].is_list == true
assert blocks[1].list_prefix == '• '
assert blocks[1].list_indent == 1
}
fn test_markdown_blockquote() {
blocks := markdown_to_blocks('> quoted text', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_blockquote == true
assert blocks[0].content.runs[0].text == 'quoted text'
}
fn test_markdown_image() {
blocks := markdown_to_blocks('', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_image == true
assert blocks[0].image_alt == 'alt text'
assert blocks[0].image_src == 'image.png'
}
fn test_markdown_horizontal_rule() {
blocks := markdown_to_blocks('above\n\n---\n\nbelow', MarkdownStyle{})
hr_blocks := blocks.filter(it.is_hr)
assert hr_blocks.len == 1
}
fn test_markdown_escape_chars() {
rt := markdown_to_rich_text(r'\*not italic\*', MarkdownStyle{})
assert rt.runs.len == 1
assert rt.runs[0].text == '*not italic*'
}
fn test_markdown_bold_italic() {
rt := markdown_to_rich_text('Use ***both*** styles', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[1].text == 'both'
}
fn test_markdown_underscore_bold() {
t := theme()
rt := markdown_to_rich_text('Use __bold__ here', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[1].text == 'bold'
assert rt.runs[1].style.family == t.b3.family
}
fn test_markdown_underscore_italic() {
t := theme()
rt := markdown_to_rich_text('Use _italic_ here', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[1].text == 'italic'
assert rt.runs[1].style.family == t.i3.family
}
fn test_markdown_autolink_url() {
rt := markdown_to_rich_text('Visit <https://example.com> now', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[1].text == 'https://example.com'
assert rt.runs[1].link == 'https://example.com'
}
fn test_markdown_autolink_email() {
rt := markdown_to_rich_text('Email <test@example.com> please', MarkdownStyle{})
assert rt.runs.len == 3
assert rt.runs[1].text == 'test@example.com'
assert rt.runs[1].link == 'mailto:test@example.com'
}
fn test_markdown_nested_blockquote() {
blocks := markdown_to_blocks('> > nested quote', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_blockquote == true
assert blocks[0].blockquote_depth == 2
}
fn test_markdown_table() {
blocks := markdown_to_blocks('| A | B |\n|---|---|\n| 1 | 2 |', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_table == true
}
// Helper to extract text from RichText for test comparison
fn rich_text_to_string(rt RichText) string {
mut s := ''
for run in rt.runs {
s += run.text
}
return s
}
// Helper to extract text from []MdRun for submodule tests
fn md_runs_to_string(runs []markdown.MdRun) string {
mut s := ''
for run in runs {
s += run.text
}
return s
}
fn test_markdown_table_parsing() {
parsed := markdown.parse_md_table('| A | B |\n|---|---|\n| 1 | 2 |'.split('\n'),
map[string]string{}, map[string]string{}) or { panic('parse failed') }
assert md_runs_to_string(parsed.headers[0]) == 'A'
assert md_runs_to_string(parsed.headers[1]) == 'B'
assert parsed.rows.len == 1
assert md_runs_to_string(parsed.rows[0][0]) == '1'
assert md_runs_to_string(parsed.rows[0][1]) == '2'
}
fn test_markdown_table_alignments() {
parsed := markdown.parse_md_table('| L | C | R |\n|:---|:---:|---:|\n| a | b | c |'.split('\n'),
map[string]string{}, map[string]string{}) or { panic('parse failed') }
assert parsed.alignments.len == 3
assert parsed.alignments[0] == .start
assert parsed.alignments[1] == .center
assert parsed.alignments[2] == .end_
}
fn test_markdown_table_no_outer_pipes() {
parsed := markdown.parse_md_table('A | B\n---|---\n1 | 2'.split('\n'), map[string]string{},
map[string]string{}) or { panic('parse failed') }
assert md_runs_to_string(parsed.headers[0]) == 'A'
assert md_runs_to_string(parsed.headers[1]) == 'B'
assert parsed.rows.len == 1
assert md_runs_to_string(parsed.rows[0][0]) == '1'
assert md_runs_to_string(parsed.rows[0][1]) == '2'
}
fn test_markdown_table_empty_cells() {
parsed := markdown.parse_md_table('| A | B | C |\n|---|---|---|\n| 1 | | 3 |'.split('\n'),
map[string]string{}, map[string]string{}) or { panic('parse failed') }
assert md_runs_to_string(parsed.rows[0][0]) == '1'
assert md_runs_to_string(parsed.rows[0][1]) == ''
assert md_runs_to_string(parsed.rows[0][2]) == '3'
}
fn test_markdown_table_inline_formatting() {
parsed := markdown.parse_md_table('| **bold** | _italic_ | `code` |\n|---|---|---|\n| [link](url) | a | b |'.split('\n'),
map[string]string{}, map[string]string{}) or { panic('parse failed') }
// Headers should have inline formatting parsed
assert md_runs_to_string(parsed.headers[0]) == 'bold'
assert md_runs_to_string(parsed.headers[1]) == 'italic'
assert md_runs_to_string(parsed.headers[2]) == 'code'
// Check that bold header has runs
assert parsed.headers[0].len >= 1
// Cell with link
assert md_runs_to_string(parsed.rows[0][0]) == 'link'
assert parsed.rows[0][0][0].link == 'url'
}
fn test_markdown_table_invalid_separator() {
// Separator without dashes should fail
result := markdown.parse_md_table('| A | B |\n|:::|:::|\n| 1 | 2 |'.split('\n'),
map[string]string{}, map[string]string{})
assert result == none
}
fn test_markdown_table_no_separator() {
// No separator row should fail
result := markdown.parse_md_table('| A | B |\n| 1 | 2 |'.split('\n'), map[string]string{},
map[string]string{})
assert result == none
}
fn test_markdown_table_without_leading_pipes() {
// Tables without leading pipes should still be recognized
source := 'Header A | Header B\n---------|----------\nCell 1 | Cell 2'
blocks := markdown_to_blocks(source, MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_table == true
if tbl := blocks[0].table_data {
assert tbl.headers.len == 2
assert rich_text_to_string(tbl.headers[0]) == 'Header A'
assert rich_text_to_string(tbl.rows[0][1]) == 'Cell 2'
} else {
assert false // table_data should exist
}
}
fn test_markdown_prose_with_pipe_not_table() {
// Prose containing | should NOT be captured as table
source := 'Use a|b syntax for alternatives.'
blocks := markdown_to_blocks(source, MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_table == false
}
fn test_markdown_footnote_basic() {
source := 'See note[^1] here\n\n[^1]: This is the footnote content.'
rt := markdown_to_rich_text(source, MarkdownStyle{})
// Should have footnote marker with tooltip
fn_runs := rt.runs.filter(it.tooltip != '')
assert fn_runs.len == 1
assert fn_runs[0].text == '\u2009[1]' // thin space prefix
assert fn_runs[0].tooltip == 'This is the footnote content.'
}
fn test_markdown_footnote_named() {
source := 'See[^note] here\n\n[^note]: Named footnote.'
rt := markdown_to_rich_text(source, MarkdownStyle{})
fn_runs := rt.runs.filter(it.tooltip != '')
assert fn_runs.len == 1
assert fn_runs[0].text == '\u2009[note]' // thin space prefix
assert fn_runs[0].tooltip == 'Named footnote.'
}
fn test_markdown_footnote_undefined() {
rt := markdown_to_rich_text('See note[^1] here', MarkdownStyle{})
// Undefined footnote rendered as literal
found := rt.runs.any(it.text.contains('[^1]'))
assert found
}
fn test_markdown_footnote_multiline() {
source := 'Text[^1]\n\n[^1]: First line\n continuation.'
rt := markdown_to_rich_text(source, MarkdownStyle{})
fn_runs := rt.runs.filter(it.tooltip != '')
assert fn_runs.len == 1
assert fn_runs[0].tooltip == 'First line continuation.'
}
fn test_markdown_footnote_multiline_blank() {
// Blank line between definition and indented continuation - preserved as paragraph break
source := 'Text[^1]\n\n[^1]: First paragraph.\n\n Second paragraph.'
rt := markdown_to_rich_text(source, MarkdownStyle{})
fn_runs := rt.runs.filter(it.tooltip != '')
assert fn_runs.len == 1
assert fn_runs[0].tooltip == 'First paragraph.\n\nSecond paragraph.'
}
fn test_markdown_reference_link() {
blocks := markdown_to_blocks('[link][ref]\n\n[ref]: https://example.com', MarkdownStyle{})
assert blocks.len == 1
links := blocks[0].content.runs.filter(it.link != '')
assert links.len == 1
assert links[0].text == 'link'
assert links[0].link == 'https://example.com'
}
fn test_markdown_implicit_reference_link() {
blocks := markdown_to_blocks('[Example][]\n\n[Example]: https://example.com', MarkdownStyle{})
assert blocks.len == 1
links := blocks[0].content.runs.filter(it.link != '')
assert links.len == 1
assert links[0].text == 'Example'
assert links[0].link == 'https://example.com'
}
fn test_markdown_shortcut_reference_link() {
blocks := markdown_to_blocks('[Example]\n\n[Example]: https://example.com', MarkdownStyle{})
assert blocks.len == 1
links := blocks[0].content.runs.filter(it.link != '')
assert links.len == 1
assert links[0].text == 'Example'
assert links[0].link == 'https://example.com'
}
fn test_markdown_reference_link_undefined() {
rt := markdown_to_rich_text('See [link][undefined] here', MarkdownStyle{})
// No definition, rendered as literal
found := rt.runs.any(it.text.contains('['))
assert found
}
fn test_markdown_list_continuation() {
// List item with continuation line should join with space, not line break
blocks := markdown_to_blocks('- item one\n continues', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_list == true
// Content should be joined: "item one continues"
found := blocks[0].content.runs.any(it.text.contains('item one continues'))
assert found
}
fn test_markdown_ordered_list_double_digit() {
blocks := markdown_to_blocks('10. tenth item', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_list == true
assert blocks[0].list_prefix == '10. '
assert blocks[0].content.runs[0].text == 'tenth item'
}
fn test_markdown_definition_list_simple() {
blocks := markdown_to_blocks('Term\n: Definition', MarkdownStyle{})
assert blocks.len == 2
assert blocks[0].is_def_term == true
assert blocks[0].content.runs[0].text == 'Term'
assert blocks[1].is_def_value == true
assert blocks[1].content.runs[0].text == 'Definition'
}
fn test_markdown_definition_list_multiline() {
blocks := markdown_to_blocks('Term\n: First line\n continues here', MarkdownStyle{})
assert blocks.len == 2
assert blocks[0].is_def_term == true
assert blocks[1].is_def_value == true
found := blocks[1].content.runs.any(it.text.contains('First line continues here'))
assert found
}
fn test_markdown_definition_list_multiple_defs() {
blocks := markdown_to_blocks('Term\n: Primary def\n: Alternative def', MarkdownStyle{})
assert blocks.len == 3
assert blocks[0].is_def_term == true
assert blocks[1].is_def_value == true
assert blocks[1].content.runs[0].text == 'Primary def'
assert blocks[2].is_def_value == true
assert blocks[2].content.runs[0].text == 'Alternative def'
}
fn test_markdown_abbreviation_basic() {
source := '*[HTML]: Hyper Text Markup Language\n\nThe HTML spec.'
blocks := markdown_to_blocks(source, MarkdownStyle{})
assert blocks.len == 1
runs := blocks[0].content.runs
// Should have: "The " + abbr("HTML") + " spec."
assert runs.len >= 3
found_abbr := runs.any(it.tooltip == 'Hyper Text Markup Language' && it.text == 'HTML')
assert found_abbr
}
fn test_markdown_abbreviation_word_boundary() {
source := '*[HTML]: Hyper Text Markup Language\n\nHTMLX is not HTML.'
blocks := markdown_to_blocks(source, MarkdownStyle{})
runs := blocks[0].content.runs
// HTML should match, HTMLX should not
abbr_runs := runs.filter(it.tooltip != '')
assert abbr_runs.len == 1
assert abbr_runs[0].text == 'HTML'
}
fn test_markdown_setext_h1() {
t := theme()
rt := markdown_to_rich_text('Hello\n=====', MarkdownStyle{})
text_run := rt.runs.filter(it.text == 'Hello')[0] or { panic('no Hello run') }
assert text_run.style.size == t.b1.size
}
fn test_markdown_setext_h2() {
t := theme()
rt := markdown_to_rich_text('World\n-----', MarkdownStyle{})
text_run := rt.runs.filter(it.text == 'World')[0] or { panic('no World run') }
assert text_run.style.size == t.b2.size
}
// Security tests
fn test_markdown_blocks_javascript_url() {
// javascript: URLs should be rejected - link empty, text unstyled
rt := markdown_to_rich_text('[click](javascript:alert(1))', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 0
// Text should still appear
found := rt.runs.any(it.text == 'click')
assert found
}
fn test_markdown_blocks_data_url() {
// data: URLs should be rejected
rt :=
markdown_to_rich_text('[click](data:text/html,<script>alert(1)</script>)', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 0
}
fn test_markdown_blocks_vbscript_url() {
// vbscript: URLs should be rejected
rt := markdown_to_rich_text('[click](vbscript:msgbox(1))', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 0
}
fn test_markdown_blocks_safe_urls() {
// http, https, mailto should work
rt := markdown_to_rich_text('[a](http://x) [b](https://y) [c](mailto:z@z)', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 3
assert links[0].link == 'http://x'
assert links[1].link == 'https://y'
assert links[2].link == 'mailto:z@z'
}
fn test_markdown_blocks_relative_url() {
// Relative URLs (no protocol) should work
rt := markdown_to_rich_text('[page](./other.html)', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 1
assert links[0].link == './other.html'
}
fn test_markdown_image_path_traversal() {
// Path traversal should be blocked
blocks := markdown_to_blocks('', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_image == true
assert blocks[0].image_src == '' // blocked
}
fn test_markdown_image_absolute_path() {
// Absolute paths should be blocked
blocks := markdown_to_blocks('', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].image_src == ''
}
fn test_markdown_image_safe_path() {
// Relative paths without traversal should work
blocks := markdown_to_blocks('', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].image_src == 'images/photo.png'
}
fn test_markdown_autolink_javascript() {
// javascript: in autolink should be rejected
rt := markdown_to_rich_text('<javascript:alert(1)>', MarkdownStyle{})
links := rt.runs.filter(it.link != '')
assert links.len == 0
}
fn test_markdown_reference_link_javascript() {
// javascript: URLs in reference links should be rejected
blocks := markdown_to_blocks('[click][x]\n\n[x]: javascript:alert(1)', MarkdownStyle{})
links := blocks[0].content.runs.filter(it.link != '')
assert links.len == 0
}
// Self-synchronizing parser tests
fn test_markdown_tilde_fence() {
source := '~~~
fn main() {}
~~~'
rt := markdown_to_rich_text(source, MarkdownStyle{})
assert rt.runs.len >= 1
found_code := rich_text_to_string(rt).contains('fn main()')
assert found_code
}
fn test_markdown_tilde_fence_with_lang() {
source := '~~~v
let x = 1
~~~'
blocks := markdown_to_blocks(source, MarkdownStyle{})
assert blocks.len >= 1
assert blocks.any(it.is_code)
}
fn test_markdown_mismatched_fence_ignored() {
// Opening ``` should not be closed by ~~~
source := '```
code here
~~~
still code
```'
blocks := markdown_to_blocks(source, MarkdownStyle{})
code_blocks := blocks.filter(it.is_code)
assert code_blocks.len == 1
// Content should include the ~~~ line since it doesn't close backtick fence
content := rich_text_to_string(code_blocks[0].content)
assert content.contains('code here')
assert content.contains('still code')
}
fn test_markdown_fenced_v_highlight() {
style := MarkdownStyle{}
source := '```v
fn main() {
return 1
}
```'
blocks := markdown_to_blocks(source, style)
code := blocks.filter(it.is_code)[0] or { panic('no code block') }
assert code.code_language == 'v'
fn_run := code.content.runs.filter(it.text == 'fn')[0] or { panic('no fn run') }
num_run := code.content.runs.filter(it.text == '1')[0] or { panic('no number run') }
assert fn_run.style.color == style.code_keyword_color
assert num_run.style.color == style.code_number_color
}
fn test_markdown_fenced_python_highlight() {
style := MarkdownStyle{}
source := '```python
# note
if x:
pass
```'
blocks := markdown_to_blocks(source, style)
code := blocks.filter(it.is_code)[0] or { panic('no code block') }
comment_run := code.content.runs.filter(it.text == '# note')[0] or { panic('no comment run') }
kw_run := code.content.runs.filter(it.text == 'if')[0] or { panic('no keyword run') }
assert comment_run.style.color == style.code_comment_color
assert kw_run.style.color == style.code_keyword_color
}
fn test_markdown_fenced_unknown_language_generic_highlight() {
style := MarkdownStyle{}
source := '```foo
x == 42
```'
blocks := markdown_to_blocks(source, style)
code := blocks.filter(it.is_code)[0] or { panic('no code block') }
assert code.code_language == 'foo'
op_run := code.content.runs.filter(it.text == '==')[0] or { panic('no operator run') }
num_run := code.content.runs.filter(it.text == '42')[0] or { panic('no number run') }
assert op_run.style.color == style.code_operator_color
assert num_run.style.color == style.code_number_color
}
fn test_code_block_state_detection() {
source := 'text
```
code
```
more text'
scanner := markdown.new_scanner(source)
// At index 0: not in code block
state0 := markdown.detect_code_block_state(scanner, 0)
assert state0.in_code_block == false
// At index 2 (inside code block)
state2 := markdown.detect_code_block_state(scanner, 2)
assert state2.in_code_block == true
assert state2.fence_char == `\``
// At index 4 (after code block closed)
state4 := markdown.detect_code_block_state(scanner, 4)
assert state4.in_code_block == false
}
fn test_code_block_state_tilde() {
source := '~~~
code'
scanner := markdown.new_scanner(source)
state := markdown.detect_code_block_state(scanner, 2)
assert state.in_code_block == true
assert state.fence_char == `~`
}
fn test_parse_code_fence() {
// Backtick fence
if fence := markdown.parse_code_fence('```') {
assert fence.char == `\``
assert fence.count == 3
} else {
assert false
}
// Tilde fence
if fence := markdown.parse_code_fence('~~~python') {
assert fence.char == `~`
assert fence.count == 3
} else {
assert false
}
// Longer fence
if fence := markdown.parse_code_fence('`````') {
assert fence.char == `\``
assert fence.count == 5
} else {
assert false
}
// Not a fence
assert markdown.parse_code_fence('hello') == none
assert markdown.parse_code_fence('``') == none
}
fn test_bounded_blockquote() {
// Blockquote should be bounded (verify no infinite loop)
mut lines := []string{cap: 150}
for _ in 0 .. 150 {
lines << '> line'
}
source := lines.join('\n')
blocks := markdown_to_blocks(source, MarkdownStyle{})
// Should have at least one blockquote block, bounded at 100 lines
assert blocks.len >= 1
assert blocks[0].is_blockquote == true
}
fn test_bounded_table() {
// Table should be bounded
mut lines := []string{cap: 10}
lines << '| A | B |'
lines << '|---|---|'
for _ in 0 .. 5 {
lines << '| 1 | 2 |'
}
source := lines.join('\n')
blocks := markdown_to_blocks(source, MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_table == true
}
fn test_unclosed_code_block() {
// Unclosed code block should still be rendered
source := '```
code without closing'
blocks := markdown_to_blocks(source, MarkdownStyle{})
code_blocks := blocks.filter(it.is_code)
assert code_blocks.len == 1
assert rich_text_to_string(code_blocks[0].content) == 'code without closing'
}
fn test_markdown_highlight_inline_limit_fallback() {
style := MarkdownStyle{}
code := 'x'.repeat(markdown.max_inline_code_highlight_bytes + 10)
// Inline code exceeding limit falls back to single unstyled run
rt := markdown_to_rich_text('`${code}`', style)
code_runs := rt.runs.filter(it.text == code)
assert code_runs.len == 1
assert code_runs[0].style.color == style.code.color
}
fn test_markdown_highlight_block_depth_limit_no_hang() {
openers := '/*'.repeat(markdown.max_highlight_comment_depth + 1)
closers := '*/'.repeat(markdown.max_highlight_comment_depth + 1)
code := '${openers}x${closers}'
source := '```v\n${code}\n```'
blocks := markdown_to_blocks(source, MarkdownStyle{})
code_blocks := blocks.filter(it.is_code)
assert code_blocks.len == 1
assert rich_text_to_string(code_blocks[0].content) == code
}
fn test_markdown_highlight_string_scan_limit_no_hang() {
code := '"' + 'a'.repeat(markdown.max_highlight_string_scan_bytes + 20)
source := '```js\n${code}\n```'
blocks := markdown_to_blocks(source, MarkdownStyle{})
code_blocks := blocks.filter(it.is_code)
assert code_blocks.len == 1
assert rich_text_to_string(code_blocks[0].content) == code
}
// Math tests
fn test_markdown_display_math_single_line() {
blocks := markdown_to_blocks(r'$$ E=mc^2 $$', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_math == true
assert blocks[0].math_latex == 'E=mc^2'
}
fn test_markdown_display_math_multi_line() {
source := '\$\$
\\int_0^1 x^2 dx
\$\$'
blocks := markdown_to_blocks(source, MarkdownStyle{})
math_blocks := blocks.filter(it.is_math)
assert math_blocks.len == 1
assert math_blocks[0].math_latex.contains('\\int_0^1')
}
fn test_markdown_math_code_fence() {
source := '```math
\\sum_{n=1}^{\\infty} \\frac{1}{n^2}
```'
blocks := markdown_to_blocks(source, MarkdownStyle{})
math_blocks := blocks.filter(it.is_math)
assert math_blocks.len == 1
assert math_blocks[0].math_latex.contains('\\sum')
// Should NOT be a code block
code_blocks := blocks.filter(it.is_code)
assert code_blocks.len == 0
}
fn test_markdown_inline_math() {
rt := markdown_to_rich_text(r'The equation $E=mc^2$ is famous.', MarkdownStyle{})
math_runs := rt.runs.filter(it.math_id != '')
assert math_runs.len == 1
assert math_runs[0].math_latex == 'E=mc^2'
assert math_runs[0].text == '\uFFFC'
}
fn test_markdown_inline_math_dollar_price() {
// $10 should NOT be treated as math (digit after $)
rt := markdown_to_rich_text(r'The price is $10 today.', MarkdownStyle{})
math_runs := rt.runs.filter(it.math_id != '')
assert math_runs.len == 0
}
fn test_markdown_inline_math_dollar_space() {
// $ x$ should NOT be math (space after opening $)
rt := markdown_to_rich_text(r'Value $ x$ here.', MarkdownStyle{})
math_runs := rt.runs.filter(it.math_id != '')
assert math_runs.len == 0
}
fn test_markdown_escaped_dollar() {
// Escaped \$ should be literal
rt := markdown_to_rich_text(r'Cost is \$5.', MarkdownStyle{})
math_runs := rt.runs.filter(it.math_id != '')
assert math_runs.len == 0
found := rt.runs.any(it.text.contains('$'))
assert found
}
fn test_markdown_inline_math_not_preceded_by_digit() {
// Digit before $ should prevent math
rt := markdown_to_rich_text(r'Get 5$x$ here.', MarkdownStyle{})
math_runs := rt.runs.filter(it.math_id != '')
assert math_runs.len == 0
}
// Edge case tests for parser fixes
fn test_markdown_escaped_closing_delimiter() {
t := theme()
// Escaped \* prevents ** from closing bold
rt := markdown_to_rich_text(r'**text\**', MarkdownStyle{})
// Bold requires matching **, but \* escapes one star.
// Verify no run has bold family (bold not matched).
for run in rt.runs {
assert run.style.family != t.b3.family
}
}
fn test_markdown_table_escaped_pipe() {
parsed := markdown.parse_md_table(r'| a \| b | c |
|---|---|
| d \| e | f |'.split('\n'),
map[string]string{}, map[string]string{}) or { panic('parse failed') }
// Should have 2 columns, not 3
assert parsed.headers.len == 2
assert md_runs_to_string(parsed.headers[0]) == 'a | b'
assert md_runs_to_string(parsed.headers[1]) == 'c'
assert md_runs_to_string(parsed.rows[0][0]) == 'd | e'
assert md_runs_to_string(parsed.rows[0][1]) == 'f'
}
fn test_markdown_blockquote_depth_first_line() {
// Depth should reflect first line, not max across all lines
blocks := markdown_to_blocks('> a\n>> b\n> c', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_blockquote == true
assert blocks[0].blockquote_depth == 1
}
fn test_markdown_math_block_many_lines() {
// Math block with >20 lines (cap consistency)
mut lines := []string{cap: 30}
lines << '$$'
for i in 0 .. 25 {
lines << 'x_{${i}} + y_{${i}}'
}
lines << '$$'
blocks := markdown_to_blocks(lines.join('\n'), MarkdownStyle{})
math_blocks := blocks.filter(it.is_math)
assert math_blocks.len == 1
assert math_blocks[0].math_latex.contains('x_{24}')
}
fn test_markdown_ordered_list_paren() {
// Ordered list with ) separator
blocks := markdown_to_blocks('1) item', MarkdownStyle{})
assert blocks.len == 1
assert blocks[0].is_list == true
assert blocks[0].list_prefix == '1) '
assert blocks[0].content.runs[0].text == 'item'
}
fn test_markdown_find_closing_trailing_backslash() {
// Backslash as last char should not cause out-of-bounds
pos := markdown.find_closing(r'abc\', 0, `x`)
assert pos == -1
}
fn test_markdown_find_double_closing_trailing_backslash() {
pos := markdown.find_double_closing(r'abc\', 0, `*`)
assert pos == -1
}
fn test_markdown_find_triple_closing_trailing_backslash() {
pos := markdown.find_triple_closing(r'abc\', 0, `*`)
assert pos == -1
}
// S1: sanitize_latex nested payload bypass
fn test_sanitize_latex_nested_bypass() {
// \inp + \input + ut → after one pass: \input
assert sanitize_latex(r'\inp\inputut') == ''
// Nested \include: \inc + \include + lude → \include
assert sanitize_latex(r'\inc\includelude') == ''
// Already-blocked single command
assert sanitize_latex(r'\write18') == ''
// Clean input unchanged
assert sanitize_latex(r'\frac{1}{2}') == r'\frac{1}{2}'
}
// S2: is_safe_url percent-encoded protocol bypass
fn test_is_safe_url_percent_encoded_javascript() {
// %6A = 'j' → javascript:
assert markdown.is_safe_url('%6Aavascript:alert(1)') == false
// Mixed case + percent encoding
assert markdown.is_safe_url('%6a%61vascript:alert(1)') == false
// Percent-encoded data:
assert markdown.is_safe_url('%64ata:text/html,<script>') == false
// Normal safe URLs still work
assert markdown.is_safe_url('https://example.com') == true
assert markdown.is_safe_url('mailto:a@b.com') == true
assert markdown.is_safe_url('./relative') == true
}
// S3: empty link definition key
fn test_empty_link_definition_rejected() {
// "[]: url" should not register as link def
scanner := markdown.new_scanner('[]: http://evil.com')
link_defs, _, _ := markdown.collect_metadata(scanner)
assert link_defs.len == 0
}
fn test_is_link_definition_empty_key() {
assert markdown.is_link_definition('[]: http://example.com') == false
assert markdown.is_link_definition('[a]: http://example.com') == true
}
// Table column limit
fn test_table_column_limit() {
// Build table with 150 columns (exceeds max_table_columns=100)
mut hdr := []string{cap: 150}
mut sep := []string{cap: 150}
mut row := []string{cap: 150}