@markdoc/markdoc 0.1.2 → 0.1.5

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 (57) hide show
  1. package/README.md +5 -2
  2. package/dist/index.d.ts +48 -30
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +7040 -6593
  5. package/dist/index.js.map +3 -3
  6. package/dist/index.mjs +6904 -6457
  7. package/dist/index.mjs.map +3 -3
  8. package/dist/src/ast/base.d.ts +2 -1
  9. package/dist/src/ast/base.d.ts.map +1 -1
  10. package/dist/src/ast/index.d.ts +1 -0
  11. package/dist/src/ast/index.d.ts.map +1 -1
  12. package/dist/src/ast/node.d.ts +5 -4
  13. package/dist/src/ast/node.d.ts.map +1 -1
  14. package/dist/src/formatter.d.ts +9 -0
  15. package/dist/src/formatter.d.ts.map +1 -0
  16. package/dist/src/parser.d.ts.map +1 -1
  17. package/dist/src/schema.d.ts +1 -0
  18. package/dist/src/schema.d.ts.map +1 -1
  19. package/dist/src/{ast/tag.d.ts → tag.d.ts} +1 -1
  20. package/dist/src/tag.d.ts.map +1 -0
  21. package/dist/src/tags/conditional.d.ts.map +1 -1
  22. package/dist/src/tags/partial.d.ts.map +1 -1
  23. package/dist/src/tokenizer/index.d.ts.map +1 -1
  24. package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
  25. package/dist/src/transformer.d.ts +1 -7
  26. package/dist/src/transformer.d.ts.map +1 -1
  27. package/dist/src/transforms/table.d.ts.map +1 -1
  28. package/dist/src/types.d.ts +12 -4
  29. package/dist/src/types.d.ts.map +1 -1
  30. package/dist/src/utils.d.ts +2 -1
  31. package/dist/src/utils.d.ts.map +1 -1
  32. package/dist/src/validator.d.ts.map +1 -1
  33. package/global.d.ts +7 -0
  34. package/index.ts +47 -29
  35. package/package.json +7 -4
  36. package/src/ast/base.ts +5 -0
  37. package/src/ast/node.test.ts +73 -21
  38. package/src/ast/node.ts +9 -4
  39. package/src/formatter.test.ts +403 -0
  40. package/src/formatter.ts +377 -0
  41. package/src/parser.test.ts +42 -0
  42. package/src/parser.ts +33 -3
  43. package/src/schema.ts +3 -1
  44. package/src/{ast/tag.ts → tag.ts} +1 -1
  45. package/src/tags/conditional.ts +10 -3
  46. package/src/tags/partial.ts +20 -3
  47. package/src/tokenizer/index.ts +5 -1
  48. package/src/tokenizer/plugins/annotations.test.ts +60 -0
  49. package/src/tokenizer/plugins/annotations.ts +4 -0
  50. package/src/transformer.ts +15 -17
  51. package/src/transforms/table.ts +3 -3
  52. package/src/types.ts +31 -19
  53. package/src/utils.test.ts +15 -2
  54. package/src/utils.ts +14 -1
  55. package/src/validator.test.ts +70 -10
  56. package/src/validator.ts +31 -12
  57. package/dist/src/ast/tag.d.ts.map +0 -1
@@ -1,13 +1,7 @@
1
- import Tag from './ast/tag';
1
+ import Tag from './tag';
2
2
  import { Class } from './schema-types/class';
3
- import type {
4
- Config,
5
- Node,
6
- NodeType,
7
- Schema,
8
- RenderableTreeNode,
9
- RenderableTreeNodes,
10
- } from './types';
3
+ import { isPromise } from './utils';
4
+ import type { Config, Node, NodeType, Schema, Transformer } from './types';
11
5
 
12
6
  type AttributesSchema = Schema['attributes'];
13
7
 
@@ -16,13 +10,6 @@ export const globalAttributes: AttributesSchema = {
16
10
  id: { type: String, render: true },
17
11
  };
18
12
 
19
- export interface Transformer {
20
- findSchema(node: Node, config: Config): Schema | undefined;
21
- node(node: Node, config: Config): RenderableTreeNodes;
22
- attributes(node: Node, config: Config): Record<string, any>;
23
- children(node: Node, config: Config): RenderableTreeNode[];
24
- }
25
-
26
13
  export default {
27
14
  findSchema(node: Node, { nodes = {}, tags = {} }: Config = {}) {
28
15
  return node.tag ? tags[node.tag] : nodes[node.type as NodeType];
@@ -55,7 +42,11 @@ export default {
55
42
  },
56
43
 
57
44
  children(node: Node, config: Config = {}) {
58
- return node.children.flatMap((child) => this.node(child, config));
45
+ const children = node.children.flatMap((child) => this.node(child, config));
46
+ if (children.some(isPromise)) {
47
+ return Promise.all(children);
48
+ }
49
+ return children;
59
50
  },
60
51
 
61
52
  node(node: Node, config: Config = {}) {
@@ -67,6 +58,13 @@ export default {
67
58
  if (!schema || !schema.render) return children;
68
59
 
69
60
  const attributes = this.attributes(node, config);
61
+
62
+ if (isPromise(attributes) || isPromise(children)) {
63
+ return Promise.all([attributes, children]).then(
64
+ (values) => new Tag(schema.render, ...values)
65
+ );
66
+ }
67
+
70
68
  return new Tag(schema.render, attributes, children);
71
69
  },
72
70
  } as Transformer;
@@ -1,8 +1,8 @@
1
1
  import Ast from '../ast/index';
2
2
 
3
- import type { Node } from '../types';
3
+ import type { Node, NodeType } from '../types';
4
4
 
5
- function convertToRow(node: Node, cellType = 'td') {
5
+ function convertToRow(node: Node, cellType: NodeType = 'td') {
6
6
  node.type = 'tr';
7
7
  node.attributes = {};
8
8
 
@@ -31,7 +31,7 @@ export default function transform(document: Node) {
31
31
  // Convert lists to rows with special-case support for conditionals
32
32
  // When a conditional is encountered, convert all of its top-level lists to rows
33
33
  if (row.type === 'list') convertToRow(row);
34
- else if (row.type === 'tag' && row.tag == 'if') {
34
+ else if (row.type === 'tag' && row.tag === 'if') {
35
35
  const children = [];
36
36
 
37
37
  for (const child of row.children) {
package/src/types.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import type Func from './ast/function';
2
2
  import type Node from './ast/node';
3
- import type Tag from './ast/tag';
3
+ import type Variable from './ast/variable';
4
+ import type Tag from './tag';
4
5
 
5
- export type { Node, Tag };
6
+ export type { Node, Tag, Variable };
6
7
  export declare type Function = Func;
7
8
 
9
+ export type MaybePromise<T> = T | Promise<T>;
10
+
8
11
  export interface AstType {
9
12
  readonly $$mdtype: 'Function' | 'Node' | 'Variable';
10
13
  resolve(config: Config): any;
@@ -58,31 +61,33 @@ export type LocationEdge = {
58
61
  };
59
62
 
60
63
  export type NodeType =
64
+ | 'blockquote'
65
+ | 'code'
61
66
  | 'document'
67
+ | 'em'
68
+ | 'error'
69
+ | 'fence'
70
+ | 'hardbreak'
62
71
  | 'heading'
63
- | 'paragraph'
64
- | 'blockquote'
65
72
  | 'hr'
66
73
  | 'image'
67
- | 'fence'
68
- | 'tag'
69
- | 'list'
74
+ | 'inline'
70
75
  | 'item'
76
+ | 'link'
77
+ | 'list'
78
+ | 'node'
79
+ | 'paragraph'
80
+ | 's'
81
+ | 'softbreak'
82
+ | 'strong'
71
83
  | 'table'
72
- | 'thead'
84
+ | 'tag'
73
85
  | 'tbody'
74
- | 'tr'
75
86
  | 'td'
76
- | 'th'
77
- | 'inline'
78
- | 'strong'
79
- | 'em'
80
- | 's'
81
- | 'link'
82
- | 'code'
83
87
  | 'text'
84
- | 'hardbreak'
85
- | 'softbreak';
88
+ | 'th'
89
+ | 'thead'
90
+ | 'tr';
86
91
 
87
92
  export type Primitive = null | boolean | number | string;
88
93
 
@@ -96,7 +101,7 @@ export type Schema<C extends Config = Config, R = string> = {
96
101
  children?: string[];
97
102
  attributes?: Record<string, SchemaAttribute>;
98
103
  selfClosing?: boolean;
99
- transform?(node: Node, config: C): RenderableTreeNodes;
104
+ transform?(node: Node, config: C): MaybePromise<RenderableTreeNodes>;
100
105
  validate?(node: Node, config: C): ValidationError[];
101
106
  };
102
107
 
@@ -111,6 +116,13 @@ export type SchemaAttribute = {
111
116
 
112
117
  export type SchemaMatches = RegExp | string[] | null;
113
118
 
119
+ export interface Transformer {
120
+ findSchema(node: Node, config: Config): Schema | undefined;
121
+ node(node: Node, config: Config): MaybePromise<RenderableTreeNodes>;
122
+ attributes(node: Node, config: Config): Record<string, any>;
123
+ children(node: Node, config: Config): RenderableTreeNode[];
124
+ }
125
+
114
126
  export type ValidationError = {
115
127
  id: string;
116
128
  level: 'debug' | 'info' | 'warning' | 'error' | 'critical';
package/src/utils.test.ts CHANGED
@@ -165,9 +165,22 @@ describe('Templating', function () {
165
165
  example=1 another="test}"}
166
166
  `;
167
167
 
168
- const end = findTagEnd(example, 0, '{', '}');
169
- expect(end).toEqual(undefined);
168
+ const end = findTagEnd(example, 0);
169
+ expect(end).toEqual(null);
170
170
  });
171
171
  });
172
+ it("shouldn't hang when {% is included in code block", () => {
173
+ const example = '```\n{%a %b %c}\n```';
174
+
175
+ const output = parseTags(example);
176
+ expect(output).toEqual([
177
+ {
178
+ type: 'text',
179
+ start: 0,
180
+ end: 17,
181
+ content: '```\n{%a %b %c}\n```',
182
+ },
183
+ ]);
184
+ });
172
185
  });
173
186
  });
package/src/utils.ts CHANGED
@@ -13,6 +13,10 @@ enum STATES {
13
13
  export const OPEN = '{%';
14
14
  export const CLOSE = '%}';
15
15
 
16
+ export function isPromise(a: any): a is Promise<any> {
17
+ return a && typeof a === 'object' && typeof a.then === 'function';
18
+ }
19
+
16
20
  export function findTagEnd(content: string, start = 0) {
17
21
  let state = STATES.normal;
18
22
  for (let pos = start; pos < content.length; pos++) {
@@ -37,6 +41,8 @@ export function findTagEnd(content: string, start = 0) {
37
41
  else if (content.startsWith(CLOSE, pos)) return pos;
38
42
  }
39
43
  }
44
+
45
+ return null;
40
46
  }
41
47
 
42
48
  function parseTag(content: string, line: number, contentStart: number) {
@@ -70,7 +76,14 @@ export function parseTags(content: string, firstLine = 0): Token[] {
70
76
 
71
77
  if (!content.startsWith(OPEN, pos)) continue;
72
78
 
73
- const end = findTagEnd(content, pos) || 0;
79
+ const end = findTagEnd(content, pos);
80
+
81
+ if (end == null) {
82
+ // If we cannot find the closing tag, we skip over it
83
+ pos = pos + OPEN.length;
84
+ continue;
85
+ }
86
+
74
87
  const text = content.slice(pos, end + CLOSE.length);
75
88
  const inner = content.slice(pos + OPEN.length, end);
76
89
  const lineStart = content.lastIndexOf('\n', pos);
@@ -24,7 +24,7 @@ describe('validate', function () {
24
24
  {
25
25
  error: {
26
26
  id: 'function-undefined',
27
- message: "Undefined function 'baz'",
27
+ message: "Undefined function: 'baz'",
28
28
  },
29
29
  },
30
30
  ]);
@@ -79,7 +79,7 @@ describe('validate', function () {
79
79
  {
80
80
  error: {
81
81
  id: 'attribute-type-invalid',
82
- message: "Attribute 'baz' must be type 'Boolean'",
82
+ message: "Attribute 'baz' must be type of 'Boolean'",
83
83
  },
84
84
  },
85
85
  ]);
@@ -94,13 +94,13 @@ describe('validate', function () {
94
94
  {
95
95
  error: {
96
96
  id: 'parameter-type-invalid',
97
- message: "Parameter '0' of 'nested' must be type 'String'",
97
+ message: "Parameter '0' of 'nested' must be type of 'String'",
98
98
  },
99
99
  },
100
100
  {
101
101
  error: {
102
102
  id: 'parameter-type-invalid',
103
- message: "Parameter '1' of 'nested' must be type 'Number'",
103
+ message: "Parameter '1' of 'nested' must be type of 'Number'",
104
104
  },
105
105
  },
106
106
  ]);
@@ -173,7 +173,7 @@ describe('validate', function () {
173
173
  {
174
174
  error: {
175
175
  id: 'parameter-missing-required',
176
- message: "Missing required parameter 'req'",
176
+ message: "Missing required parameter: 'req'",
177
177
  },
178
178
  },
179
179
  ]);
@@ -199,7 +199,7 @@ describe('validate', function () {
199
199
  type: 'tag',
200
200
  error: {
201
201
  id: 'parameter-undefined',
202
- message: "Invalid parameter 'foo'",
202
+ message: "Invalid parameter: 'foo'",
203
203
  },
204
204
  },
205
205
  ]);
@@ -211,7 +211,7 @@ describe('validate', function () {
211
211
  type: 'tag',
212
212
  error: {
213
213
  id: 'parameter-undefined',
214
- message: "Invalid parameter '0'",
214
+ message: "Invalid parameter: '0'",
215
215
  },
216
216
  },
217
217
  ]);
@@ -223,7 +223,7 @@ describe('validate', function () {
223
223
  type: 'tag',
224
224
  error: {
225
225
  id: 'parameter-undefined',
226
- message: "Invalid parameter '0'",
226
+ message: "Invalid parameter: '0'",
227
227
  },
228
228
  },
229
229
  ]);
@@ -242,7 +242,7 @@ describe('validate', function () {
242
242
  const error: ValidationError = {
243
243
  id: 'attribute-type-invalid',
244
244
  level: 'error',
245
- message: "Attribute 'href' must be type 'Link'",
245
+ message: "Attribute 'href' must be type of 'Link'",
246
246
  };
247
247
 
248
248
  return [error];
@@ -270,7 +270,7 @@ describe('validate', function () {
270
270
  error: {
271
271
  id: 'attribute-type-invalid',
272
272
  level: 'error',
273
- message: "Attribute 'href' must be type 'Link'",
273
+ message: "Attribute 'href' must be type of 'Link'",
274
274
  },
275
275
  },
276
276
  ]);
@@ -295,4 +295,64 @@ describe('validate', function () {
295
295
  expect(output).toEqual([]);
296
296
  });
297
297
  });
298
+
299
+ describe('variable validation', () => {
300
+ it('should only validate if the variables config is passed', () => {
301
+ const example = `{% $valid.variable %}`;
302
+ const output = validate(example, {});
303
+
304
+ expect(output).toEqual([]);
305
+ });
306
+
307
+ it('should warn against missing variables', () => {
308
+ const example = `{% $undefinedVariable %}`;
309
+ const output = validate(example, { variables: {} });
310
+
311
+ expect(output).toDeepEqualSubset([
312
+ {
313
+ type: 'text',
314
+ error: {
315
+ id: 'variable-undefined',
316
+ level: 'error',
317
+ message: "Undefined variable: 'undefinedVariable'",
318
+ },
319
+ },
320
+ ]);
321
+ });
322
+
323
+ it('should not warn if variable exists', () => {
324
+ const example = `{% $valid.variable %}`;
325
+ const output = validate(example, {
326
+ variables: { valid: { variable: false } },
327
+ });
328
+
329
+ expect(output).toEqual([]);
330
+ });
331
+ });
332
+
333
+ it('should not error for missing support for code_block', () => {
334
+ const example = ` # https://spec.commonmark.org/0.30/#indented-code-block
335
+ 4-space indented code`;
336
+ const output = validate(example, {});
337
+
338
+ expect(output).toEqual([]);
339
+ });
340
+
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, {});
345
+
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`",
354
+ },
355
+ },
356
+ ]);
357
+ });
298
358
  });
package/src/validator.ts CHANGED
@@ -70,7 +70,7 @@ function validateFunction(fn: Function, config: Config): ValidationError[] {
70
70
  {
71
71
  id: 'function-undefined',
72
72
  level: 'critical',
73
- message: `Undefined function '${fn.name}'`,
73
+ message: `Undefined function: '${fn.name}'`,
74
74
  },
75
75
  ];
76
76
 
@@ -84,7 +84,7 @@ function validateFunction(fn: Function, config: Config): ValidationError[] {
84
84
  errors.push({
85
85
  id: 'parameter-undefined',
86
86
  level: 'error',
87
- message: `Invalid parameter '${key}'`,
87
+ message: `Invalid parameter: '${key}'`,
88
88
  });
89
89
 
90
90
  continue;
@@ -100,7 +100,7 @@ function validateFunction(fn: Function, config: Config): ValidationError[] {
100
100
  level: 'error',
101
101
  message: `Parameter '${key}' of '${
102
102
  fn.name
103
- }' must be type '${typeToString(param.type)}'`,
103
+ }' must be type of '${typeToString(param.type)}'`,
104
104
  });
105
105
  } else if (Array.isArray(valid)) {
106
106
  errors.push(...valid);
@@ -114,7 +114,7 @@ function validateFunction(fn: Function, config: Config): ValidationError[] {
114
114
  errors.push({
115
115
  id: 'parameter-missing-required',
116
116
  level: 'error',
117
- message: `Missing required parameter '${key}'`,
117
+ message: `Missing required parameter: '${key}'`,
118
118
  });
119
119
 
120
120
  return errors;
@@ -129,8 +129,8 @@ export default function validate(node: Node, config: Config) {
129
129
  id: node.tag ? 'tag-undefined' : 'node-undefined',
130
130
  level: 'critical',
131
131
  message: node.tag
132
- ? `Undefined tag '${node.tag}'`
133
- : `Undefined node '${node.type}'`,
132
+ ? `Undefined tag: '${node.tag}'`
133
+ : `Undefined node: '${node.type}'`,
134
134
  });
135
135
 
136
136
  return errors;
@@ -140,7 +140,7 @@ export default function validate(node: Node, config: Config) {
140
140
  errors.push({
141
141
  id: 'tag-selfclosing-has-children',
142
142
  level: 'critical',
143
- message: `Tag '${node.tag}' should be self-closing`,
143
+ message: `'${node.tag}' tag should be self-closing`,
144
144
  });
145
145
 
146
146
  const attributes = {
@@ -157,7 +157,7 @@ export default function validate(node: Node, config: Config) {
157
157
  errors.push({
158
158
  id: 'attribute-undefined',
159
159
  level: 'error',
160
- message: `Invalid attribute '${key}'`,
160
+ message: `Invalid attribute: '${key}'`,
161
161
  });
162
162
 
163
163
  continue;
@@ -168,7 +168,26 @@ export default function validate(node: Node, config: Config) {
168
168
  if (Ast.isAst(value)) {
169
169
  if (Ast.isFunction(value) && config.validation?.validateFunctions)
170
170
  errors.push(...validateFunction(value, config));
171
- else continue;
171
+ else if (Ast.isVariable(value) && config.variables) {
172
+ let missing = false;
173
+ let variables = config.variables;
174
+
175
+ for (const key of value.path) {
176
+ if (!Object.prototype.hasOwnProperty.call(variables, key)) {
177
+ missing = true;
178
+ break;
179
+ }
180
+ variables = variables[key];
181
+ }
182
+
183
+ if (missing) {
184
+ errors.push({
185
+ id: 'variable-undefined',
186
+ level: 'error',
187
+ message: `Undefined variable: '${value.path.join('.')}'`,
188
+ });
189
+ }
190
+ } else continue;
172
191
  }
173
192
 
174
193
  value = value as string;
@@ -176,7 +195,7 @@ export default function validate(node: Node, config: Config) {
176
195
  errors.push({
177
196
  id: 'attribute-value-invalid',
178
197
  level: 'error',
179
- message: 'The ID attribute must not start with a number',
198
+ message: 'The id attribute must not start with a number',
180
199
  });
181
200
 
182
201
  if (type) {
@@ -185,7 +204,7 @@ export default function validate(node: Node, config: Config) {
185
204
  errors.push({
186
205
  id: 'attribute-type-invalid',
187
206
  level: errorLevel || 'error',
188
- message: `Attribute '${key}' must be type '${typeToString(type)}'`,
207
+ message: `Attribute '${key}' must be type of '${typeToString(type)}'`,
189
208
  });
190
209
  }
191
210
  if (Array.isArray(valid)) {
@@ -217,7 +236,7 @@ export default function validate(node: Node, config: Config) {
217
236
  errors.push({
218
237
  id: 'attribute-missing-required',
219
238
  level: 'error',
220
- message: `Missing required attribute '${key}'`,
239
+ message: `Missing required attribute: '${key}'`,
221
240
  });
222
241
 
223
242
  for (const { type } of node.children) {
@@ -1 +0,0 @@
1
- {"version":3,"file":"tag.d.ts","sourceRoot":"","sources":["../../../src/ast/tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,QAAQ,CAAC,QAAQ,QAAkB;IAEnC,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,QAAQ,EAAE,kBAAkB,EAAE,CAAC;gBAG7B,IAAI,SAAQ,EACZ,UAAU,KAAK,EACf,QAAQ,GAAE,kBAAkB,EAAO;CAMtC"}