@markdoc/markdoc 0.1.0

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.
Files changed (112) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/dist/index.d.ts +120 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +7961 -0
  6. package/dist/index.js.map +7 -0
  7. package/dist/index.mjs +7959 -0
  8. package/dist/index.mjs.map +7 -0
  9. package/dist/react.js +40 -0
  10. package/dist/react.js.map +7 -0
  11. package/dist/src/ast/base.d.ts +6 -0
  12. package/dist/src/ast/base.d.ts.map +1 -0
  13. package/dist/src/ast/function.d.ts +9 -0
  14. package/dist/src/ast/function.d.ts.map +1 -0
  15. package/dist/src/ast/index.d.ts +26 -0
  16. package/dist/src/ast/index.d.ts.map +1 -0
  17. package/dist/src/ast/node.d.ts +21 -0
  18. package/dist/src/ast/node.d.ts.map +1 -0
  19. package/dist/src/ast/tag.d.ts +9 -0
  20. package/dist/src/ast/tag.d.ts.map +1 -0
  21. package/dist/src/ast/variable.d.ts +8 -0
  22. package/dist/src/ast/variable.d.ts.map +1 -0
  23. package/dist/src/functions/index.d.ts +11 -0
  24. package/dist/src/functions/index.d.ts.map +1 -0
  25. package/dist/src/parser.d.ts +4 -0
  26. package/dist/src/parser.d.ts.map +1 -0
  27. package/dist/src/renderers/html.d.ts +3 -0
  28. package/dist/src/renderers/html.d.ts.map +1 -0
  29. package/dist/src/renderers/index.d.ts +10 -0
  30. package/dist/src/renderers/index.d.ts.map +1 -0
  31. package/dist/src/renderers/react/react.d.ts +11 -0
  32. package/dist/src/renderers/react/react.d.ts.map +1 -0
  33. package/dist/src/renderers/react/shared.d.ts +5 -0
  34. package/dist/src/renderers/react/shared.d.ts.map +1 -0
  35. package/dist/src/renderers/react/static.d.ts +3 -0
  36. package/dist/src/renderers/react/static.d.ts.map +1 -0
  37. package/dist/src/schema-types/class.d.ts +6 -0
  38. package/dist/src/schema-types/class.d.ts.map +1 -0
  39. package/dist/src/schema.d.ts +27 -0
  40. package/dist/src/schema.d.ts.map +1 -0
  41. package/dist/src/tags/conditional.d.ts +5 -0
  42. package/dist/src/tags/conditional.d.ts.map +1 -0
  43. package/dist/src/tags/index.d.ts +44 -0
  44. package/dist/src/tags/index.d.ts.map +1 -0
  45. package/dist/src/tags/partial.d.ts +3 -0
  46. package/dist/src/tags/partial.d.ts.map +1 -0
  47. package/dist/src/tags/table.d.ts +3 -0
  48. package/dist/src/tags/table.d.ts.map +1 -0
  49. package/dist/src/tokenizer/index.d.ts +10 -0
  50. package/dist/src/tokenizer/index.d.ts.map +1 -0
  51. package/dist/src/tokenizer/plugins/annotations.d.ts +3 -0
  52. package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -0
  53. package/dist/src/tokenizer/plugins/frontmatter.d.ts +3 -0
  54. package/dist/src/tokenizer/plugins/frontmatter.d.ts.map +1 -0
  55. package/dist/src/transformer.d.ts +12 -0
  56. package/dist/src/transformer.d.ts.map +1 -0
  57. package/dist/src/transforms/index.d.ts +4 -0
  58. package/dist/src/transforms/index.d.ts.map +1 -0
  59. package/dist/src/transforms/table.d.ts +3 -0
  60. package/dist/src/transforms/table.d.ts.map +1 -0
  61. package/dist/src/types.d.ts +88 -0
  62. package/dist/src/types.d.ts.map +1 -0
  63. package/dist/src/utils.d.ts +6 -0
  64. package/dist/src/utils.d.ts.map +1 -0
  65. package/dist/src/validator.d.ts +6 -0
  66. package/dist/src/validator.d.ts.map +1 -0
  67. package/global.d.ts +11 -0
  68. package/index.ts +138 -0
  69. package/package.json +63 -0
  70. package/spec/support/matchers.d.ts +6 -0
  71. package/src/ast/base.ts +41 -0
  72. package/src/ast/function.ts +22 -0
  73. package/src/ast/index.ts +28 -0
  74. package/src/ast/node.test.ts +307 -0
  75. package/src/ast/node.ts +71 -0
  76. package/src/ast/tag.ts +19 -0
  77. package/src/ast/variable.test.ts +35 -0
  78. package/src/ast/variable.ts +17 -0
  79. package/src/functions/index.test.ts +34 -0
  80. package/src/functions/index.ts +45 -0
  81. package/src/grammar/tag.d.ts +23 -0
  82. package/src/grammar/tag.js +1836 -0
  83. package/src/grammar/tag.pegjs +170 -0
  84. package/src/grammar/tag.test.ts +364 -0
  85. package/src/parser.test.ts +597 -0
  86. package/src/parser.ts +158 -0
  87. package/src/renderers/html.test.ts +62 -0
  88. package/src/renderers/html.ts +46 -0
  89. package/src/renderers/index.ts +5 -0
  90. package/src/renderers/react/react.test.ts +204 -0
  91. package/src/renderers/react/react.ts +51 -0
  92. package/src/renderers/react/shared.ts +16 -0
  93. package/src/renderers/react/static.ts +52 -0
  94. package/src/schema-types/class.ts +23 -0
  95. package/src/schema.ts +233 -0
  96. package/src/tags/conditional.ts +45 -0
  97. package/src/tags/index.ts +10 -0
  98. package/src/tags/partial.ts +33 -0
  99. package/src/tags/table.ts +5 -0
  100. package/src/tokenizer/index.ts +21 -0
  101. package/src/tokenizer/plugins/annotations.test.ts +337 -0
  102. package/src/tokenizer/plugins/annotations.ts +133 -0
  103. package/src/tokenizer/plugins/frontmatter.test.ts +86 -0
  104. package/src/tokenizer/plugins/frontmatter.ts +39 -0
  105. package/src/transformer.ts +72 -0
  106. package/src/transforms/index.ts +2 -0
  107. package/src/transforms/table.ts +51 -0
  108. package/src/types.ts +142 -0
  109. package/src/utils.test.ts +173 -0
  110. package/src/utils.ts +116 -0
  111. package/src/validator.test.ts +298 -0
  112. package/src/validator.ts +233 -0
@@ -0,0 +1,597 @@
1
+ // @ts-check
2
+
3
+ import Tokenizer from '../src/tokenizer';
4
+ import parser from '../src/parser';
5
+ import Variable from '../src/ast/variable';
6
+ import { any } from 'deep-assert';
7
+
8
+ describe('Markdown parser', function () {
9
+ const fence = '```';
10
+ const tokenizer = new Tokenizer();
11
+
12
+ function convert(example) {
13
+ const content = example.replace(/\n\s+/gm, '\n').trim();
14
+ const tokens = tokenizer.tokenize(content);
15
+ return parser(tokens);
16
+ }
17
+
18
+ describe('handling frontmatter', function () {
19
+ it('simple frontmatter', function () {
20
+ const example = convert(`
21
+ ---
22
+ foo: bar
23
+ ---
24
+ `);
25
+
26
+ expect(example).toDeepEqual({
27
+ ...any(),
28
+ type: 'document',
29
+ attributes: {
30
+ frontmatter: 'foo: bar',
31
+ },
32
+ children: [],
33
+ });
34
+ });
35
+
36
+ it('frontmatter followed by content', function () {
37
+ const example = convert(`
38
+ ---
39
+ foo: bar
40
+ test: true
41
+ ---
42
+
43
+ # This is a test
44
+ `);
45
+
46
+ expect(example).toDeepEqual({
47
+ ...any(),
48
+ type: 'document',
49
+ attributes: {
50
+ frontmatter: 'foo: bar\ntest: true',
51
+ },
52
+ children: [{ type: 'heading', attributes: { level: 1 }, ...any() }],
53
+ });
54
+ });
55
+ });
56
+
57
+ describe('handling attributes', function () {
58
+ it('for heading', function () {
59
+ const document = convert(`# Sample Heading`);
60
+ expect(document.children[0].attributes).toDeepEqual({ level: 1 });
61
+ });
62
+
63
+ it('for list', function () {
64
+ const unordered = convert(`
65
+ * Example 1
66
+ * Example 2
67
+ * Example 3
68
+ `);
69
+
70
+ const ordered = convert(`
71
+ 1. Example 1
72
+ 2. Example 2
73
+ 3. Example 3
74
+ `);
75
+
76
+ expect(unordered.children[0].attributes.ordered).toEqual(false);
77
+ expect(ordered.children[0].attributes.ordered).toEqual(true);
78
+ });
79
+
80
+ it('for link with one word', function () {
81
+ const document = convert(`
82
+ [foo](/bar)
83
+ `);
84
+
85
+ const link = document.children[0].children[0].children[0];
86
+ expect(link.attributes).toDeepEqual({ href: '/bar' });
87
+ });
88
+
89
+ it('for link', function () {
90
+ const document = convert(`
91
+ [this is a test](/bar)
92
+ `);
93
+
94
+ const link = document.children[0].children[0].children[0];
95
+ expect(link.attributes).toDeepEqual({ href: '/bar' });
96
+ });
97
+
98
+ it('for text', function () {
99
+ const document = convert(`
100
+ This is a test
101
+ `);
102
+
103
+ const p = document.children[0].children[0].children[0];
104
+ expect(p.attributes).toDeepEqual({ content: 'This is a test' });
105
+ });
106
+
107
+ it('for code fence', function () {
108
+ const simple = convert(`
109
+ ${fence}ruby
110
+ This is a test
111
+ ${fence}
112
+ `);
113
+
114
+ const complex = convert(`
115
+ ${fence}ruby this is a test
116
+ This is a test
117
+ ${fence}
118
+ `);
119
+
120
+ const empty = convert(`
121
+ ${fence}
122
+ This is a test
123
+ ${fence}
124
+ `);
125
+
126
+ expect(simple.children[0].attributes).toDeepEqual({
127
+ language: 'ruby',
128
+ content: 'This is a test\n',
129
+ });
130
+
131
+ expect(complex.children[0].attributes).toDeepEqual({
132
+ language: 'ruby',
133
+ content: 'This is a test\n',
134
+ });
135
+
136
+ expect(empty.children[0].attributes).toDeepEqual({
137
+ content: 'This is a test\n',
138
+ });
139
+ });
140
+ });
141
+
142
+ it('handling a header', function () {
143
+ const example = convert(`
144
+ # Sample Heading
145
+
146
+ This is a sample paragraph
147
+ `);
148
+
149
+ expect(example).toDeepEqualSubset({
150
+ type: 'document',
151
+ children: [
152
+ {
153
+ type: 'heading',
154
+ attributes: { level: 1 },
155
+ children: [
156
+ {
157
+ type: 'inline',
158
+ children: [
159
+ { type: 'text', attributes: { content: 'Sample Heading' } },
160
+ ],
161
+ },
162
+ ],
163
+ },
164
+ {
165
+ type: 'paragraph',
166
+ children: [
167
+ {
168
+ type: 'inline',
169
+ children: [
170
+ {
171
+ type: 'text',
172
+ attributes: { content: 'This is a sample paragraph' },
173
+ },
174
+ ],
175
+ },
176
+ ],
177
+ },
178
+ ],
179
+ });
180
+ });
181
+
182
+ it('handling an image', function () {
183
+ const example = convert('![Alt](/logo.png)');
184
+ expect(example).toDeepEqualSubset({
185
+ type: 'document',
186
+ children: [
187
+ {
188
+ type: 'paragraph',
189
+ attributes: {},
190
+ children: [
191
+ {
192
+ type: 'inline',
193
+ children: [
194
+ {
195
+ type: 'image',
196
+ attributes: { alt: 'Alt', src: '/logo.png' },
197
+ children: [],
198
+ },
199
+ ],
200
+ },
201
+ ],
202
+ },
203
+ ],
204
+ });
205
+ });
206
+
207
+ describe('handling lists', function () {
208
+ it('with bullets', function () {
209
+ const example = convert(`
210
+ * foo
211
+ * bar
212
+ `);
213
+
214
+ expect(example).toDeepEqualSubset({
215
+ type: 'document',
216
+ children: [
217
+ {
218
+ type: 'list',
219
+ attributes: { ordered: false },
220
+ children: [
221
+ {
222
+ type: 'item',
223
+ children: [
224
+ {
225
+ type: 'inline',
226
+ children: [
227
+ { type: 'text', attributes: { content: 'foo' } },
228
+ ],
229
+ },
230
+ ],
231
+ },
232
+ {
233
+ type: 'item',
234
+ children: [
235
+ {
236
+ type: 'inline',
237
+ children: [
238
+ { type: 'text', attributes: { content: 'bar' } },
239
+ ],
240
+ },
241
+ ],
242
+ },
243
+ ],
244
+ },
245
+ ],
246
+ });
247
+ });
248
+
249
+ it('with numbers', function () {
250
+ const example = convert(`
251
+ 1. foo
252
+ 2. bar
253
+ `);
254
+
255
+ expect(example).toDeepEqualSubset({
256
+ type: 'document',
257
+ children: [
258
+ {
259
+ type: 'list',
260
+ attributes: { ordered: true },
261
+ children: [
262
+ {
263
+ type: 'item',
264
+ children: [
265
+ {
266
+ type: 'inline',
267
+ children: [
268
+ { type: 'text', attributes: { content: 'foo' } },
269
+ ],
270
+ },
271
+ ],
272
+ },
273
+ {
274
+ type: 'item',
275
+ children: [
276
+ {
277
+ type: 'inline',
278
+ children: [
279
+ { type: 'text', attributes: { content: 'bar' } },
280
+ ],
281
+ },
282
+ ],
283
+ },
284
+ ],
285
+ },
286
+ ],
287
+ });
288
+ });
289
+ });
290
+
291
+ describe('handling fenced code', function () {
292
+ it('with a language', function () {
293
+ const example = convert(`
294
+ ${fence}ruby
295
+ puts "foo"
296
+ ${fence}
297
+ `);
298
+
299
+ expect(example).toDeepEqualSubset({
300
+ type: 'document',
301
+ children: [
302
+ {
303
+ type: 'fence',
304
+ attributes: { language: 'ruby', content: 'puts "foo"\n' },
305
+ },
306
+ ],
307
+ });
308
+ });
309
+
310
+ it('with an annotation', function () {
311
+ const example = convert(`
312
+ ${fence}ruby {% #foo .bar %}
313
+ test
314
+ ${fence}
315
+ `);
316
+
317
+ expect(example).toDeepEqualSubset({
318
+ type: 'document',
319
+ children: [
320
+ {
321
+ type: 'fence',
322
+ attributes: {
323
+ id: 'foo',
324
+ class: { bar: true },
325
+ language: 'ruby',
326
+ content: 'test\n',
327
+ },
328
+ },
329
+ ],
330
+ });
331
+ });
332
+ });
333
+
334
+ describe('handling tags', function () {
335
+ describe('at block level', function () {
336
+ it('with a class', function () {
337
+ const example = convert(`
338
+ {% callout .foo .bar %}
339
+ ### Heading
340
+
341
+ This is a paragraph
342
+ {% /callout %}
343
+ `);
344
+
345
+ expect(example).toDeepEqualSubset({
346
+ type: 'document',
347
+ children: [
348
+ {
349
+ type: 'tag',
350
+ tag: 'callout',
351
+ attributes: { class: { foo: true, bar: true } },
352
+ children: [
353
+ {
354
+ type: 'heading',
355
+ attributes: { level: 3 },
356
+ children: [
357
+ {
358
+ type: 'inline',
359
+ children: [
360
+ { type: 'text', attributes: { content: 'Heading' } },
361
+ ],
362
+ },
363
+ ],
364
+ },
365
+ {
366
+ type: 'paragraph',
367
+ children: [
368
+ {
369
+ type: 'inline',
370
+ children: [
371
+ {
372
+ type: 'text',
373
+ attributes: { content: 'This is a paragraph' },
374
+ },
375
+ ],
376
+ },
377
+ ],
378
+ },
379
+ ],
380
+ },
381
+ ],
382
+ });
383
+ });
384
+
385
+ it('with nesting', function () {
386
+ const example = convert(`
387
+ {% callout %}
388
+ {% callout %}
389
+ This is a test
390
+ {% /callout %}
391
+ {% /callout %}
392
+ `);
393
+
394
+ expect(example).toDeepEqualSubset({
395
+ type: 'document',
396
+ children: [
397
+ {
398
+ type: 'tag',
399
+ tag: 'callout',
400
+ children: [
401
+ {
402
+ type: 'tag',
403
+ tag: 'callout',
404
+ children: [
405
+ {
406
+ type: 'paragraph',
407
+ children: [
408
+ {
409
+ type: 'inline',
410
+ children: [
411
+ {
412
+ type: 'text',
413
+ attributes: { content: 'This is a test' },
414
+ },
415
+ ],
416
+ },
417
+ ],
418
+ },
419
+ ],
420
+ },
421
+ ],
422
+ },
423
+ ],
424
+ });
425
+ });
426
+ });
427
+ });
428
+
429
+ describe('handling annotations', function () {
430
+ describe('in a header', function () {
431
+ it('with an id', function () {
432
+ const example = convert(`# Sample Heading {% #foo-bar %}`);
433
+ expect(example).toDeepEqualSubset({
434
+ type: 'document',
435
+ children: [
436
+ {
437
+ type: 'heading',
438
+ attributes: { id: 'foo-bar', level: 1 },
439
+ children: [
440
+ {
441
+ type: 'inline',
442
+ children: [
443
+ {
444
+ type: 'text',
445
+ attributes: { content: 'Sample Heading ' },
446
+ },
447
+ ],
448
+ },
449
+ ],
450
+ },
451
+ ],
452
+ });
453
+ });
454
+
455
+ it('with a class', function () {
456
+ const example = convert(`# Sample Heading {% .foo-bar .test %}`);
457
+ expect(example).toDeepEqualSubset({
458
+ type: 'document',
459
+ children: [
460
+ {
461
+ type: 'heading',
462
+ attributes: { class: { 'foo-bar': true, test: true }, level: 1 },
463
+ children: [
464
+ {
465
+ type: 'inline',
466
+ children: [
467
+ {
468
+ type: 'text',
469
+ attributes: { content: 'Sample Heading ' },
470
+ },
471
+ ],
472
+ },
473
+ ],
474
+ },
475
+ ],
476
+ });
477
+ });
478
+
479
+ it('with complex values', function () {
480
+ const example = convert(
481
+ `# Sample Heading {% #asdf .foo-bar .test foo="bar" %}`
482
+ );
483
+ expect(example).toDeepEqualSubset({
484
+ type: 'document',
485
+ children: [
486
+ {
487
+ type: 'heading',
488
+ attributes: {
489
+ class: { 'foo-bar': true, test: true },
490
+ id: 'asdf',
491
+ level: 1,
492
+ foo: 'bar',
493
+ },
494
+ children: [
495
+ {
496
+ type: 'inline',
497
+ children: [
498
+ {
499
+ type: 'text',
500
+ attributes: { content: 'Sample Heading ' },
501
+ },
502
+ ],
503
+ },
504
+ ],
505
+ },
506
+ ],
507
+ });
508
+ });
509
+ });
510
+ });
511
+
512
+ describe('handling variables', function () {
513
+ it('by itself on a line', function () {
514
+ const example = convert(`{% $test %}`);
515
+ expect(example).toDeepEqualSubset({
516
+ type: 'document',
517
+ children: [
518
+ {
519
+ type: 'paragraph',
520
+ children: [
521
+ {
522
+ type: 'inline',
523
+ children: [
524
+ {
525
+ type: 'text',
526
+ attributes: { content: new Variable(['test'], null) },
527
+ },
528
+ ],
529
+ },
530
+ ],
531
+ },
532
+ ],
533
+ });
534
+ });
535
+
536
+ it('in an inline text node', function () {
537
+ const example = convert(`This is a test: {% $test %}`);
538
+ expect(example).toDeepEqualSubset({
539
+ type: 'document',
540
+ children: [
541
+ {
542
+ type: 'paragraph',
543
+ children: [
544
+ {
545
+ type: 'inline',
546
+ children: [
547
+ { type: 'text', attributes: { content: 'This is a test: ' } },
548
+ {
549
+ type: 'text',
550
+ attributes: { content: new Variable(['test'], null) },
551
+ },
552
+ ],
553
+ },
554
+ ],
555
+ },
556
+ ],
557
+ });
558
+ });
559
+
560
+ it('with nested property access', function () {
561
+ const example = convert('{% $bar.baz[1].test %}');
562
+ expect(example).toDeepEqualSubset({
563
+ type: 'document',
564
+ children: [
565
+ {
566
+ type: 'paragraph',
567
+ children: [
568
+ {
569
+ type: 'inline',
570
+ children: [
571
+ {
572
+ type: 'text',
573
+ attributes: {
574
+ content: new Variable(['bar', 'baz', 1, 'test'], null),
575
+ },
576
+ },
577
+ ],
578
+ },
579
+ ],
580
+ },
581
+ ],
582
+ });
583
+ });
584
+ });
585
+
586
+ it('parsing nested tags with indentation should not throw', function () {
587
+ expect(() => {
588
+ convert(`
589
+ {% tag1 %}
590
+ {% tag2 %}
591
+ Contents
592
+ {% /tag2 %}
593
+ {% /tag1 %}
594
+ `);
595
+ }).not.toThrow();
596
+ });
597
+ });