@markdoc/markdoc 0.3.0 → 0.3.1

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.
@@ -111,6 +111,29 @@ describe('MarkdownIt Annotations plugin', function () {
111
111
  });
112
112
 
113
113
  describe('multiline', function () {
114
+ const basicExample = [
115
+ {
116
+ type: 'tag_open',
117
+ nesting: 1,
118
+ meta: {
119
+ attributes: [
120
+ { type: 'attribute', name: 'id', value: 'foo' },
121
+ { type: 'class', name: 'bar', value: true },
122
+ { type: 'attribute', name: 'baz', value: 1 },
123
+ ],
124
+ tag: 'test',
125
+ },
126
+ },
127
+ { type: 'paragraph_open', tag: 'p', nesting: 1 },
128
+ {
129
+ type: 'inline',
130
+ nesting: 0,
131
+ children: [{ type: 'text', nesting: 0, content: 'This is a test' }],
132
+ },
133
+ { type: 'paragraph_close', tag: 'p', nesting: -1 },
134
+ { type: 'tag_close', nesting: -1, meta: { tag: 'test' } },
135
+ ];
136
+
114
137
  it('basic', function () {
115
138
  const example = parse(`
116
139
  {% test #foo .bar
@@ -119,29 +142,28 @@ describe('MarkdownIt Annotations plugin', function () {
119
142
  {% /test %}
120
143
  `);
121
144
 
122
- expect(example).toDeepEqualSubset([
123
- {
124
- type: 'tag_open',
125
- nesting: 1,
126
- meta: {
127
- attributes: [
128
- { type: 'attribute', name: 'id', value: 'foo' },
129
- { type: 'class', name: 'bar', value: true },
130
- { type: 'attribute', name: 'baz', value: 1 },
131
- ],
132
- tag: 'test',
133
- },
134
- },
135
- { type: 'paragraph_open', tag: 'p', nesting: 1 },
136
- {
137
- type: 'inline',
138
- nesting: 0,
139
- children: [{ type: 'text', nesting: 0, content: 'This is a test' }],
140
- },
141
- { type: 'paragraph_close', tag: 'p', nesting: -1 },
142
- { type: 'tag_close', nesting: -1, meta: { tag: 'test' } },
143
- ]);
145
+ expect(example).toDeepEqualSubset(basicExample);
146
+ expect(example[0].map).toDeepEqual([0, 2]);
147
+ expect(example[1].map).toDeepEqual([2, 3]);
148
+ expect(example[4].map).toDeepEqual([3, 4]);
149
+ expect(example.length).toEqual(5);
150
+ expect(example[2].children.length).toEqual(1);
151
+ });
152
+
153
+ it('basic with symbols on separate lines', function () {
154
+ const example = parse(`
155
+ {%
156
+ test #foo .bar
157
+ baz=1
158
+ %}
159
+ This is a test
160
+ {% /test %}
161
+ `);
144
162
 
163
+ expect(example).toDeepEqualSubset(basicExample);
164
+ expect(example[0].map).toDeepEqual([0, 4]);
165
+ expect(example[2].map).toDeepEqual([4, 5]);
166
+ expect(example[4].map).toDeepEqual([5, 6]);
145
167
  expect(example.length).toEqual(5);
146
168
  expect(example[2].children.length).toEqual(1);
147
169
  });
@@ -66,7 +66,9 @@ function block(
66
66
 
67
67
  const contentStart = start + OPEN.length;
68
68
  const content = state.src.slice(contentStart, tagEnd).trim();
69
- const lines = content.split('\n').length;
69
+ const lines = state.src
70
+ .slice(start, tagEnd + CLOSE.length)
71
+ .split('\n').length;
70
72
 
71
73
  if (content[0] === '$') return false;
72
74
 
package/src/types.ts CHANGED
@@ -29,6 +29,7 @@ export type ConfigType = Partial<{
29
29
  functions: Record<string, ConfigFunction>;
30
30
  partials: Record<string, any>;
31
31
  validation?: {
32
+ parents?: Node[];
32
33
  validateFunctions?: boolean;
33
34
  };
34
35
  }>;
@@ -42,7 +43,7 @@ export type ConfigFunction = {
42
43
 
43
44
  export interface CustomAttributeTypeInterface {
44
45
  transform?(value: any, config: Config): Scalar;
45
- validate?(value: any, config: Config): ValidationError[];
46
+ validate?(value: any, config: Config, name: string): ValidationError[];
46
47
  }
47
48
 
48
49
  export interface CustomAttributeType {
@@ -116,7 +117,7 @@ export type SchemaAttribute = {
116
117
  default?: any;
117
118
  required?: boolean;
118
119
  matches?: SchemaMatches | ((config: Config) => SchemaMatches);
119
- validate?(value: any, config: Config): ValidationError[];
120
+ validate?(value: any, config: Config, name: string): ValidationError[];
120
121
  errorLevel?: ValidationError['level'];
121
122
  description?: string;
122
123
  };
@@ -544,4 +544,141 @@ bar
544
544
  ]);
545
545
  });
546
546
  });
547
+
548
+ describe('attribute validation key', () => {
549
+ it('simple case', () => {
550
+ const example = `{% foo bar={baz: 3} /%}`;
551
+
552
+ function validator(value, config, name) {
553
+ return value.baz < 5
554
+ ? [
555
+ {
556
+ id: 'invalid-foo-bar',
557
+ level: 'error',
558
+ message: `The value of '${name}.baz' must be less than five`,
559
+ },
560
+ ]
561
+ : [];
562
+ }
563
+
564
+ const config = {
565
+ tags: {
566
+ foo: {
567
+ attributes: {
568
+ bar: {
569
+ type: Object,
570
+ validate: validator,
571
+ },
572
+ blah: {
573
+ type: Object,
574
+ validate: validator,
575
+ },
576
+ },
577
+ },
578
+ },
579
+ };
580
+
581
+ const errs = validate(example, config);
582
+ expect(errs[0].error.message).toEqual(
583
+ "The value of 'bar.baz' must be less than five"
584
+ );
585
+ });
586
+
587
+ it('custom attribute type', () => {
588
+ const example = `{% foo bar={baz: 3} /%}`;
589
+
590
+ class CustomType {
591
+ validate(value, config, name) {
592
+ return value.baz < 5
593
+ ? [
594
+ {
595
+ id: 'invalid-foo-bar',
596
+ level: 'error',
597
+ message: `The value of '${name}.baz' must be less than five`,
598
+ },
599
+ ]
600
+ : [];
601
+ }
602
+ }
603
+
604
+ const config = {
605
+ tags: {
606
+ foo: {
607
+ attributes: {
608
+ bar: {
609
+ type: CustomType,
610
+ },
611
+ blah: {
612
+ type: CustomType,
613
+ },
614
+ },
615
+ },
616
+ },
617
+ };
618
+
619
+ const errs = validate(example, config);
620
+ expect(errs[0].error.message).toEqual(
621
+ "The value of 'bar.baz' must be less than five"
622
+ );
623
+ });
624
+ });
625
+
626
+ describe('parent validation', () => {
627
+ it('for deep nesting', () => {
628
+ const doc = `
629
+ {% foo %}
630
+ {% bar %}
631
+ {% baz %}
632
+ # testing
633
+ {% /baz %}
634
+ {% /bar %}
635
+ {% /foo %}
636
+ `;
637
+
638
+ const doc2 = `
639
+ {% foo %}
640
+ {% bar %}
641
+ {% /bar %}
642
+ {% /foo %}
643
+
644
+ {% bar %}
645
+ {% baz %}
646
+ # testing
647
+ {% /baz %}
648
+ {% /bar %}
649
+ `;
650
+
651
+ const config = {
652
+ tags: {
653
+ foo: {},
654
+ bar: {},
655
+ baz: {},
656
+ },
657
+ nodes: {
658
+ heading: {
659
+ ...Markdoc.nodes.heading,
660
+ validate(node, config) {
661
+ if (config.validation.parents.find((p) => p.tag === 'foo'))
662
+ return [
663
+ {
664
+ id: 'heading-in-foo',
665
+ level: 'error',
666
+ message: "Can't nest a heading in tag 'foo'",
667
+ },
668
+ ];
669
+
670
+ return [];
671
+ },
672
+ },
673
+ },
674
+ };
675
+
676
+ const errors = validate(doc, config);
677
+ expect(errors.length).toEqual(1);
678
+ expect(errors[0].error.id).toEqual('heading-in-foo');
679
+
680
+ const errors2 = validate(doc2, config);
681
+ expect(errors2.length).toEqual(0);
682
+ });
683
+ });
547
684
  });
package/src/validator.ts CHANGED
@@ -24,7 +24,8 @@ type TypeParam = NonNullable<SchemaAttribute['type']>;
24
24
  export function validateType(
25
25
  type: TypeParam,
26
26
  value: Value,
27
- config: Config
27
+ config: Config,
28
+ key: string
28
29
  ): boolean | ValidationError[] {
29
30
  if (!type) return true;
30
31
 
@@ -40,14 +41,14 @@ export function validateType(
40
41
  if (Ast.isAst(value)) return true;
41
42
 
42
43
  if (Array.isArray(type))
43
- return type.some((t) => validateType(t, value, config));
44
+ return type.some((t) => validateType(t, value, config, key));
44
45
 
45
46
  if (typeof type === 'string') type = TypeMappings[type];
46
47
 
47
48
  if (typeof type === 'function') {
48
49
  const instance: any = new type();
49
50
  if (instance.validate) {
50
- return instance.validate(value, config);
51
+ return instance.validate(value, config, key);
51
52
  }
52
53
  }
53
54
 
@@ -94,7 +95,7 @@ function validateFunction(fn: Function, config: Config): ValidationError[] {
94
95
  if (Ast.isAst(value) && !Ast.isFunction(value)) continue;
95
96
 
96
97
  if (param.type) {
97
- const valid = validateType(param.type, value, config);
98
+ const valid = validateType(param.type, value, config, key);
98
99
  if (valid === false) {
99
100
  errors.push({
100
101
  id: 'parameter-type-invalid',
@@ -217,7 +218,7 @@ export default function validator(node: Node, config: Config) {
217
218
  value = value as string;
218
219
 
219
220
  if (type) {
220
- const valid = validateType(type, value, config);
221
+ const valid = validateType(type, value, config, key);
221
222
  if (valid === false) {
222
223
  errors.push({
223
224
  id: 'attribute-type-invalid',
@@ -250,7 +251,7 @@ export default function validator(node: Node, config: Config) {
250
251
  });
251
252
 
252
253
  if (typeof attrib.validate === 'function') {
253
- const attribErrors = attrib.validate(value, config);
254
+ const attribErrors = attrib.validate(value, config, key);
254
255
  if (Array.isArray(attribErrors)) errors.push(...attribErrors);
255
256
  }
256
257
  }
@@ -292,3 +293,37 @@ export default function validator(node: Node, config: Config) {
292
293
 
293
294
  return errors;
294
295
  }
296
+
297
+ export function* walkWithParents(
298
+ node: Node,
299
+ parents: Node[] = []
300
+ ): Generator<[Node, Node[]]> {
301
+ yield [node, parents];
302
+ for (const child of [...Object.values(node.slots), ...node.children])
303
+ yield* walkWithParents(child, [...parents, node]);
304
+ }
305
+
306
+ export function validateTree(content: Node, config: Config) {
307
+ const output = [...walkWithParents(content)].map(([node, parents]) => {
308
+ const { type, lines, location } = node;
309
+ const updatedConfig = {
310
+ validation: { ...config.validation, parents },
311
+ ...config,
312
+ };
313
+ const errors = validator(node, updatedConfig);
314
+
315
+ if (isPromise(errors)) {
316
+ return errors.then((e) =>
317
+ e.map((error) => ({ type, lines, location, error }))
318
+ );
319
+ }
320
+
321
+ return errors.map((error) => ({ type, lines, location, error }));
322
+ });
323
+
324
+ if (output.some(isPromise)) {
325
+ return Promise.all(output).then((o) => o.flat());
326
+ }
327
+
328
+ return output.flat();
329
+ }