@khanacademy/pure-markdown 0.2.13 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,553 +0,0 @@
1
- import {describe, it} from "@jest/globals";
2
-
3
- import {parse} from "../index";
4
-
5
- describe("pure markdown", () => {
6
- describe("parser", () => {
7
- it.each([
8
- {
9
- content: "math $y = x + 1$",
10
- expected: [
11
- {
12
- type: "paragraph",
13
- content: [
14
- {type: "text", content: "math "},
15
- {type: "math", content: "y = x + 1"},
16
- ],
17
- },
18
- ],
19
- },
20
- {
21
- content: "hi $y = x + 1$ there",
22
- expected: [
23
- {
24
- type: "paragraph",
25
- content: [
26
- {type: "text", content: "hi "},
27
- {type: "math", content: "y = x + 1"},
28
- {type: "text", content: " there"},
29
- ],
30
- },
31
- ],
32
- },
33
- ])("should parse math", ({content, expected}) => {
34
- // Arrange, Act
35
- const parsed = parse(content);
36
-
37
- // Assert
38
- expect(parsed).toEqual(expected);
39
- });
40
-
41
- it.each([
42
- {
43
- content: "math $y = \\text{$x + 1$}$",
44
- expected: [
45
- {
46
- type: "paragraph",
47
- content: [
48
- {type: "text", content: "math "},
49
- {type: "math", content: "y = \\text{$x + 1$}"},
50
- ],
51
- },
52
- ],
53
- },
54
- {
55
- content:
56
- "math $ x^2 \\text{blah $math \\text{some $more math$} $ } $",
57
- expected: [
58
- {
59
- type: "paragraph",
60
- content: [
61
- {type: "text", content: "math "},
62
- {
63
- type: "math",
64
- content:
65
- " x^2 \\text{blah $math " +
66
- "\\text{some $more math$} $ } ",
67
- },
68
- ],
69
- },
70
- ],
71
- },
72
- ])("should parse nested math", ({content, expected}) => {
73
- // Arrange, Act
74
- const parsed = parse(content);
75
-
76
- // Assert
77
- expect(parsed).toEqual(expected);
78
- });
79
-
80
- it.each([
81
- {
82
- content: "math $\\\\$",
83
- expected: [
84
- {
85
- type: "paragraph",
86
- content: [
87
- {type: "text", content: "math "},
88
- {type: "math", content: "\\\\"},
89
- ],
90
- },
91
- ],
92
- },
93
- {
94
- content: "math $\\$$",
95
- expected: [
96
- {
97
- type: "paragraph",
98
- content: [
99
- {type: "text", content: "math "},
100
- {type: "math", content: "\\$"},
101
- ],
102
- },
103
- ],
104
- },
105
- {
106
- content: "${$",
107
- expected: [
108
- {
109
- type: "paragraph",
110
- content: [
111
- {type: "unescapedDollar"},
112
- {type: "text", content: "{"},
113
- {type: "unescapedDollar"},
114
- ],
115
- },
116
- ],
117
- },
118
- {
119
- content: "math $\\{$",
120
- expected: [
121
- {
122
- type: "paragraph",
123
- content: [
124
- {type: "text", content: "math "},
125
- {type: "math", content: "\\{"},
126
- ],
127
- },
128
- ],
129
- },
130
- {
131
- content: "hello $ escaped dollar \\$ $ not math",
132
- expected: [
133
- {
134
- type: "paragraph",
135
- content: [
136
- {type: "text", content: "hello "},
137
- {type: "math", content: " escaped dollar \\$ "},
138
- {type: "text", content: " not math"},
139
- ],
140
- },
141
- ],
142
- },
143
- {
144
- content: "$math$ not math $ oops extra dollar",
145
- expected: [
146
- {
147
- type: "paragraph",
148
- content: [
149
- {content: "math", type: "math"},
150
- {content: " not math ", type: "text"},
151
- {type: "unescapedDollar"},
152
- {content: " oops extra dollar", type: "text"},
153
- ],
154
- },
155
- ],
156
- },
157
- ])("should allow escaping in math", ({content, expected}) => {
158
- // Arrange, Act
159
- const parsed = parse(content);
160
-
161
- // Assert
162
- expect(parsed).toEqual(expected);
163
- });
164
-
165
- it.each([
166
- {
167
- content: "$x + y = 7$",
168
- expected: [
169
- {
170
- type: "blockMath",
171
- content: "x + y = 7",
172
- },
173
- ],
174
- },
175
- {
176
- content: "$x + y = 7$\nnot math",
177
- expected: [
178
- {
179
- type: "paragraph",
180
- content: [
181
- {type: "math", content: "x + y = 7"},
182
- {type: "text", content: "\nnot math"},
183
- ],
184
- },
185
- ],
186
- },
187
- {
188
- content: " $x + y = 7$ \n\n \n$3 + 5 = 7$",
189
- expected: [
190
- {
191
- type: "blockMath",
192
- content: "x + y = 7",
193
- },
194
- {
195
- type: "blockMath",
196
- content: "3 + 5 = 7",
197
- },
198
- ],
199
- },
200
- {
201
- content: " $x + y = 7$",
202
- expected: [
203
- {
204
- type: "codeBlock",
205
- content: "$x + y = 7$",
206
- lang: undefined,
207
- },
208
- ],
209
- },
210
- {
211
- content: "> $x + y = 7$",
212
- expected: [
213
- {
214
- type: "blockQuote",
215
- content: [
216
- {
217
- type: "blockMath",
218
- content: "x + y = 7",
219
- },
220
- ],
221
- },
222
- ],
223
- },
224
- ])("should parse block math", ({content, expected}) => {
225
- // Arrange, Act
226
- const parsed = parse(content);
227
-
228
- // Assert
229
- expect(parsed).toEqual(expected);
230
- });
231
-
232
- it.each([
233
- {
234
- content: "hello $ single dollar paragraph\n\n not math $",
235
- expected: [
236
- {
237
- type: "paragraph",
238
- content: [
239
- {content: "hello ", type: "text"},
240
- {type: "unescapedDollar"},
241
- {content: " single dollar paragraph", type: "text"},
242
- ],
243
- },
244
- {
245
- type: "paragraph",
246
- content: [
247
- {content: " not math ", type: "text"},
248
- {type: "unescapedDollar"},
249
- ],
250
- },
251
- ],
252
- },
253
- {
254
- content: "hello $ bad { math $",
255
- expected: [
256
- {
257
- type: "paragraph",
258
- content: [
259
- {content: "hello ", type: "text"},
260
- {type: "unescapedDollar"},
261
- {content: " bad ", type: "text"},
262
- {content: "{ math ", type: "text"},
263
- {type: "unescapedDollar"},
264
- ],
265
- },
266
- ],
267
- },
268
- ])("should break on paragraphs", ({content, expected}) => {
269
- // Arrange, Act
270
- const parsed = parse(content);
271
-
272
- // Assert
273
- expect(parsed).toEqual(expected);
274
- });
275
-
276
- it.each([
277
- {
278
- content: "[[☃ test 1]]",
279
- expected: [
280
- {
281
- type: "paragraph",
282
- content: [
283
- {
284
- type: "widget",
285
- widgetType: "test",
286
- id: "test 1",
287
- },
288
- ],
289
- },
290
- ],
291
- },
292
- {
293
- content: "[[☃ test 1]]+[[☃ input-number 2]]",
294
- expected: [
295
- {
296
- type: "paragraph",
297
- content: [
298
- {
299
- type: "widget",
300
- widgetType: "test",
301
- id: "test 1",
302
- },
303
- {
304
- type: "text",
305
- content: "+",
306
- },
307
- {
308
- type: "widget",
309
- widgetType: "input-number",
310
- id: "input-number 2",
311
- },
312
- ],
313
- },
314
- ],
315
- },
316
- {
317
- content: "*[[☃ test 2]]* [[☃ input-number 1]]",
318
- expected: [
319
- {
320
- type: "paragraph",
321
- content: [
322
- {
323
- type: "em",
324
- content: [
325
- {
326
- type: "widget",
327
- widgetType: "test",
328
- id: "test 2",
329
- },
330
- ],
331
- },
332
- {
333
- type: "text",
334
- content: " ",
335
- },
336
- {
337
- type: "widget",
338
- widgetType: "input-number",
339
- id: "input-number 1",
340
- },
341
- ],
342
- },
343
- ],
344
- },
345
- ])("should parse widget types and ids", ({content, expected}) => {
346
- // Arrange, Act
347
- const parsed = parse(content);
348
-
349
- // Assert
350
- expect(parsed).toEqual(expected);
351
- });
352
-
353
- it("should allow escaping widget identifiers", () => {
354
- // Arrange, Act
355
- const parsed = parse("\\[[☃ test 1]]");
356
-
357
- // Assert
358
- expect(parsed).toEqual([
359
- {
360
- type: "paragraph",
361
- content: [
362
- {content: "[", type: "text"},
363
- {content: "[☃ test 1", type: "text"},
364
- {content: "]", type: "text"},
365
- {content: "]", type: "text"},
366
- ],
367
- },
368
- ]);
369
- });
370
-
371
- it.each([
372
- {
373
- content: "[[☃ test 1]][[☃ test 2]]",
374
- expected: [
375
- {
376
- type: "paragraph",
377
- content: [
378
- {type: "widget", widgetType: "test", id: "test 1"},
379
- {type: "widget", widgetType: "test", id: "test 2"},
380
- ],
381
- },
382
- ],
383
- },
384
- {
385
- content: "[[☃ test 1]] [[☃ test 2]]",
386
- expected: [
387
- {
388
- type: "paragraph",
389
- content: [
390
- {type: "widget", widgetType: "test", id: "test 1"},
391
- {type: "text", content: " "},
392
- {type: "widget", widgetType: "test", id: "test 2"},
393
- ],
394
- },
395
- ],
396
- },
397
- ])(
398
- "should parse widgets next to each other as widgets",
399
- ({content, expected}) => {
400
- // Arrange, Act
401
- const parsed = parse(content);
402
-
403
- // Assert
404
- expect(parsed).toEqual(expected);
405
- },
406
- );
407
-
408
- it("should parse multiple columns", () => {
409
- // Arrange, Act
410
- const parsed = parse("hi\n\n" + "=====\n\n" + "there\n\n");
411
-
412
- // Assert
413
- expect(parsed).toEqual([
414
- {
415
- type: "columns",
416
- col1: [
417
- {
418
- type: "paragraph",
419
- content: [
420
- {
421
- type: "text",
422
- content: "hi",
423
- },
424
- ],
425
- },
426
- ],
427
- col2: [
428
- {
429
- type: "paragraph",
430
- content: [
431
- {
432
- type: "text",
433
- content: "there",
434
- },
435
- ],
436
- },
437
- ],
438
- },
439
- ]);
440
- });
441
-
442
- it.each([
443
- {
444
- content: "1. test\n\n" + "2. boo\n\n",
445
- expected: [
446
- {
447
- type: "paragraph",
448
- content: [
449
- {
450
- type: "text",
451
- content: "1",
452
- },
453
- {
454
- type: "text",
455
- content: ". test",
456
- },
457
- ],
458
- },
459
- {
460
- type: "paragraph",
461
- content: [
462
- {
463
- type: "text",
464
- content: "2",
465
- },
466
- {
467
- type: "text",
468
- content: ". boo",
469
- },
470
- ],
471
- },
472
- ],
473
- },
474
- {
475
- content: "* test\n\n" + "* boo\n\n",
476
- expected: [
477
- {
478
- type: "paragraph",
479
- content: [
480
- {
481
- type: "text",
482
- content: "* test",
483
- },
484
- ],
485
- },
486
- {
487
- type: "paragraph",
488
- content: [
489
- {
490
- type: "text",
491
- content: "* boo",
492
- },
493
- ],
494
- },
495
- ],
496
- },
497
- ])("should ignore lists in jipt mode", ({content, expected}) => {
498
- // Arrange, Act
499
- const parsed = parse(content, {isJipt: true});
500
-
501
- // Assert
502
- expect(parsed).toEqual(expected);
503
- });
504
-
505
- it.each([
506
- {
507
- content: "$",
508
- expected: [
509
- {
510
- type: "paragraph",
511
- content: [{type: "unescapedDollar"}],
512
- },
513
- ],
514
- },
515
- {
516
- content: "hello $ single dollar",
517
- expected: [
518
- {
519
- type: "paragraph",
520
- content: [
521
- {content: "hello ", type: "text"},
522
- {type: "unescapedDollar"},
523
- {content: " single dollar", type: "text"},
524
- ],
525
- },
526
- ],
527
- },
528
- ])("should detect unescaped dollars", ({content, expected}) => {
529
- // Arrange, Act
530
- const parsed = parse(content);
531
-
532
- // Assert
533
- expect(parsed).toEqual(expected);
534
- });
535
-
536
- it("should parse titled table with unescaped dollars", () => {
537
- // Arrange
538
- const content =
539
- "|| **Table title** ||\n" +
540
- "header 1 | header 2 | header 3\n" +
541
- "- | - | -\n" +
542
- "data 1 | data 2 | data 3\n" +
543
- "data 4 | $data 5 | data 6\n" +
544
- "data 7 | data 8 | data 9";
545
-
546
- // Act
547
- const parsed = parse(content);
548
-
549
- // Assert
550
- expect(parsed).toMatchSnapshot();
551
- });
552
- });
553
- });