@markdoc/markdoc 0.1.6 → 0.1.8

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.
@@ -0,0 +1,51 @@
1
+ import type MarkdownIt from 'markdown-it/lib';
2
+ import type StateBlock from 'markdown-it/lib/rules_block/state_block';
3
+ import type StateInline from 'markdown-it/lib/rules_inline/state_inline';
4
+
5
+ const OPEN = '<!--';
6
+ const CLOSE = '-->';
7
+
8
+ function block(
9
+ state: StateBlock,
10
+ startLine: number,
11
+ endLine: number,
12
+ silent: boolean
13
+ ): boolean {
14
+ const start = state.bMarks[startLine] + state.tShift[startLine];
15
+ if (!state.src.startsWith(OPEN, start)) return false;
16
+
17
+ const close = state.src.indexOf(CLOSE, start);
18
+
19
+ if (!close) return false;
20
+ if (silent) return true;
21
+
22
+ const content = state.src.slice(start + OPEN.length, close);
23
+ const lines = content.split('\n').length;
24
+ const token = state.push('comment', '', 0);
25
+ token.content = content.trim();
26
+ token.map = [startLine, startLine + lines];
27
+ state.line += lines;
28
+
29
+ return true;
30
+ }
31
+
32
+ function inline(state: StateInline, silent: boolean): boolean {
33
+ if (!state.src.startsWith(OPEN, state.pos)) return false;
34
+
35
+ const close = state.src.indexOf(CLOSE, state.pos);
36
+
37
+ if (!close) return false;
38
+ if (silent) return true;
39
+
40
+ const content = state.src.slice(state.pos + OPEN.length, close);
41
+ const token = state.push('comment', '', 0);
42
+ token.content = content.trim();
43
+ state.pos = close + CLOSE.length;
44
+
45
+ return true;
46
+ }
47
+
48
+ export default function plugin(md: MarkdownIt) {
49
+ md.block.ruler.before('table', 'comment', block, { alt: ['paragraph'] });
50
+ md.inline.ruler.push('comment', inline);
51
+ }
package/src/types.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import type Func from './ast/function';
2
2
  import type Node from './ast/node';
3
- import type Variable from './ast/variable';
3
+ import type Var from './ast/variable';
4
4
  import type Tag from './tag';
5
5
 
6
- export type { Node, Tag, Variable };
6
+ export type { Node, Tag };
7
7
  export declare type Function = Func;
8
+ export declare type Variable = Var;
8
9
 
9
10
  export type MaybePromise<T> = T | Promise<T>;
10
11
 
@@ -102,7 +103,7 @@ export type Schema<C extends Config = Config, R = string> = {
102
103
  attributes?: Record<string, SchemaAttribute>;
103
104
  selfClosing?: boolean;
104
105
  transform?(node: Node, config: C): MaybePromise<RenderableTreeNodes>;
105
- validate?(node: Node, config: C): ValidationError[];
106
+ validate?(node: Node, config: C): MaybePromise<ValidationError[]>;
106
107
  };
107
108
 
108
109
  export type SchemaAttribute = {
@@ -1,4 +1,4 @@
1
- import Markdoc from '../index';
1
+ import Markdoc, { nodes } from '../index';
2
2
  import type { ValidationError } from './types';
3
3
 
4
4
  function validate(string, config) {
@@ -232,6 +232,52 @@ describe('validate', function () {
232
232
  });
233
233
  });
234
234
 
235
+ describe('attribute validation', () => {
236
+ it('should return error on failure to match array', () => {
237
+ const example = '{% foo jawn="cat" /%}';
238
+ const schema = {
239
+ tags: {
240
+ foo: {
241
+ attributes: {
242
+ jawn: {
243
+ type: String,
244
+ matches: ['bar', 'baz', 'bat'],
245
+ },
246
+ },
247
+ },
248
+ },
249
+ };
250
+
251
+ expect(validate(example, schema)).toDeepEqualSubset([
252
+ {
253
+ type: 'tag',
254
+ error: {
255
+ id: 'attribute-value-invalid',
256
+ message: `Attribute 'jawn' must match one of ["bar","baz","bat"]. Got 'cat' instead.`,
257
+ },
258
+ },
259
+ ]);
260
+ });
261
+
262
+ // https://github.com/markdoc/markdoc/issues/122
263
+ it('should validate partial file attributes', () => {
264
+ const example = `{% partial file="non-existent.md" /%}`;
265
+ const output = validate(example, {});
266
+
267
+ expect(output).toDeepEqualSubset([
268
+ {
269
+ type: 'tag',
270
+ error: {
271
+ id: 'attribute-value-invalid',
272
+ level: 'error',
273
+ message:
274
+ "Partial `non-existent.md` not found. The 'file' attribute must be set in `config.partials`",
275
+ },
276
+ },
277
+ ]);
278
+ });
279
+ });
280
+
235
281
  describe('custom type registration example', () => {
236
282
  class Link {
237
283
  validate(value) {
@@ -338,21 +384,32 @@ describe('validate', function () {
338
384
  expect(output).toEqual([]);
339
385
  });
340
386
 
341
- // https://github.com/markdoc/markdoc/issues/122
342
- it('should validate partial file attributes', () => {
343
- const example = `{% partial file="non-existent.md" /%}`;
344
- const output = validate(example, {});
387
+ describe('async support', () => {
388
+ it('should allow async validators', async () => {
389
+ const doc = `![img](/src)`;
390
+
391
+ const config = {
392
+ nodes: {
393
+ image: {
394
+ ...nodes.image,
395
+ async validate() {
396
+ const message = await new Promise((res) => res('Error!'));
397
+ return [{ message }];
398
+ },
399
+ },
400
+ },
401
+ };
345
402
 
346
- expect(output).toDeepEqualSubset([
347
- {
348
- type: 'tag',
349
- error: {
350
- id: 'attribute-value-invalid',
351
- level: 'error',
352
- message:
353
- "Partial `non-existent.md` not found. The 'file' attribute must be set in `config.partials`",
403
+ const errors = await validate(doc, config);
404
+
405
+ expect(errors).toDeepEqualSubset([
406
+ {
407
+ type: 'image',
408
+ lines: [0, 1],
409
+ location: {},
410
+ error: { message: 'Error!' },
354
411
  },
355
- },
356
- ]);
412
+ ]);
413
+ });
357
414
  });
358
415
  });
package/src/validator.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { globalAttributes } from './transformer';
2
2
  import Ast from './ast/index';
3
+ import { isPromise } from './utils';
3
4
 
4
5
  import type {
5
6
  Node,
@@ -120,7 +121,7 @@ function validateFunction(fn: Function, config: Config): ValidationError[] {
120
121
  return errors;
121
122
  }
122
123
 
123
- export default function validate(node: Node, config: Config) {
124
+ export default function validator(node: Node, config: Config) {
124
125
  const schema = node.findSchema(config);
125
126
  const errors: ValidationError[] = [...(node.errors || [])];
126
127
 
@@ -148,8 +149,6 @@ export default function validate(node: Node, config: Config) {
148
149
  ...schema.attributes,
149
150
  };
150
151
 
151
- if (schema.validate) errors.push(...schema.validate(node, config));
152
-
153
152
  for (let [key, value] of Object.entries(node.attributes)) {
154
153
  const attrib = attributes[key];
155
154
 
@@ -220,14 +219,14 @@ export default function validate(node: Node, config: Config) {
220
219
  level: errorLevel || 'error',
221
220
  message: `Attribute '${key}' must match one of ${JSON.stringify(
222
221
  matches
223
- )}`,
222
+ )}. Got '${value}' instead.`,
224
223
  });
225
224
 
226
225
  if (matches instanceof RegExp && !matches.test(value))
227
226
  errors.push({
228
227
  id: 'attribute-value-invalid',
229
228
  level: errorLevel || 'error',
230
- message: `Attribute '${key}' must match ${matches}`,
229
+ message: `Attribute '${key}' must match ${matches}. Got '${value}' instead.`,
231
230
  });
232
231
  }
233
232
 
@@ -248,5 +247,14 @@ export default function validate(node: Node, config: Config) {
248
247
  });
249
248
  }
250
249
 
250
+ if (schema.validate) {
251
+ const schemaErrors = schema.validate(node, config);
252
+ if (isPromise(schemaErrors)) {
253
+ return schemaErrors.then((e) => errors.concat(e));
254
+ }
255
+
256
+ errors.push(...schemaErrors);
257
+ }
258
+
251
259
  return errors;
252
260
  }