@markdoc/markdoc 0.1.3 → 0.1.6

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 (53) hide show
  1. package/README.md +1 -1
  2. package/dist/index.d.ts +48 -30
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +6744 -6329
  5. package/dist/index.js.map +3 -3
  6. package/dist/index.mjs +6827 -6412
  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/tag.d.ts +9 -0
  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/transformer.d.ts +1 -7
  25. package/dist/src/transformer.d.ts.map +1 -1
  26. package/dist/src/transforms/table.d.ts.map +1 -1
  27. package/dist/src/types.d.ts +12 -4
  28. package/dist/src/types.d.ts.map +1 -1
  29. package/dist/src/utils.d.ts +1 -0
  30. package/dist/src/utils.d.ts.map +1 -1
  31. package/dist/src/validator.d.ts.map +1 -1
  32. package/index.ts +47 -29
  33. package/package.json +3 -2
  34. package/src/ast/base.ts +5 -0
  35. package/src/ast/node.test.ts +73 -21
  36. package/src/ast/node.ts +9 -4
  37. package/src/formatter.test.ts +404 -0
  38. package/src/formatter.ts +373 -0
  39. package/src/parser.ts +5 -1
  40. package/src/schema.ts +2 -1
  41. package/src/tag.ts +22 -0
  42. package/src/tags/conditional.ts +10 -3
  43. package/src/tags/partial.ts +20 -3
  44. package/src/tokenizer/index.ts +5 -1
  45. package/src/transformer.ts +15 -17
  46. package/src/transforms/table.ts +2 -2
  47. package/src/types.ts +31 -19
  48. package/src/utils.ts +4 -0
  49. package/src/validator.test.ts +60 -0
  50. package/src/validator.ts +20 -1
  51. package/dist/src/ast/tag.d.ts +0 -9
  52. package/dist/src/ast/tag.d.ts.map +0 -1
  53. package/src/ast/tag.ts +0 -19
@@ -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
@@ -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;
@@ -1,9 +0,0 @@
1
- import type { RenderableTreeNode } from '../types';
2
- export default class Tag {
3
- readonly $$mdtype: "Tag";
4
- name: string;
5
- attributes: Record<string, any>;
6
- children: RenderableTreeNode[];
7
- constructor(name?: string, attributes?: {}, children?: RenderableTreeNode[]);
8
- }
9
- //# sourceMappingURL=tag.d.ts.map
@@ -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"}
package/src/ast/tag.ts DELETED
@@ -1,19 +0,0 @@
1
- import type { RenderableTreeNode } from '../types';
2
-
3
- export default class Tag {
4
- readonly $$mdtype = 'Tag' as const;
5
-
6
- name: string;
7
- attributes: Record<string, any>;
8
- children: RenderableTreeNode[];
9
-
10
- constructor(
11
- name = 'div',
12
- attributes = {},
13
- children: RenderableTreeNode[] = []
14
- ) {
15
- this.name = name;
16
- this.attributes = attributes;
17
- this.children = children;
18
- }
19
- }