forked from Rosey/markdown-draft-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-to-draft.spec.js
More file actions
768 lines (634 loc) · 28.9 KB
/
markdown-to-draft.spec.js
File metadata and controls
768 lines (634 loc) · 28.9 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
import { markdownToDraft, draftToMarkdown } from '../src/index';
describe('markdownToDraft', function () {
it('renders empty text correctly', function () {
var markdown = '';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
});
it('renders text according to Remarkable options', function () {
var markdown = '(p)';
var conversionResult = markdownToDraft(markdown, {remarkableOptions: {typographer: true}});
expect(conversionResult.blocks[0].text).toEqual('§');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
});
it('renders text according to Remarkable options (with preset)', function () {
var markdown = '(p)';
var conversionResult = markdownToDraft(markdown, {remarkablePreset: 'full', remarkableOptions: {typographer: true}});
expect(conversionResult.blocks[0].text).toEqual('§');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
});
it('renders unstyled blank lines correctly', function () {
var markdown = 'a\nb\n\nc\n\n\nd';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('a\nb');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
expect(conversionResult.blocks[1].text).toEqual('c');
expect(conversionResult.blocks[1].type).toEqual('unstyled');
expect(conversionResult.blocks[2].text).toEqual('d');
expect(conversionResult.blocks[2].type).toEqual('unstyled');
conversionResult = markdownToDraft(markdown, {preserveNewlines: true});
expect(conversionResult.blocks[0].text).toEqual('a\nb');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
expect(conversionResult.blocks[1].text).toEqual('');
expect(conversionResult.blocks[1].type).toEqual('unstyled');
expect(conversionResult.blocks[2].text).toEqual('c');
expect(conversionResult.blocks[2].type).toEqual('unstyled');
expect(conversionResult.blocks[3].text).toEqual('');
expect(conversionResult.blocks[3].type).toEqual('unstyled');
expect(conversionResult.blocks[4].text).toEqual('');
expect(conversionResult.blocks[4].type).toEqual('unstyled');
expect(conversionResult.blocks[5].text).toEqual('d');
expect(conversionResult.blocks[5].type).toEqual('unstyled');
});
it('renders hardbreaks correctly', function () {
var markdown = 'First line \nSecond line';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('First line\nSecond line');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
});
describe('blockquotes', function () {
it('renders blockquotes correctly', function () {
var markdown = '> Test I am a blockquote';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('blockquote');
expect(conversionResult.blocks[0].text).toEqual('Test I am a blockquote');
});
it('can handle an empty blockquote', function () {
var markdown = '>';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('blockquote');
expect(conversionResult.blocks[0].text).toEqual('');
});
it('can handle a blockquote with non-blockquote text around it', function () {
var markdown = 'Hey I am not blockquote \n\n I am also not a blockquote \n\n > Test I am a blockquote\n\n more not blockquote';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[2].type).toEqual('blockquote');
expect(conversionResult.blocks[2].text).toEqual('Test I am a blockquote');
});
it ('can handle empty blockquote between blockquote with content', function () {
var markdown = '> Testing\n> \n> \n> Hello';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('blockquote');
expect(conversionResult.blocks[0].text).toEqual('Testing');
expect(conversionResult.blocks[1].type).toEqual('blockquote');
expect(conversionResult.blocks[1].text).toEqual('Hello');
});
});
describe('headings', function () {
it ('renders h1 correctly', function () {
var markdown = '# Test I am a heading';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('header-one');
expect(conversionResult.blocks[0].text).toEqual('Test I am a heading');
});
it ('renders h2 correctly', function () {
var markdown = '## Test I am a heading';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('header-two');
expect(conversionResult.blocks[0].text).toEqual('Test I am a heading');
});
it ('renders h3 correctly', function () {
var markdown = '### Test I am a heading';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('header-three');
expect(conversionResult.blocks[0].text).toEqual('Test I am a heading');
});
it ('renders h4 correctly', function () {
var markdown = '#### Test I am a heading';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('header-four');
expect(conversionResult.blocks[0].text).toEqual('Test I am a heading');
});
it ('renders h5 correctly', function () {
var markdown = '##### Test I am a heading';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('header-five');
expect(conversionResult.blocks[0].text).toEqual('Test I am a heading');
});
it ('renders h6 correctly', function () {
var markdown = '###### Test I am a heading';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('header-six');
expect(conversionResult.blocks[0].text).toEqual('Test I am a heading');
});
it('can handle an empty heading', function () {
var markdown = '#';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('header-one');
expect(conversionResult.blocks[0].text).toEqual('');
});
it('can handle heading with non-heading text around it', function () {
var markdown = 'Test I am not a heading \n\n # I am a heading \n\n I am more no heading';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[1].type).toEqual('header-one');
expect(conversionResult.blocks[1].text).toEqual('I am a heading');
});
});
describe('lists', function () {
it('can handle an unordered list item', function () {
var markdown = '- Hi I am an unordered List Item';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('unordered-list-item');
expect(conversionResult.blocks[0].text).toEqual('Hi I am an unordered List Item');
});
it('can handle an ordered list item', function () {
var markdown = '1. Hi I am a list item';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[0].text).toEqual('Hi I am a list item');
});
it('can handle an empty unordered list item', function () {
var markdown = '-';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('unordered-list-item');
expect(conversionResult.blocks[0].text).toEqual('');
});
it('can handle an ordered empty list item', function () {
var markdown = '1.';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[0].text).toEqual('');
});
it('can handle nested unordered lists', function () {
var markdown = '- item\n - item';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('unordered-list-item');
expect(conversionResult.blocks[0].depth).toEqual(0);
expect(conversionResult.blocks[1].type).toEqual('unordered-list-item');
expect(conversionResult.blocks[1].depth).toEqual(1);
});
it('can handle nested ordered lists', function () {
var markdown = '1. item\n 1. item';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[0].depth).toEqual(0);
expect(conversionResult.blocks[1].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[1].depth).toEqual(1);
});
it ('can handle complex nested ordered lists', function () {
var markdown = '1. Test Item one unnested\n 1. Test Item one nested\n 2. Test Item two nested\n 3. Test item three nested\n2. Test item two unnested\n3. Test item three unnested\n4. Test Item Four unnested\n 1. Test item one nested under test item four\n 1. Test item one double nested\n 2. Test item two double nested';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[0].depth).toEqual(0);
expect(conversionResult.blocks[1].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[1].depth).toEqual(1);
expect(conversionResult.blocks[2].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[2].depth).toEqual(1);
expect(conversionResult.blocks[3].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[3].depth).toEqual(1);
expect(conversionResult.blocks[4].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[4].depth).toEqual(0);
expect(conversionResult.blocks[5].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[5].depth).toEqual(0);
expect(conversionResult.blocks[6].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[6].depth).toEqual(0);
expect(conversionResult.blocks[7].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[7].depth).toEqual(1);
expect(conversionResult.blocks[8].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[8].depth).toEqual(2);
expect(conversionResult.blocks[9].type).toEqual('ordered-list-item');
expect(conversionResult.blocks[9].depth).toEqual(2);
});
});
describe ('codeblocks', function () {
it ('renders single-line codeblock correctly', function () {
var markdown = '```\nsingle line codeblock\n```';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('single line codeblock');
expect(conversionResult.blocks[0].type).toEqual('code-block');
});
it ('renders codeblock with syntax correctly', function () {
var markdown = '```javascript\nsingle line codeblock\n```';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('single line codeblock');
expect(conversionResult.blocks[0].type).toEqual('code-block');
expect(conversionResult.blocks[0].data.language).toEqual('javascript');
});
it ('renders single-line codeblock with a single trailing newline correctly', function () {
var markdown = '```\nsingle line codeblock with trailing newline\n\n```';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('single line codeblock with trailing newline\n');
expect(conversionResult.blocks[0].type).toEqual('code-block');
});
it ('renders single-line codeblock wrapping newlines correctly', function () {
var markdown = '```\n\nsingle line codeblock with wrapping newlines\n\n```';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('\nsingle line codeblock with wrapping newlines\n');
expect(conversionResult.blocks[0].type).toEqual('code-block');
});
it ('renders multi-line codeblock correctly', function () {
var markdown = '```\nTest \n\n here is more \n ok\n```';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('Test \n\n here is more \n ok');
expect(conversionResult.blocks[0].type).toEqual('code-block');
});
});
it('renders links correctly', function () {
var markdown = 'This is a test of [a link](https://google.com)\n\n\n\nAnd [perhaps](https://facebook.github.io/draft-js/) we should test once more.';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('This is a test of a link');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
expect(conversionResult.blocks[0].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[0].entityRanges[0].offset).toEqual(18);
expect(conversionResult.blocks[0].entityRanges[0].length).toEqual(6);
var blockOneKey = conversionResult.blocks[0].entityRanges[0].key;
expect(conversionResult.entityMap[blockOneKey].type).toEqual('LINK');
expect(conversionResult.entityMap[blockOneKey].data.url).toEqual('https://google.com');
expect(conversionResult.entityMap[blockOneKey].data.href).toEqual('https://google.com');
expect(conversionResult.blocks[1].text).toEqual('And perhaps we should test once more.');
expect(conversionResult.blocks[1].type).toEqual('unstyled');
expect(conversionResult.blocks[1].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[1].entityRanges[0].offset).toEqual(4);
expect(conversionResult.blocks[1].entityRanges[0].length).toEqual(7);
var blockTwoKey = conversionResult.blocks[1].entityRanges[0].key;
expect(conversionResult.entityMap[blockTwoKey].type).toEqual('LINK');
expect(conversionResult.entityMap[blockTwoKey].data.url).toEqual('https://facebook.github.io/draft-js/');
expect(conversionResult.entityMap[blockTwoKey].data.href).toEqual('https://facebook.github.io/draft-js/');
});
it('renders "the kitchen sink" correctly', function () {
var markdown = '# Hello!\n\nMy name is **Rose** :) \nToday, I\'m here to talk to you about how great markdown is!\n\n## First, here\'s a few bullet points:\n\n- One\n- Two\n- Three\n\n```\nA codeblock\n```\n\nAnd then... `some monospace text`?\nOr... _italics?_';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('Hello!');
expect(conversionResult.blocks[0].type).toEqual('header-one');
expect(conversionResult.blocks[0].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[0].entityRanges).toEqual([]);
expect(conversionResult.blocks[1].text).toEqual('My name is Rose :)\nToday, I\'m here to talk to you about how great markdown is!');
expect(conversionResult.blocks[1].type).toEqual('unstyled');
expect(conversionResult.blocks[1].inlineStyleRanges[0].offset).toEqual(11);
expect(conversionResult.blocks[1].inlineStyleRanges[0].length).toEqual(4);
expect(conversionResult.blocks[1].inlineStyleRanges[0].style).toEqual('BOLD');
expect(conversionResult.blocks[2].text).toEqual('First, here\'s a few bullet points:');
expect(conversionResult.blocks[2].type).toEqual('header-two');
expect(conversionResult.blocks[2].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[2].entityRanges).toEqual([]);
expect(conversionResult.blocks[3].text).toEqual('One');
expect(conversionResult.blocks[3].type).toEqual('unordered-list-item');
expect(conversionResult.blocks[3].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[3].entityRanges).toEqual([]);
expect(conversionResult.blocks[4].text).toEqual('Two');
expect(conversionResult.blocks[4].type).toEqual('unordered-list-item');
expect(conversionResult.blocks[4].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[4].entityRanges).toEqual([]);
expect(conversionResult.blocks[5].text).toEqual('Three');
expect(conversionResult.blocks[5].type).toEqual('unordered-list-item');
expect(conversionResult.blocks[5].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[5].entityRanges).toEqual([]);
expect(conversionResult.blocks[6].text).toEqual('A codeblock');
expect(conversionResult.blocks[6].type).toEqual('code-block');
expect(conversionResult.blocks[6].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[6].entityRanges).toEqual([]);
expect(conversionResult.blocks[7].text).toEqual('And then... some monospace text?\nOr... italics?');
expect(conversionResult.blocks[7].type).toEqual('unstyled');
expect(conversionResult.blocks[7].inlineStyleRanges[0].offset).toEqual(12);
expect(conversionResult.blocks[7].inlineStyleRanges[0].length).toEqual(19);
expect(conversionResult.blocks[7].inlineStyleRanges[0].style).toEqual('CODE');
expect(conversionResult.blocks[7].inlineStyleRanges[1].offset).toEqual(39);
expect(conversionResult.blocks[7].inlineStyleRanges[1].length).toEqual(8);
expect(conversionResult.blocks[7].inlineStyleRanges[1].style).toEqual('ITALIC');
expect(conversionResult.blocks[7].entityRanges).toEqual([]);
});
it('can handle block entity data', function () {
const MentionRegexp = /^@\[([^\]]*)\]\s*\(([^)]+)\)/;
function mentionWrapper(remarkable) {
remarkable.inline.ruler.push('mention', function mention(state, silent) {
// it is surely not our rule, so we could stop early
if (!state.src || !state.pos) {
return false;
}
if (state.src[state.pos] !== '@') {
return false;
}
var match = MentionRegexp.exec(state.src.slice(state.pos));
if (!match) {
return false;
}
// in silent mode it shouldn't output any tokens or modify pending
if (!silent) {
state.push({
type: 'mention_open',
name: match[1],
id: match[2],
level: state.level
});
state.push({
type: 'text',
content: '@' + match[1],
level: state.level + 1
});
state.push({
type: 'mention_close',
level: state.level
});
}
// every rule should set state.pos to a position after token"s contents
state.pos += match[0].length;
return true;
});
}
var markdown = 'Test @[Rose](1)';
var conversionResult = markdownToDraft(markdown, {
remarkablePlugins: [mentionWrapper],
blockEntities: {
mention_open: function (item) {
return {
type: 'MENTION',
mutability: 'IMMUTABLE',
data: {
id: item.id,
name: item.name
}
};
}
}
});
expect(conversionResult.blocks[0].text).toEqual('Test @Rose');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
expect(conversionResult.blocks[0].inlineStyleRanges).toEqual([]);
expect(conversionResult.blocks[0].entityRanges[0].offset).toEqual(5);
expect(conversionResult.blocks[0].entityRanges[0].length).toEqual(5);
var blockOneKey = conversionResult.blocks[0].entityRanges[0].key;
expect(conversionResult.entityMap[blockOneKey].type).toEqual('MENTION');
expect(conversionResult.entityMap[blockOneKey].data.id).toEqual('1');
expect(conversionResult.entityMap[blockOneKey].data.name).toEqual('Rose');
});
it('can handle block data', function () {
var markdown = '```js\ntest()\n```';
var conversionResult = markdownToDraft(markdown, {
blockTypes: {
fence: function (item) {
return {
type: 'code-block',
data: {
lang: item.params
}
}
}
}
});
expect(conversionResult.blocks[0].type).toEqual('code-block');
expect(conversionResult.blocks[0].data.lang).toEqual('js');
});
it('can handle custom standalone block data', function () {
var delimiter = '---';
var blockRule = function (state, startLine, endLine, silent) {
var marker,
len,
nextLine,
mem,
content,
haveEndMarker = false,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine]
// Check if the current line is the first line
if (startLine !== 0) { return false }
// Check if the line contains at least 3 characters
if (pos + 3 > max) { return false }
// Check if the first character is a '-'
marker = state.src.charCodeAt(pos)
if (marker !== 0x2D) { return false }
// Check marker length
mem = pos
pos = state.skipChars(pos, marker)
len = pos - mem
if (len < 3) { return false }
// Since start is found, we can report success here in validation mode
if (silent) { return true }
nextLine = startLine
for (;;) {
nextLine++
// Break if the current line is the last line
if (nextLine >= endLine) {
break
}
pos = mem = state.bMarks[nextLine] + state.tShift[nextLine]
max = state.eMarks[nextLine]
// Skip to next line if the current line does not start with the
// marker
if (state.src.charCodeAt(pos) !== marker) { continue }
// Skip to next line if the closing dash is indented with more than
// 4 spaces
if (state.tShift[nextLine] - state.blkIndent >= 4) { continue }
pos = state.skipChars(pos, marker)
// Skip to next line if the number of closing dashed is not the same
// as the opening ones.
if (pos - mem < len) { continue }
// Skip to next line if there are more characters after possible
// closing dashes
pos = state.skipSpaces(pos)
if (pos < max) { continue }
// Mark block end
haveEndMarker = true
break
}
state.line = nextLine + (haveEndMarker ? 1 : 0)
content = state.getLines(startLine + 1, nextLine, state.blkIndent, false).trim()
state.tokens.push({
type: 'yaml_frontmatter',
content: content,
level: state.level,
lines: [ startLine, state.line ]
})
return true
}
var frontmatterYamlWrapper = function (remarkable) {
remarkable.block.ruler.before('hr', 'yaml_frontmatter', blockRule)
}
var markdown = '---\nsomeMetadata: content\n---';
var conversionResult = markdownToDraft(markdown, {
remarkablePlugins: [frontmatterYamlWrapper],
blockTypes: {
yaml_frontmatter: function (item) {
return {
type: 'atomic',
content: item.content
}
}
},
remarkableStandaloneBlocks: ['yaml_frontmatter']
});
expect(conversionResult.blocks[0].type).toEqual('atomic');
expect(conversionResult.blocks[0].content).toEqual('someMetadata: content');
});
it('can handle simple nested styles', function () {
var markdown = '__*hello* world__';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].inlineStyleRanges[0].length).toBe(11);
expect(conversionResult.blocks[0].inlineStyleRanges[1].length).toBe(5);
});
it('can handle more complex nested styles', function () {
var markdown = '**bold _bolditalic_** _italic_ regular';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult).toEqual({
'entityMap': {},
'blocks': [
{
'depth': 0,
'type': 'unstyled',
'text': 'bold bolditalic italic regular',
'entityRanges': [],
'inlineStyleRanges': [
{
'offset': 0,
'length': 15,
'style': 'BOLD'
},
{
'offset': 5,
'length': 10,
'style': 'ITALIC'
},
{
'offset': 16,
'length': 6,
'style': 'ITALIC'
}
]
}
]
});
});
it ('ignores tables by default', function () {
var markdown = 'this is the first line.\n' +
'\n' +
'this is the second line.\n' +
'\n' +
'| foo | bar |\n' +
'| --- | --- |\n' +
'| baz | bim |\n' +
'\n' +
'This is another line under the table.';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult.blocks[0].text).toEqual('this is the first line.');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
expect(conversionResult.blocks[1].text).toEqual('this is the second line.');
expect(conversionResult.blocks[1].type).toEqual('unstyled');
expect(conversionResult.blocks[2].text).toEqual('| foo | bar |\n| --- | --- |\n| baz | bim |');
expect(conversionResult.blocks[2].type).toEqual('unstyled');
expect(conversionResult.blocks[3].text).toEqual('This is another line under the table.');
expect(conversionResult.blocks[3].type).toEqual('unstyled');
});
it('can handle emoji', function () {
// Note `'👍'.length === 2`
var markdown = 'Testing 👍 _italic_ words words words **bold** words words words';
var conversionResult = markdownToDraft(markdown);
expect(conversionResult).toEqual({
'entityMap': {},
'blocks': [
{
'depth': 0,
'type': 'unstyled',
'text': 'Testing 👍 italic words words words bold words words words',
'entityRanges': [],
'inlineStyleRanges': [
{
'offset': 10,
'length': 6,
'style': 'ITALIC'
},
{
'offset': 35,
'length': 4,
'style': 'BOLD'
}
]
}
]
});
});
it('can handle hr', function () {
var markdown = 'this is the first line.\n' +
'\n' +
'---\n' +
'\n' +
'this is the second line.';
var conversionResult = markdownToDraft(markdown, {
blockTypes: {
hr: function (_item) {
return {
type: 'HR',
text: ''
};
}
}
});
expect(conversionResult.blocks[0].text).toEqual('this is the first line.');
expect(conversionResult.blocks[0].type).toEqual('unstyled');
expect(conversionResult.blocks[1].text).toEqual('');
expect(conversionResult.blocks[1].type).toEqual('HR');
expect(conversionResult.blocks[2].text).toEqual('this is the second line.');
expect(conversionResult.blocks[2].type).toEqual('unstyled');
});
it('can disable rules', () => {
var markdown = 'This is a test of [a link](https://google.com)';
var remarkableOptions = {
remarkableOptions: {
disable: {
inline: 'links'
}
}
}
var conversionResult = markdownToDraft(markdown, remarkableOptions);
expect(conversionResult.blocks[0].text).toEqual(
'This is a test of [a link](https://google.com)'
);
});
it('can enable rules', () => {
var markdown = 'H~2~O is a liquid. 2^10^ is 1024.';
var remarkableOptions = {
remarkableOptions: {
enable: {
inline: ['sub', 'sup']
}
}
}
var conversionResult = markdownToDraft(markdown, remarkableOptions);
expect(conversionResult.blocks[0].text).toEqual(
'HO is a liquid. 2 is 1024.'
);
});
it ('can enable tables with a string', function () {
var markdown = 'this is the first line.\n' +
'\n' +
'this is the second line.\n' +
'\n' +
'| foo | bar |\n' +
'| --- | --- |\n' +
'| baz | bim |\n' +
'\n' +
'This is another line under the table.';
var remarkableOptions = {
remarkableOptions: {
enable: {
block: 'table'
}
}
}
var conversionResult = markdownToDraft(markdown, remarkableOptions);
expect(conversionResult.blocks[0].text).toEqual('this is the first line.');
expect(conversionResult.blocks[1].text).toEqual('bim');
expect(conversionResult.blocks[2].text).toEqual('This is another line under the table.');
});
it ('can enable tables with an array', function () {
var markdown = 'this is the first line.\n' +
'\n' +
'this is the second line.\n' +
'\n' +
'| foo | bar |\n' +
'| --- | --- |\n' +
'| baz | bim |\n' +
'\n' +
'This is another line under the table.';
var remarkableOptions = {
remarkableOptions: {
enable: {
block: ['table']
}
}
}
var conversionResult = markdownToDraft(markdown, remarkableOptions);
expect(conversionResult.blocks[0].text).toEqual('this is the first line.');
expect(conversionResult.blocks[1].text).toEqual('bim');
expect(conversionResult.blocks[2].text).toEqual('This is another line under the table.');
});
});