@markdoc/markdoc 0.5.12 → 0.5.14
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.
- package/README.md +35 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +472 -448
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +472 -448
- package/dist/index.mjs.map +4 -4
- package/dist/react.js +4 -4
- package/dist/react.js.map +2 -2
- package/dist/src/ast/base.d.ts +1 -1
- package/dist/src/ast/base.d.ts.map +1 -1
- package/dist/src/ast/function.d.ts +1 -1
- package/dist/src/ast/function.d.ts.map +1 -1
- package/dist/src/ast/index.d.ts +2 -2
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/ast/variable.d.ts +1 -1
- package/dist/src/ast/variable.d.ts.map +1 -1
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/functions/index.d.ts.map +1 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/renderers/html.d.ts.map +1 -1
- package/dist/src/renderers/react/react.d.ts +2 -2
- package/dist/src/renderers/react/react.d.ts.map +1 -1
- package/dist/src/renderers/react/static.d.ts +1 -1
- package/dist/src/renderers/react/static.d.ts.map +1 -1
- package/dist/src/schema-types/class.d.ts +1 -1
- package/dist/src/schema-types/class.d.ts.map +1 -1
- package/dist/src/schema-types/conditional.d.ts +1 -1
- package/dist/src/schema-types/conditional.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/conditional.d.ts +1 -1
- package/dist/src/tags/conditional.d.ts.map +1 -1
- package/dist/src/tokenizer/index.d.ts.map +1 -1
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/validator.d.ts +1 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/index.ts +21 -22
- package/package.json +9 -14
- package/spec/support/matchers.d.ts +6 -4
- package/src/ast/base.ts +21 -6
- package/src/ast/function.ts +1 -1
- package/src/ast/index.ts +3 -4
- package/src/ast/node.test.ts +126 -36
- package/src/ast/node.ts +4 -4
- package/src/ast/variable.test.ts +5 -4
- package/src/ast/variable.ts +1 -1
- package/src/formatter.test.ts +4 -3
- package/src/formatter.ts +11 -11
- package/src/functions/index.test.ts +8 -7
- package/src/functions/index.ts +1 -1
- package/src/grammar/tag.d.ts +1 -1
- package/src/grammar/tag.js +208 -285
- package/src/grammar/tag.pegjs +8 -8
- package/src/grammar/tag.test.ts +80 -48
- package/src/parser.test.ts +124 -66
- package/src/parser.ts +29 -7
- package/src/renderers/html.test.ts +56 -16
- package/src/renderers/html.ts +10 -2
- package/src/renderers/react/react.test.ts +39 -27
- package/src/renderers/react/react.ts +9 -10
- package/src/renderers/react/static.ts +5 -6
- package/src/schema-types/class.ts +1 -1
- package/src/schema-types/conditional.ts +1 -1
- package/src/schema.ts +9 -4
- package/src/tag.test.ts +2 -1
- package/src/tag.ts +1 -1
- package/src/tags/conditional.ts +4 -4
- package/src/tags/index.ts +2 -2
- package/src/tags/partial.ts +1 -1
- package/src/tokenizer/index.ts +6 -5
- package/src/tokenizer/plugins/annotations.test.ts +29 -28
- package/src/tokenizer/plugins/annotations.ts +12 -12
- package/src/tokenizer/plugins/comments.test.ts +11 -10
- package/src/tokenizer/plugins/comments.ts +2 -2
- package/src/tokenizer/plugins/frontmatter.test.ts +7 -6
- package/src/tokenizer/plugins/frontmatter.ts +2 -2
- package/src/tokenizer/plugins/link.test.ts +14 -13
- package/src/transformer.ts +11 -7
- package/src/transforms/table.test.ts +18 -17
- package/src/transforms/table.ts +1 -1
- package/src/utils.test.ts +23 -22
- package/src/utils.ts +3 -3
- package/src/validator.test.ts +205 -31
- package/src/validator.ts +21 -13
package/src/parser.test.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
1
2
|
// @ts-check
|
|
2
3
|
|
|
3
|
-
import Tokenizer from '../src/tokenizer';
|
|
4
|
-
import parser from '../src/parser';
|
|
5
|
-
import Variable from '../src/ast/variable';
|
|
6
4
|
import { any } from 'deep-assert';
|
|
5
|
+
import Variable from '../src/ast/variable';
|
|
6
|
+
import parser from '../src/parser';
|
|
7
|
+
import Tokenizer from '../src/tokenizer';
|
|
7
8
|
|
|
8
|
-
describe('Markdown parser',
|
|
9
|
+
describe('Markdown parser', () => {
|
|
9
10
|
const fence = '```';
|
|
10
11
|
const tokenizer = new Tokenizer({ allowComments: true });
|
|
11
12
|
|
|
@@ -15,8 +16,8 @@ describe('Markdown parser', function () {
|
|
|
15
16
|
return parser(tokens, options);
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
describe('handling options',
|
|
19
|
-
it('no args',
|
|
19
|
+
describe('handling options', () => {
|
|
20
|
+
it('no args', () => {
|
|
20
21
|
const example = convert(`# This is a test`);
|
|
21
22
|
expect(example.children[0]).toDeepEqual({
|
|
22
23
|
...any(),
|
|
@@ -28,7 +29,7 @@ describe('Markdown parser', function () {
|
|
|
28
29
|
});
|
|
29
30
|
});
|
|
30
31
|
|
|
31
|
-
it('filename as string',
|
|
32
|
+
it('filename as string', () => {
|
|
32
33
|
const example = convert(`# This is a test`, 'foo.md');
|
|
33
34
|
expect(example.children[0]).toDeepEqual({
|
|
34
35
|
...any(),
|
|
@@ -40,7 +41,7 @@ describe('Markdown parser', function () {
|
|
|
40
41
|
});
|
|
41
42
|
});
|
|
42
43
|
|
|
43
|
-
it('filename as property',
|
|
44
|
+
it('filename as property', () => {
|
|
44
45
|
const example = convert(`# This is a test`, { file: 'foo.md' });
|
|
45
46
|
expect(example.children[0]).toDeepEqual({
|
|
46
47
|
...any(),
|
|
@@ -52,7 +53,7 @@ describe('Markdown parser', function () {
|
|
|
52
53
|
});
|
|
53
54
|
});
|
|
54
55
|
|
|
55
|
-
it('location off',
|
|
56
|
+
it('location off', () => {
|
|
56
57
|
const example = convert(`# This is a test`, {
|
|
57
58
|
file: 'foo.md',
|
|
58
59
|
location: false,
|
|
@@ -60,7 +61,7 @@ describe('Markdown parser', function () {
|
|
|
60
61
|
expect(example.children[0].location).toBeUndefined();
|
|
61
62
|
});
|
|
62
63
|
|
|
63
|
-
it('location on',
|
|
64
|
+
it('location on', () => {
|
|
64
65
|
const example = convert(`# This is a test`, {
|
|
65
66
|
file: 'foo.md',
|
|
66
67
|
location: true,
|
|
@@ -73,8 +74,8 @@ describe('Markdown parser', function () {
|
|
|
73
74
|
});
|
|
74
75
|
});
|
|
75
76
|
|
|
76
|
-
describe('handling frontmatter',
|
|
77
|
-
it('simple frontmatter',
|
|
77
|
+
describe('handling frontmatter', () => {
|
|
78
|
+
it('simple frontmatter', () => {
|
|
78
79
|
const example = convert(`
|
|
79
80
|
---
|
|
80
81
|
foo: bar
|
|
@@ -91,7 +92,7 @@ describe('Markdown parser', function () {
|
|
|
91
92
|
});
|
|
92
93
|
});
|
|
93
94
|
|
|
94
|
-
it('frontmatter followed by content',
|
|
95
|
+
it('frontmatter followed by content', () => {
|
|
95
96
|
const example = convert(`
|
|
96
97
|
---
|
|
97
98
|
foo: bar
|
|
@@ -112,8 +113,8 @@ describe('Markdown parser', function () {
|
|
|
112
113
|
});
|
|
113
114
|
});
|
|
114
115
|
|
|
115
|
-
describe('handling attributes',
|
|
116
|
-
it('for emphasis',
|
|
116
|
+
describe('handling attributes', () => {
|
|
117
|
+
it('for emphasis', () => {
|
|
117
118
|
const items = (doc, n = 0) =>
|
|
118
119
|
doc.children[0].children[0].children[n].attributes.marker;
|
|
119
120
|
|
|
@@ -136,12 +137,12 @@ describe('Markdown parser', function () {
|
|
|
136
137
|
expect(items(example3a, 1)).toEqual('**');
|
|
137
138
|
});
|
|
138
139
|
|
|
139
|
-
it('for heading',
|
|
140
|
+
it('for heading', () => {
|
|
140
141
|
const document = convert(`# Sample Heading`);
|
|
141
142
|
expect(document.children[0].attributes).toDeepEqual({ level: 1 });
|
|
142
143
|
});
|
|
143
144
|
|
|
144
|
-
it('for list',
|
|
145
|
+
it('for list', () => {
|
|
145
146
|
const unordered = convert(`
|
|
146
147
|
* Example 1
|
|
147
148
|
* Example 2
|
|
@@ -158,7 +159,7 @@ describe('Markdown parser', function () {
|
|
|
158
159
|
expect(ordered.children[0].attributes.ordered).toEqual(true);
|
|
159
160
|
});
|
|
160
161
|
|
|
161
|
-
it('for ordered list start',
|
|
162
|
+
it('for ordered list start', () => {
|
|
162
163
|
const unordered = convert(`
|
|
163
164
|
* Example 1
|
|
164
165
|
* Example 2
|
|
@@ -184,7 +185,7 @@ describe('Markdown parser', function () {
|
|
|
184
185
|
expect(start(numberedStartAtOne)).toEqual(undefined);
|
|
185
186
|
});
|
|
186
187
|
|
|
187
|
-
it('for link with one word',
|
|
188
|
+
it('for link with one word', () => {
|
|
188
189
|
const document = convert(`
|
|
189
190
|
[foo](/bar)
|
|
190
191
|
`);
|
|
@@ -193,7 +194,7 @@ describe('Markdown parser', function () {
|
|
|
193
194
|
expect(link.attributes).toDeepEqual({ href: '/bar' });
|
|
194
195
|
});
|
|
195
196
|
|
|
196
|
-
it('for link with one word and a title',
|
|
197
|
+
it('for link with one word and a title', () => {
|
|
197
198
|
const document = convert(`
|
|
198
199
|
[foo](/bar "title")
|
|
199
200
|
`);
|
|
@@ -202,7 +203,7 @@ describe('Markdown parser', function () {
|
|
|
202
203
|
expect(link.attributes).toDeepEqual({ href: '/bar', title: 'title' });
|
|
203
204
|
});
|
|
204
205
|
|
|
205
|
-
it('for link',
|
|
206
|
+
it('for link', () => {
|
|
206
207
|
const document = convert(`
|
|
207
208
|
[this is a test](/bar)
|
|
208
209
|
`);
|
|
@@ -211,7 +212,7 @@ describe('Markdown parser', function () {
|
|
|
211
212
|
expect(link.attributes).toDeepEqual({ href: '/bar' });
|
|
212
213
|
});
|
|
213
214
|
|
|
214
|
-
it('for text',
|
|
215
|
+
it('for text', () => {
|
|
215
216
|
const document = convert(`
|
|
216
217
|
This is a test
|
|
217
218
|
`);
|
|
@@ -220,7 +221,7 @@ describe('Markdown parser', function () {
|
|
|
220
221
|
expect(p.attributes).toDeepEqual({ content: 'This is a test' });
|
|
221
222
|
});
|
|
222
223
|
|
|
223
|
-
it('for code fence',
|
|
224
|
+
it('for code fence', () => {
|
|
224
225
|
const simple = convert(`
|
|
225
226
|
${fence}ruby
|
|
226
227
|
This is a test
|
|
@@ -254,7 +255,7 @@ describe('Markdown parser', function () {
|
|
|
254
255
|
});
|
|
255
256
|
});
|
|
256
257
|
|
|
257
|
-
it('for image with no title',
|
|
258
|
+
it('for image with no title', () => {
|
|
258
259
|
const document = convert(``);
|
|
259
260
|
const image = document.children[0].children[0].children[0];
|
|
260
261
|
expect(image.attributes.title).toBe(undefined);
|
|
@@ -262,7 +263,7 @@ describe('Markdown parser', function () {
|
|
|
262
263
|
expect(image.attributes.alt).toBe('foo');
|
|
263
264
|
});
|
|
264
265
|
|
|
265
|
-
it('for image with a title',
|
|
266
|
+
it('for image with a title', () => {
|
|
266
267
|
const document = convert(``);
|
|
267
268
|
const image = document.children[0].children[0].children[0];
|
|
268
269
|
expect(image.attributes.title).toBe('title');
|
|
@@ -270,7 +271,7 @@ describe('Markdown parser', function () {
|
|
|
270
271
|
expect(image.attributes.alt).toBe('foo');
|
|
271
272
|
});
|
|
272
273
|
|
|
273
|
-
it('for table with alignments',
|
|
274
|
+
it('for table with alignments', () => {
|
|
274
275
|
const document = convert(`
|
|
275
276
|
| Left | Center | Right |
|
|
276
277
|
| :--- | :----: | ----: |
|
|
@@ -288,7 +289,7 @@ describe('Markdown parser', function () {
|
|
|
288
289
|
});
|
|
289
290
|
});
|
|
290
291
|
|
|
291
|
-
it('handling a header',
|
|
292
|
+
it('handling a header', () => {
|
|
292
293
|
const example = convert(`
|
|
293
294
|
# Sample Heading
|
|
294
295
|
|
|
@@ -328,7 +329,7 @@ describe('Markdown parser', function () {
|
|
|
328
329
|
});
|
|
329
330
|
});
|
|
330
331
|
|
|
331
|
-
it('handling an image',
|
|
332
|
+
it('handling an image', () => {
|
|
332
333
|
const example = convert('');
|
|
333
334
|
expect(example).toDeepEqualSubset({
|
|
334
335
|
type: 'document',
|
|
@@ -353,8 +354,8 @@ describe('Markdown parser', function () {
|
|
|
353
354
|
});
|
|
354
355
|
});
|
|
355
356
|
|
|
356
|
-
describe('handling lists',
|
|
357
|
-
it('with bullets',
|
|
357
|
+
describe('handling lists', () => {
|
|
358
|
+
it('with bullets', () => {
|
|
358
359
|
const example = convert(`
|
|
359
360
|
* foo
|
|
360
361
|
* bar
|
|
@@ -395,7 +396,7 @@ describe('Markdown parser', function () {
|
|
|
395
396
|
});
|
|
396
397
|
});
|
|
397
398
|
|
|
398
|
-
it('with numbers',
|
|
399
|
+
it('with numbers', () => {
|
|
399
400
|
const example = convert(`
|
|
400
401
|
1. foo
|
|
401
402
|
2. bar
|
|
@@ -437,8 +438,8 @@ describe('Markdown parser', function () {
|
|
|
437
438
|
});
|
|
438
439
|
});
|
|
439
440
|
|
|
440
|
-
describe('handling fenced code',
|
|
441
|
-
it('with a language',
|
|
441
|
+
describe('handling fenced code', () => {
|
|
442
|
+
it('with a language', () => {
|
|
442
443
|
const example = convert(`
|
|
443
444
|
${fence}ruby
|
|
444
445
|
puts "foo"
|
|
@@ -456,7 +457,7 @@ describe('Markdown parser', function () {
|
|
|
456
457
|
});
|
|
457
458
|
});
|
|
458
459
|
|
|
459
|
-
it('with an annotation',
|
|
460
|
+
it('with an annotation', () => {
|
|
460
461
|
const example = convert(`
|
|
461
462
|
${fence}ruby {% #foo .bar %}
|
|
462
463
|
test
|
|
@@ -480,9 +481,9 @@ describe('Markdown parser', function () {
|
|
|
480
481
|
});
|
|
481
482
|
});
|
|
482
483
|
|
|
483
|
-
describe('handling tags',
|
|
484
|
-
describe('at block level',
|
|
485
|
-
it('with a class',
|
|
484
|
+
describe('handling tags', () => {
|
|
485
|
+
describe('at block level', () => {
|
|
486
|
+
it('with a class', () => {
|
|
486
487
|
const example = convert(`
|
|
487
488
|
{% callout .foo .bar %}
|
|
488
489
|
### Heading
|
|
@@ -531,7 +532,7 @@ describe('Markdown parser', function () {
|
|
|
531
532
|
});
|
|
532
533
|
});
|
|
533
534
|
|
|
534
|
-
it('with nesting',
|
|
535
|
+
it('with nesting', () => {
|
|
535
536
|
const example = convert(`
|
|
536
537
|
{% callout %}
|
|
537
538
|
{% callout %}
|
|
@@ -575,9 +576,9 @@ describe('Markdown parser', function () {
|
|
|
575
576
|
});
|
|
576
577
|
});
|
|
577
578
|
|
|
578
|
-
describe('handling annotations',
|
|
579
|
-
describe('in a header',
|
|
580
|
-
it('with an id',
|
|
579
|
+
describe('handling annotations', () => {
|
|
580
|
+
describe('in a header', () => {
|
|
581
|
+
it('with an id', () => {
|
|
581
582
|
const example = convert(`# Sample Heading {% #foo-bar %}`);
|
|
582
583
|
expect(example).toDeepEqualSubset({
|
|
583
584
|
type: 'document',
|
|
@@ -601,7 +602,7 @@ describe('Markdown parser', function () {
|
|
|
601
602
|
});
|
|
602
603
|
});
|
|
603
604
|
|
|
604
|
-
it('with a class',
|
|
605
|
+
it('with a class', () => {
|
|
605
606
|
const example = convert(`# Sample Heading {% .foo-bar .test %}`);
|
|
606
607
|
expect(example).toDeepEqualSubset({
|
|
607
608
|
type: 'document',
|
|
@@ -625,9 +626,9 @@ describe('Markdown parser', function () {
|
|
|
625
626
|
});
|
|
626
627
|
});
|
|
627
628
|
|
|
628
|
-
it('with complex values',
|
|
629
|
+
it('with complex values', () => {
|
|
629
630
|
const example = convert(
|
|
630
|
-
`# Sample Heading {% #asdf .foo-bar .test foo="bar" %}
|
|
631
|
+
`# Sample Heading {% #asdf .foo-bar .test foo="bar" %}`,
|
|
631
632
|
);
|
|
632
633
|
expect(example).toDeepEqualSubset({
|
|
633
634
|
type: 'document',
|
|
@@ -655,11 +656,32 @@ describe('Markdown parser', function () {
|
|
|
655
656
|
],
|
|
656
657
|
});
|
|
657
658
|
});
|
|
659
|
+
|
|
660
|
+
it('with a valid level', () => {
|
|
661
|
+
const example = convert(`# Sample Heading {% level=2 %}`);
|
|
662
|
+
const heading = example.children[0];
|
|
663
|
+
expect(heading.attributes.level).toEqual(2);
|
|
664
|
+
expect(heading.errors).toDeepEqualSubset([
|
|
665
|
+
{ id: 'duplicate-attribute', level: 'warning' },
|
|
666
|
+
]);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it('with an invalid level', () => {
|
|
670
|
+
const levels = ['"1>x"', '"2"', '0', '7'];
|
|
671
|
+
for (const level of levels) {
|
|
672
|
+
const example = convert(`# Sample Heading {% level=${level} %}`);
|
|
673
|
+
const heading = example.children[0];
|
|
674
|
+
expect(heading.attributes.level).toEqual(1);
|
|
675
|
+
expect(heading.errors).toDeepEqualSubset([
|
|
676
|
+
{ id: 'attribute-value-invalid', level: 'error' },
|
|
677
|
+
]);
|
|
678
|
+
}
|
|
679
|
+
});
|
|
658
680
|
});
|
|
659
681
|
});
|
|
660
682
|
|
|
661
|
-
describe('handling variables',
|
|
662
|
-
it('by itself on a line',
|
|
683
|
+
describe('handling variables', () => {
|
|
684
|
+
it('by itself on a line', () => {
|
|
663
685
|
const example = convert(`{% $test %}`);
|
|
664
686
|
expect(example).toDeepEqualSubset({
|
|
665
687
|
type: 'document',
|
|
@@ -682,7 +704,7 @@ describe('Markdown parser', function () {
|
|
|
682
704
|
});
|
|
683
705
|
});
|
|
684
706
|
|
|
685
|
-
it('in an inline text node',
|
|
707
|
+
it('in an inline text node', () => {
|
|
686
708
|
const example = convert(`This is a test: {% $test %}`);
|
|
687
709
|
expect(example).toDeepEqualSubset({
|
|
688
710
|
type: 'document',
|
|
@@ -706,7 +728,7 @@ describe('Markdown parser', function () {
|
|
|
706
728
|
});
|
|
707
729
|
});
|
|
708
730
|
|
|
709
|
-
it('with nested property access',
|
|
731
|
+
it('with nested property access', () => {
|
|
710
732
|
const example = convert('{% $bar.baz[1].test %}');
|
|
711
733
|
expect(example).toDeepEqualSubset({
|
|
712
734
|
type: 'document',
|
|
@@ -732,7 +754,7 @@ describe('Markdown parser', function () {
|
|
|
732
754
|
});
|
|
733
755
|
});
|
|
734
756
|
|
|
735
|
-
it('parsing nested tags with indentation should not throw',
|
|
757
|
+
it('parsing nested tags with indentation should not throw', () => {
|
|
736
758
|
expect(() => {
|
|
737
759
|
convert(`
|
|
738
760
|
{% tag1 %}
|
|
@@ -744,7 +766,7 @@ describe('Markdown parser', function () {
|
|
|
744
766
|
}).not.toThrow();
|
|
745
767
|
});
|
|
746
768
|
|
|
747
|
-
it('parsing comments',
|
|
769
|
+
it('parsing comments', () => {
|
|
748
770
|
const example = convert(`
|
|
749
771
|
this is a test
|
|
750
772
|
|
|
@@ -760,8 +782,44 @@ describe('Markdown parser', function () {
|
|
|
760
782
|
});
|
|
761
783
|
});
|
|
762
784
|
|
|
763
|
-
describe('handles attribute errors correctly',
|
|
764
|
-
|
|
785
|
+
describe('handles attribute errors correctly', () => {
|
|
786
|
+
for (const name of ['__proto__', 'prototype', 'constructor']) {
|
|
787
|
+
for (const source of [
|
|
788
|
+
`{% foo ${name}={src:"javascript:alert(1)"} /%}`,
|
|
789
|
+
`text {% ${name}={src:"javascript:alert(1)"} %}`,
|
|
790
|
+
` {% ${name}={src:"javascript:alert(1)"} %}`,
|
|
791
|
+
`${fence} {% ${name}={src:"javascript:alert(1)"} %}\ntext\n${fence}`,
|
|
792
|
+
]) {
|
|
793
|
+
it(`rejects reserved attribute ${name} in ${source}`, () => {
|
|
794
|
+
const document = convert(source);
|
|
795
|
+
const node = [...document.walk()].find((node) =>
|
|
796
|
+
node.errors.some((error) => error.id === 'attribute-name-invalid'),
|
|
797
|
+
);
|
|
798
|
+
|
|
799
|
+
expect(node).toBeDefined();
|
|
800
|
+
expect(node?.errors).toDeepEqualSubset([
|
|
801
|
+
{ id: 'attribute-name-invalid', level: 'critical' },
|
|
802
|
+
]);
|
|
803
|
+
expect(Object.getPrototypeOf(node?.attributes)).toBeNull();
|
|
804
|
+
expect(
|
|
805
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: Keep the regression check compatible with ES2021.
|
|
806
|
+
Object.prototype.hasOwnProperty.call(node?.attributes, name),
|
|
807
|
+
).toBe(false);
|
|
808
|
+
expect(node?.attributes.src).toBeUndefined();
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
it('does not treat inherited property names as duplicates', () => {
|
|
814
|
+
const document = convert('{% foo toString="first" toString="second" /%}');
|
|
815
|
+
expect(document.children[0].errors).toDeepEqualSubset([
|
|
816
|
+
{ id: 'duplicate-attribute', level: 'warning' },
|
|
817
|
+
]);
|
|
818
|
+
expect(document.children[0].errors.length).toBe(1);
|
|
819
|
+
expect(document.children[0].attributes.toString).toBe('second');
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
it('with error for duplicate attributes', () => {
|
|
765
823
|
const example = convert(`{% foo bar=1 bar=2 bar=3 bar=4 /%}`);
|
|
766
824
|
expect(example.children[0].errors.length).toBe(3);
|
|
767
825
|
expect(example).toDeepEqualSubset({
|
|
@@ -779,7 +837,7 @@ describe('Markdown parser', function () {
|
|
|
779
837
|
});
|
|
780
838
|
});
|
|
781
839
|
|
|
782
|
-
it('with error for duplicate ids',
|
|
840
|
+
it('with error for duplicate ids', () => {
|
|
783
841
|
const example = convert(`{% foo #bar #baz #qux /%}`);
|
|
784
842
|
expect(example.children[0].errors.length).toBe(2);
|
|
785
843
|
expect(example).toDeepEqualSubset({
|
|
@@ -796,7 +854,7 @@ describe('Markdown parser', function () {
|
|
|
796
854
|
});
|
|
797
855
|
});
|
|
798
856
|
|
|
799
|
-
it('with annotation values',
|
|
857
|
+
it('with annotation values', () => {
|
|
800
858
|
const example = convert(`testing {% foo=1 foo=2 %}`);
|
|
801
859
|
expect(example.children[0].errors.length).toBe(1);
|
|
802
860
|
expect(example).toDeepEqualSubset({
|
|
@@ -810,7 +868,7 @@ describe('Markdown parser', function () {
|
|
|
810
868
|
});
|
|
811
869
|
});
|
|
812
870
|
|
|
813
|
-
it('across annotations on the same node',
|
|
871
|
+
it('across annotations on the same node', () => {
|
|
814
872
|
const example = convert(`testing {% foo=1 %} another test {% foo=1 %}`);
|
|
815
873
|
expect(example.children[0].errors.length).toBe(1);
|
|
816
874
|
expect(example).toDeepEqualSubset({
|
|
@@ -824,13 +882,13 @@ describe('Markdown parser', function () {
|
|
|
824
882
|
});
|
|
825
883
|
});
|
|
826
884
|
|
|
827
|
-
it('with no error for multiple classes',
|
|
885
|
+
it('with no error for multiple classes', () => {
|
|
828
886
|
const example = convert(`{% foo .bar .baz .qux /%}`);
|
|
829
887
|
expect(example.children[0].errors.length).toBe(0);
|
|
830
888
|
});
|
|
831
889
|
});
|
|
832
890
|
|
|
833
|
-
it('displays error for annotations in a fence',
|
|
891
|
+
it('displays error for annotations in a fence', () => {
|
|
834
892
|
const example = convert(`
|
|
835
893
|
~~~
|
|
836
894
|
test
|
|
@@ -843,7 +901,7 @@ describe('Markdown parser', function () {
|
|
|
843
901
|
expect(example.children[0].errors[0]?.id).toEqual('no-inline-annotations');
|
|
844
902
|
});
|
|
845
903
|
|
|
846
|
-
it('correctly identifies inlines',
|
|
904
|
+
it('correctly identifies inlines', () => {
|
|
847
905
|
const example = convert(`
|
|
848
906
|
# This is a test
|
|
849
907
|
|
|
@@ -917,8 +975,8 @@ describe('Markdown parser', function () {
|
|
|
917
975
|
});
|
|
918
976
|
});
|
|
919
977
|
|
|
920
|
-
describe('handles structural errors correctly',
|
|
921
|
-
it('with unmatched closing tag',
|
|
978
|
+
describe('handles structural errors correctly', () => {
|
|
979
|
+
it('with unmatched closing tag', () => {
|
|
922
980
|
const example = convert(`
|
|
923
981
|
{% foo %}
|
|
924
982
|
Test
|
|
@@ -928,7 +986,7 @@ describe('Markdown parser', function () {
|
|
|
928
986
|
expect(example.children[0].errors[0].id).toEqual('missing-closing');
|
|
929
987
|
});
|
|
930
988
|
|
|
931
|
-
it('missing opening',
|
|
989
|
+
it('missing opening', () => {
|
|
932
990
|
const example = convert(`
|
|
933
991
|
This a test
|
|
934
992
|
{% /foo %}
|
|
@@ -937,7 +995,7 @@ describe('Markdown parser', function () {
|
|
|
937
995
|
expect(example.children[1].errors[0].id).toEqual('missing-opening');
|
|
938
996
|
});
|
|
939
997
|
|
|
940
|
-
it('with missing closing tag',
|
|
998
|
+
it('with missing closing tag', () => {
|
|
941
999
|
const example = convert(`
|
|
942
1000
|
{% foo %}
|
|
943
1001
|
Test
|
|
@@ -947,7 +1005,7 @@ describe('Markdown parser', function () {
|
|
|
947
1005
|
});
|
|
948
1006
|
});
|
|
949
1007
|
|
|
950
|
-
describe('table parsing',
|
|
1008
|
+
describe('table parsing', () => {
|
|
951
1009
|
function setupTableDoc(rows: string[]) {
|
|
952
1010
|
return `
|
|
953
1011
|
{% table %}
|
|
@@ -959,7 +1017,7 @@ describe('Markdown parser', function () {
|
|
|
959
1017
|
`;
|
|
960
1018
|
}
|
|
961
1019
|
|
|
962
|
-
it('should preserve default if tag during table parsing without extra parser args',
|
|
1020
|
+
it('should preserve default if tag during table parsing without extra parser args', () => {
|
|
963
1021
|
const document = setupTableDoc([
|
|
964
1022
|
`
|
|
965
1023
|
{% if $fakeCondition.condition1 %}
|
|
@@ -980,7 +1038,7 @@ describe('Markdown parser', function () {
|
|
|
980
1038
|
expect(tags).toContain('if');
|
|
981
1039
|
});
|
|
982
1040
|
|
|
983
|
-
it('should not preserve unregistered tags during table parsing without extra parser args',
|
|
1041
|
+
it('should not preserve unregistered tags during table parsing without extra parser args', () => {
|
|
984
1042
|
const document = setupTableDoc([
|
|
985
1043
|
`
|
|
986
1044
|
{% if-pref conditions=[{platform: 'web'}] %}
|
|
@@ -1002,7 +1060,7 @@ describe('Markdown parser', function () {
|
|
|
1002
1060
|
expect(tags).not.toContain('if-pref');
|
|
1003
1061
|
});
|
|
1004
1062
|
|
|
1005
|
-
it('should preserve all registered tags and ignore unregistered tags during table parsing with extra parser args',
|
|
1063
|
+
it('should preserve all registered tags and ignore unregistered tags during table parsing with extra parser args', () => {
|
|
1006
1064
|
const document = setupTableDoc([
|
|
1007
1065
|
`
|
|
1008
1066
|
{% if-pref conditions=[{platform: 'web'}] %}
|
package/src/parser.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import Node from './ast/node';
|
|
2
2
|
import transforms from './transforms/index';
|
|
3
|
-
import { OPEN } from './utils';
|
|
4
|
-
|
|
5
3
|
import type { AttributeValue, NodeType, ParserArgs, Token } from './types';
|
|
4
|
+
import { OPEN } from './utils';
|
|
6
5
|
|
|
7
6
|
const mappings: Record<string, string> = {
|
|
8
7
|
ordered_list: 'list',
|
|
@@ -18,7 +17,30 @@ function annotate(node: Node, attributes: AttributeValue[]) {
|
|
|
18
17
|
|
|
19
18
|
const { name, value, type } = attribute;
|
|
20
19
|
if (type === 'attribute') {
|
|
21
|
-
if (
|
|
20
|
+
if (['__proto__', 'prototype', 'constructor'].includes(name)) {
|
|
21
|
+
node.errors.push({
|
|
22
|
+
id: 'attribute-name-invalid',
|
|
23
|
+
level: 'critical',
|
|
24
|
+
message: `Invalid attribute name: '${name}'`,
|
|
25
|
+
});
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (
|
|
30
|
+
node.type === 'heading' &&
|
|
31
|
+
name === 'level' &&
|
|
32
|
+
!(Number.isInteger(value) && value >= 1 && value <= 6)
|
|
33
|
+
) {
|
|
34
|
+
node.errors.push({
|
|
35
|
+
id: 'attribute-value-invalid',
|
|
36
|
+
level: 'error',
|
|
37
|
+
message: `Invalid heading level: '${value}'`,
|
|
38
|
+
});
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: Object.hasOwn requires ES2022; Markdoc targets ES2021.
|
|
43
|
+
if (Object.prototype.hasOwnProperty.call(node.attributes, name))
|
|
22
44
|
node.errors.push({
|
|
23
45
|
id: 'duplicate-attribute',
|
|
24
46
|
level: 'warning',
|
|
@@ -61,7 +83,7 @@ function handleAttrs(token: Token, type: string) {
|
|
|
61
83
|
case 'text':
|
|
62
84
|
case 'code':
|
|
63
85
|
case 'comment':
|
|
64
|
-
return { content:
|
|
86
|
+
return { content: token.meta?.variable || token.content };
|
|
65
87
|
case 'fence': {
|
|
66
88
|
// fence tokens always have `info` set (empty string if unused)
|
|
67
89
|
const [language] = token.info!.split(' ', 1);
|
|
@@ -74,7 +96,7 @@ function handleAttrs(token: Token, type: string) {
|
|
|
74
96
|
if (token.attrs) {
|
|
75
97
|
const attrs = Object.fromEntries(token.attrs);
|
|
76
98
|
|
|
77
|
-
let align;
|
|
99
|
+
let align: 'left' | 'center' | 'right' | undefined;
|
|
78
100
|
if (attrs.style) {
|
|
79
101
|
const style = String(attrs.style);
|
|
80
102
|
if (style.includes('left')) {
|
|
@@ -103,7 +125,7 @@ function handleToken(
|
|
|
103
125
|
file?: string,
|
|
104
126
|
handleSlots?: boolean,
|
|
105
127
|
addLocation?: boolean,
|
|
106
|
-
inlineParent?: Node
|
|
128
|
+
inlineParent?: Node,
|
|
107
129
|
) {
|
|
108
130
|
if (token.type === 'frontmatter') {
|
|
109
131
|
nodes[0].attributes.frontmatter = token.content;
|
|
@@ -152,7 +174,7 @@ function handleToken(
|
|
|
152
174
|
typeName as NodeType,
|
|
153
175
|
attrs,
|
|
154
176
|
undefined,
|
|
155
|
-
tag || undefined
|
|
177
|
+
tag || undefined,
|
|
156
178
|
);
|
|
157
179
|
const { position = {} } = token;
|
|
158
180
|
|