/
marked-spec.js
1062 lines (968 loc) · 29.2 KB
/
marked-spec.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
import { marked, Renderer, Slugger, lexer, parseInline, use, getDefaults, walkTokens as _walkTokens } from '../../src/marked.js';
describe('Test heading ID functionality', () => {
it('should add id attribute by default', () => {
const renderer = new Renderer();
const slugger = new Slugger();
const header = renderer.heading('test', 1, 'test', slugger);
expect(header).toBe('<h1 id="test">test</h1>\n');
});
it('should NOT add id attribute when options set false', () => {
const renderer = new Renderer({ headerIds: false });
const header = renderer.heading('test', 1, 'test');
expect(header).toBe('<h1>test</h1>\n');
});
});
describe('Test slugger functionality', () => {
it('should use lowercase slug', () => {
const slugger = new Slugger();
expect(slugger.slug('Test')).toBe('test');
});
it('should be unique to avoid collisions 1280', () => {
const slugger = new Slugger();
expect(slugger.slug('test')).toBe('test');
expect(slugger.slug('test')).toBe('test-1');
expect(slugger.slug('test')).toBe('test-2');
});
it('should be unique when slug ends with number', () => {
const slugger = new Slugger();
expect(slugger.slug('test 1')).toBe('test-1');
expect(slugger.slug('test')).toBe('test');
expect(slugger.slug('test')).toBe('test-2');
});
it('should be unique when slug ends with hyphen number', () => {
const slugger = new Slugger();
expect(slugger.slug('foo')).toBe('foo');
expect(slugger.slug('foo')).toBe('foo-1');
expect(slugger.slug('foo 1')).toBe('foo-1-1');
expect(slugger.slug('foo-1')).toBe('foo-1-2');
expect(slugger.slug('foo')).toBe('foo-2');
});
it('should allow non-latin chars', () => {
const slugger = new Slugger();
expect(slugger.slug('привет')).toBe('привет');
});
it('should remove ampersands 857', () => {
const slugger = new Slugger();
expect(slugger.slug('This & That Section')).toBe('this--that-section');
});
it('should remove periods', () => {
const slugger = new Slugger();
expect(slugger.slug('file.txt')).toBe('filetxt');
});
it('should remove html tags', () => {
const slugger = new Slugger();
expect(slugger.slug('<em>html</em>')).toBe('html');
});
it('should not increment seen when using dryrun option', () => {
const slugger = new Slugger();
expect(slugger.slug('<h1>This Section</h1>', { dryrun: true })).toBe('this-section');
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
});
it('should still return the next unique id when using dryrun', () => {
const slugger = new Slugger();
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
expect(slugger.slug('<h1>This Section</h1>', { dryrun: true })).toBe('this-section-1');
});
it('should be repeatable in a sequence', () => {
const slugger = new Slugger();
expect(slugger.slug('foo')).toBe('foo');
expect(slugger.slug('foo')).toBe('foo-1');
expect(slugger.slug('foo')).toBe('foo-2');
expect(slugger.slug('foo', { dryrun: true })).toBe('foo-3');
expect(slugger.slug('foo', { dryrun: true })).toBe('foo-3');
expect(slugger.slug('foo')).toBe('foo-3');
expect(slugger.slug('foo')).toBe('foo-4');
});
});
describe('Test paragraph token type', () => {
it('should use the "paragraph" type on top level', () => {
const md = 'A Paragraph.\n\n> A blockquote\n\n- list item\n';
const tokens = lexer(md);
expect(tokens[0].type).toBe('paragraph');
expect(tokens[2].tokens[0].type).toBe('paragraph');
expect(tokens[3].items[0].tokens[0].type).toBe('text');
});
});
describe('changeDefaults', () => {
it('should change global defaults', async() => {
const { defaults, changeDefaults } = await import('../../src/defaults.js');
expect(defaults.test).toBeUndefined();
changeDefaults({ test: true });
expect((await import('../../src/defaults.js')).defaults.test).toBe(true);
});
});
describe('inlineLexer', () => {
it('should send html to renderer.html', () => {
const renderer = new Renderer();
spyOn(renderer, 'html').and.callThrough();
const md = 'HTML Image: <img alt="MY IMAGE" src="example.png" />';
marked(md, { renderer });
expect(renderer.html).toHaveBeenCalledWith('<img alt="MY IMAGE" src="example.png" />');
});
});
describe('parseInline', () => {
it('should parse inline tokens', () => {
const md = '**strong** _em_';
const html = parseInline(md);
expect(html).toBe('<strong>strong</strong> <em>em</em>');
});
it('should not parse block tokens', () => {
const md = '# header\n\n_em_';
const html = parseInline(md);
expect(html).toBe('# header\n\n<em>em</em>');
});
});
describe('use extension', () => {
it('should use custom block tokenizer + renderer extensions', () => {
const underline = {
name: 'underline',
level: 'block',
tokenizer(src) {
const rule = /^:([^\n]*)(?:\n|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'underline',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[1].trim() // You can add additional properties to your tokens to pass along to the renderer
};
}
},
renderer(token) {
return `<u>${token.text}</u>\n`;
}
};
use({ extensions: [underline] });
let html = marked('Not Underlined\n:Underlined\nNot Underlined');
expect(html).toBe('<p>Not Underlined\n:Underlined\nNot Underlined</p>\n');
html = marked('Not Underlined\n\n:Underlined\n\nNot Underlined');
expect(html).toBe('<p>Not Underlined</p>\n<u>Underlined</u>\n<p>Not Underlined</p>\n');
});
it('should interrupt paragraphs if using "start" property', () => {
const underline = {
extensions: [{
name: 'underline',
level: 'block',
start(src) { return src.indexOf(':'); },
tokenizer(src) {
const rule = /^:([^\n]*):(?:\n|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'underline',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[1].trim() // You can add additional properties to your tokens to pass along to the renderer
};
}
},
renderer(token) {
return `<u>${token.text}</u>\n`;
}
}]
};
use(underline);
const html = marked('Not Underlined A\n:Underlined B:\nNot Underlined C\n:Not Underlined D');
expect(html).toBe('<p>Not Underlined A</p>\n<u>Underlined B</u>\n<p>Not Underlined C\n:Not Underlined D</p>\n');
});
it('should use custom inline tokenizer + renderer extensions', () => {
const underline = {
name: 'underline',
level: 'inline',
start(src) { return src.indexOf('='); },
tokenizer(src) {
const rule = /^=([^=]+)=/;
const match = rule.exec(src);
if (match) {
return {
type: 'underline',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[1].trim() // You can add additional properties to your tokens to pass along to the renderer
};
}
},
renderer(token) {
return `<u>${token.text}</u>`;
}
};
use({ extensions: [underline] });
const html = marked('Not Underlined =Underlined= Not Underlined');
expect(html).toBe('<p>Not Underlined <u>Underlined</u> Not Underlined</p>\n');
});
it('should handle interacting block and inline extensions', () => {
const descriptionlist = {
name: 'descriptionList',
level: 'block',
start(src) {
const match = src.match(/:[^:\n]/);
if (match) {
return match.index;
}
},
tokenizer(src, tokens) {
const rule = /^(?::[^:\n]+:[^:\n]*(?:\n|$))+/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'descriptionList',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[0].trim(), // You can add additional properties to your tokens to pass along to the renderer
tokens: []
};
this.lexer.inlineTokens(token.text, token.tokens);
return token;
}
},
renderer(token) {
return `<dl>${this.parser.parseInline(token.tokens)}\n</dl>`;
}
};
const description = {
name: 'description',
level: 'inline',
start(src) { return src.indexOf(':'); },
tokenizer(src, tokens) {
const rule = /^:([^:\n]+):([^:\n]*)(?:\n|$)/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'description',
raw: match[0],
dt: [],
dd: []
};
this.lexer.inline(match[1].trim(), token.dt);
this.lexer.inline(match[2].trim(), token.dd);
return token;
}
},
renderer(token) {
return `\n<dt>${this.parser.parseInline(token.dt)}</dt><dd>${this.parser.parseInline(token.dd)}</dd>`;
}
};
use({ extensions: [descriptionlist, description] });
const html = marked('A Description List with One Description:\n'
+ ': Topic 1 : Description 1\n'
+ ': **Topic 2** : *Description 2*');
expect(html).toBe('<p>A Description List with One Description:</p>\n'
+ '<dl>'
+ '\n<dt>Topic 1</dt><dd>Description 1</dd>'
+ '\n<dt><strong>Topic 2</strong></dt><dd><em>Description 2</em></dd>'
+ '\n</dl>');
});
it('should allow other options mixed into the extension', () => {
const extension = {
name: 'underline',
level: 'block',
start(src) { return src.indexOf(':'); },
tokenizer(src) {
const rule = /^:([^\n]*):(?:\n|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'underline',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[1].trim() // You can add additional properties to your tokens to pass along to the renderer
};
}
},
renderer(token) {
return `<u>${token.text}</u>\n`;
}
};
use({ sanitize: true, silent: true, extensions: [extension] });
const html = marked(':test:\ntest\n<div></div>');
expect(html).toBe('<u>test</u>\n<p>test</p>\n<p><div></div></p>\n');
});
it('should handle renderers that return false', () => {
const extension = {
name: 'test',
level: 'block',
tokenizer(src) {
const rule = /^:([^\n]*):(?:\n|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'test',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[1].trim() // You can add additional properties to your tokens to pass along to the renderer
};
}
},
renderer(token) {
if (token.text === 'test') {
return 'test';
}
return false;
}
};
const fallbackRenderer = {
name: 'test',
level: 'block',
renderer(token) {
if (token.text === 'Test') {
return 'fallback';
}
return false;
}
};
use({ extensions: [fallbackRenderer, extension] });
const html = marked(':Test:\n\n:test:\n\n:none:');
expect(html).toBe('fallbacktest');
});
it('should fall back when tokenizers return false', () => {
const extension = {
name: 'test',
level: 'block',
tokenizer(src) {
const rule = /^:([^\n]*):(?:\n|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'test',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[1].trim() // You can add additional properties to your tokens to pass along to the renderer
};
}
return false;
},
renderer(token) {
return token.text;
}
};
const extension2 = {
name: 'test',
level: 'block',
tokenizer(src) {
const rule = /^:([^\n]*):(?:\n|$)/;
const match = rule.exec(src);
if (match) {
if (match[1].match(/^[A-Z]/)) {
return {
type: 'test',
raw: match[0],
text: match[1].trim().toUpperCase()
};
}
}
return false;
}
};
use({ extensions: [extension, extension2] });
const html = marked(':Test:\n\n:test:');
expect(html).toBe('TESTtest');
});
it('should override original tokenizer/renderer with same name, but fall back if returns false', () => {
const extension = {
extensions: [{
name: 'heading',
level: 'block',
tokenizer(src) {
return false; // fall back to default `heading` tokenizer
},
renderer(token) {
return '<h' + token.depth + '>' + token.text + ' RENDERER EXTENSION</h' + token.depth + '>\n';
}
},
{
name: 'code',
level: 'block',
tokenizer(src) {
const rule = /^:([^\n]*):(?:\n|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'code',
raw: match[0],
text: match[1].trim() + ' TOKENIZER EXTENSION'
};
}
},
renderer(token) {
return false; // fall back to default `code` renderer
}
}]
};
use(extension);
const html = marked('# extension1\n:extension2:');
expect(html).toBe('<h1>extension1 RENDERER EXTENSION</h1>\n<pre><code>extension2 TOKENIZER EXTENSION\n</code></pre>\n');
});
it('should walk only specified child tokens', () => {
const walkableDescription = {
extensions: [{
name: 'walkableDescription',
level: 'inline',
start(src) { return src.indexOf(':'); },
tokenizer(src, tokens) {
const rule = /^:([^:\n]+):([^:\n]*)(?:\n|$)/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'walkableDescription',
raw: match[0],
dt: [],
dd: [],
tokens: []
};
this.lexer.inline(match[1].trim(), token.dt);
this.lexer.inline(match[2].trim(), token.dd);
this.lexer.inline('unwalked', token.tokens);
return token;
}
},
renderer(token) {
return `\n<dt>${this.parser.parseInline(token.dt)} - ${this.parser.parseInline(token.tokens)}</dt><dd>${this.parser.parseInline(token.dd)}</dd>`;
},
childTokens: ['dd', 'dt']
}],
walkTokens(token) {
if (token.type === 'text') {
token.text += ' walked';
}
}
};
use(walkableDescription);
const html = marked(': Topic 1 : Description 1\n'
+ ': **Topic 2** : *Description 2*');
expect(html).toBe('<p>\n<dt>Topic 1 walked - unwalked</dt><dd>Description 1 walked</dd>'
+ '\n<dt><strong>Topic 2 walked</strong> - unwalked</dt><dd><em>Description 2 walked</em></dd></p>\n');
});
describe('multiple extensions', () => {
function createExtension(name) {
return {
extensions: [{
name: `block-${name}`,
level: 'block',
start(src) { return src.indexOf('::'); },
tokenizer(src, tokens) {
if (src.startsWith(`::${name}\n`)) {
const text = `:${name}`;
const token = {
type: `block-${name}`,
raw: `::${name}\n`,
text,
tokens: []
};
this.lexer.inline(token.text, token.tokens);
return token;
}
},
renderer(token) {
return `<${token.type}>${this.parser.parseInline(token.tokens)}</${token.type}>\n`;
}
}, {
name: `inline-${name}`,
level: 'inline',
start(src) { return src.indexOf(':'); },
tokenizer(src, tokens) {
if (src.startsWith(`:${name}`)) {
return {
type: `inline-${name}`,
raw: `:${name}`,
text: `used ${name}`
};
}
},
renderer(token) {
return token.text;
}
}],
tokenizer: {
heading(src) {
if (src.startsWith(`# ${name}`)) {
const token = {
type: 'heading',
raw: `# ${name}`,
text: `used ${name}`,
depth: 1,
tokens: []
};
this.lexer.inline(token.text, token.tokens);
return token;
}
return false;
}
},
renderer: {
heading(text, depth, raw, slugger) {
if (text === name) {
return `<h${depth}>${text}</h${depth}>\n`;
}
return false;
}
},
walkTokens(token) {
if (token.text === `used ${name}`) {
token.text += ' walked';
}
},
headerIds: false
};
}
function createFalseExtension(name) {
return {
extensions: [{
name: `block-${name}`,
level: 'block',
start(src) { return src.indexOf('::'); },
tokenizer(src, tokens) {
return false;
},
renderer(token) {
return false;
}
}, {
name: `inline-${name}`,
level: 'inline',
start(src) { return src.indexOf(':'); },
tokenizer(src, tokens) {
return false;
},
renderer(token) {
return false;
}
}],
headerIds: false
};
}
function runTest() {
const html = marked(`
::extension1
::extension2
:extension1
:extension2
# extension1
# extension2
# no extension
`);
expect(`\n${html}\n`.replace(/\n+/g, '\n')).toBe(`
<block-extension1>used extension1 walked</block-extension1>
<block-extension2>used extension2 walked</block-extension2>
<p>used extension1 walked
used extension2 walked</p>
<h1>used extension1 walked</h1>
<h1>used extension2 walked</h1>
<h1>no extension</h1>
`);
}
it('should merge extensions when calling marked.use multiple times', () => {
use(createExtension('extension1'));
use(createExtension('extension2'));
runTest();
});
it('should merge extensions when calling marked.use with multiple extensions', () => {
use(
createExtension('extension1'),
createExtension('extension2')
);
runTest();
});
it('should fall back to any extensions with the same name if the first returns false', () => {
use(
createExtension('extension1'),
createExtension('extension2'),
createFalseExtension('extension1'),
createFalseExtension('extension2')
);
runTest();
});
});
it('should allow deleting/editing tokens', () => {
const styleTags = {
extensions: [{
name: 'inlineStyleTag',
level: 'inline',
start(src) {
const match = src.match(/ *{[^\{]/);
if (match) {
return match.index;
}
},
tokenizer(src, tokens) {
const rule = /^ *{([^\{\}\n]+)}$/;
const match = rule.exec(src);
if (match) {
return {
type: 'inlineStyleTag',
raw: match[0], // This is the text that you want your token to consume from the source
text: match[1]
};
}
}
},
{
name: 'styled',
renderer(token) {
token.type = token.originalType;
const text = this.parser.parse([token]);
const openingTag = /(<[^\s<>]+)([^\n<>]*>.*)/s.exec(text);
if (openingTag) {
return `${openingTag[1]} ${token.style}${openingTag[2]}`;
}
return text;
}
}],
walkTokens(token) {
if (token.tokens) {
const finalChildToken = token.tokens[token.tokens.length - 1];
if (finalChildToken && finalChildToken.type === 'inlineStyleTag') {
token.originalType = token.type;
token.type = 'styled';
token.style = `style="color:${finalChildToken.text};"`;
token.tokens.pop();
}
}
},
headerIds: false
};
use(styleTags);
const html = marked('This is a *paragraph* with blue text. {blue}\n'
+ '# This is a *header* with red text {red}');
expect(html).toBe('<p style="color:blue;">This is a <em>paragraph</em> with blue text.</p>\n'
+ '<h1 style="color:red;">This is a <em>header</em> with red text</h1>\n');
});
it('should use renderer', () => {
const extension = {
renderer: {
paragraph(text) {
return 'extension';
}
}
};
spyOn(extension.renderer, 'paragraph').and.callThrough();
use(extension);
const html = marked('text');
expect(extension.renderer.paragraph).toHaveBeenCalledWith('text');
expect(html).toBe('extension');
});
it('should use tokenizer', () => {
const extension = {
tokenizer: {
paragraph(text) {
const token = {
type: 'paragraph',
raw: text,
text: 'extension',
tokens: []
};
this.lexer.inline(token.text, token.tokens);
return token;
}
}
};
spyOn(extension.tokenizer, 'paragraph').and.callThrough();
use(extension);
const html = marked('text');
expect(extension.tokenizer.paragraph).toHaveBeenCalledWith('text');
expect(html).toBe('<p>extension</p>\n');
});
it('should use walkTokens', () => {
let walked = 0;
const extension = {
walkTokens(token) {
walked++;
}
};
use(extension);
marked('text');
expect(walked).toBe(2);
});
it('should use walkTokens in async', (done) => {
let walked = 0;
const extension = {
walkTokens(token) {
walked++;
}
};
use(extension);
marked('text', () => {
expect(walked).toBe(2);
done();
});
});
it('should use options from extension', () => {
const extension = {
headerIds: false
};
use(extension);
const html = marked('# heading');
expect(html).toBe('<h1>heading</h1>\n');
});
it('should call all walkTokens in reverse order', () => {
let walkedOnce = 0;
let walkedTwice = 0;
const extension1 = {
walkTokens(token) {
if (token.walkedOnce) {
walkedTwice++;
}
}
};
const extension2 = {
walkTokens(token) {
walkedOnce++;
token.walkedOnce = true;
}
};
use(extension1);
use(extension2);
marked('text');
expect(walkedOnce).toBe(2);
expect(walkedTwice).toBe(2);
});
it('should use last extension function and not override others', () => {
const extension1 = {
renderer: {
paragraph(text) {
return 'extension1 paragraph\n';
},
html(html) {
return 'extension1 html\n';
}
}
};
const extension2 = {
renderer: {
paragraph(text) {
return 'extension2 paragraph\n';
}
}
};
use(extension1);
use(extension2);
const html = marked(`
paragraph
<html />
# heading
`);
expect(html).toBe('extension2 paragraph\nextension1 html\n<h1 id="heading">heading</h1>\n');
});
it('should use previous extension when returning false', () => {
const extension1 = {
renderer: {
paragraph(text) {
if (text !== 'original') {
return 'extension1 paragraph\n';
}
return false;
}
}
};
const extension2 = {
renderer: {
paragraph(text) {
if (text !== 'extension1' && text !== 'original') {
return 'extension2 paragraph\n';
}
return false;
}
}
};
use(extension1);
use(extension2);
const html = marked(`
paragraph
extension1
original
`);
expect(html).toBe('extension2 paragraph\nextension1 paragraph\n<p>original</p>\n');
});
it('should get options with this.options', () => {
const extension = {
renderer: {
heading: () => {
return this && this.options ? 'arrow options\n' : 'arrow no options\n';
},
html: function() {
return this.options ? 'function options\n' : 'function no options\n';
},
paragraph() {
return this.options ? 'shorthand options\n' : 'shorthand no options\n';
}
}
};
use(extension);
const html = marked(`
# heading
<html />
paragraph
`);
expect(html).toBe('arrow no options\nfunction options\nshorthand options\n');
});
});
describe('async highlight', () => {
let highlight, markdown;
beforeEach(() => {
highlight = jasmine.createSpy('highlight', (text, lang, callback) => {
setImmediate(() => {
callback(null, `async ${text || ''}`);
});
});
markdown = `
\`\`\`lang1
text 1
\`\`\`
> \`\`\`lang2
> text 2
> \`\`\`
- \`\`\`lang3
text 3
\`\`\`
`;
});
it('should highlight codeblocks async', (done) => {
highlight.and.callThrough();
marked(markdown, { highlight }, (err, html) => {
if (err) {
fail(err);
}
expect(html).toBe(`<pre><code class="language-lang1">async text 1
</code></pre>
<blockquote>
<pre><code class="language-lang2">async text 2
</code></pre>
</blockquote>
<ul>
<li><pre><code class="language-lang3">async text 3
</code></pre>
</li>
</ul>
`);
done();
});
});
it('should call callback for each error in highlight', (done) => {
highlight.and.callFake((text, lang, callback) => {
callback(new Error('highlight error'));
});
let numErrors = 0;
marked(markdown, { highlight }, (err, html) => {
expect(err).toBeTruthy();
expect(html).toBeUndefined();
if (err) {
numErrors++;
}
if (numErrors === 3) {
done();
}
});
});
it('should highlight codeblocks when not async', (done) => {
highlight.and.callFake((text, lang, callback) => {
callback(null, `async ${text || ''}`);
});
marked(markdown, { highlight }, (err, html) => {
if (err) {
fail(err);
}
expect(html).toBe(`<pre><code class="language-lang1">async text 1
</code></pre>
<blockquote>
<pre><code class="language-lang2">async text 2
</code></pre>
</blockquote>
<ul>
<li><pre><code class="language-lang3">async text 3
</code></pre>
</li>
</ul>
`);
done();
});
});
});
describe('walkTokens', () => {
it('should walk over every token', () => {
const markdown = `
paragraph
---
# heading
\`\`\`
code
\`\`\`
| a | b |
|---|---|
| 1 | 2 |
| 3 | 4 |
> blockquote
- list
<div>html</div>
[link](https://example.com)
![image](https://example.com/image.jpg)
**strong**
*em*
\`codespan\`
~~del~~
br
br
`;
const tokens = lexer(markdown, { ...getDefaults(), breaks: true });
const tokensSeen = [];
_walkTokens(tokens, (token) => {
tokensSeen.push([token.type, (token.raw || '').replace(/\n/g, '')]);
});
expect(tokensSeen).toEqual([
['space', ''],
['paragraph', 'paragraph'],
['text', 'paragraph'],
['space', ''],