@markdoc/markdoc 0.5.5 → 0.5.7

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,213 @@
1
+ import Markdoc from '../../index';
2
+
3
+ function validate(string: string, config = {}) {
4
+ return Markdoc.validate(Markdoc.parse(string), config);
5
+ }
6
+
7
+ describe('table transform validation', function () {
8
+ it('produces an error for non-row content at the row level of a table', function () {
9
+ const input = `{% table %}
10
+ * Heading 1
11
+ * Heading 2
12
+ ---
13
+ * Cell 1
14
+ * Cell 2
15
+ {% if $foo %}
16
+ This is invalid conditional content at the row level of the table.
17
+ {% /if %}
18
+ This is invalid non-conditional content at the row level.
19
+ {% /table %}`;
20
+
21
+ const errors = validate(input);
22
+ const tableSyntaxErrors = errors.filter(
23
+ (e) => e.error.id === 'table-syntax'
24
+ );
25
+
26
+ // One error for the paragraph inside the conditional,
27
+ // one for the bare paragraph at the row level
28
+ expect(tableSyntaxErrors.length).toBe(2);
29
+ for (const err of tableSyntaxErrors) {
30
+ expect(err.error.level).toBe('critical');
31
+ expect(err.error.message).toContain('paragraph');
32
+ expect(err.error.message).toContain('indented');
33
+ }
34
+
35
+ // Errors should point to the actual invalid content, not the table
36
+ const conditionalError = tableSyntaxErrors.find(
37
+ (e) => e.location?.start.line === 7
38
+ );
39
+ const rowLevelError = tableSyntaxErrors.find(
40
+ (e) => e.location?.start.line === 9
41
+ );
42
+ expect(conditionalError).toBeDefined();
43
+ expect(conditionalError?.location?.start.line).toBe(7);
44
+ expect(conditionalError?.location?.end.line).toBe(8);
45
+ expect(conditionalError?.lines).toEqual([7, 8]);
46
+ expect(rowLevelError).toBeDefined();
47
+ expect(rowLevelError?.location?.start.line).toBe(9);
48
+ expect(rowLevelError?.location?.end.line).toBe(10);
49
+ expect(rowLevelError?.lines).toEqual([9, 10]);
50
+ });
51
+
52
+ it('does not produce errors for valid conditional rows', function () {
53
+ const input = `{% table %}
54
+ * Heading 1
55
+ * Heading 2
56
+ ---
57
+ {% if $foo %}
58
+ * Row 1 Cell 1
59
+ * Row 1 Cell 2
60
+ {% /if %}
61
+ ---
62
+ {% if $bar %}
63
+ * Row 2 Cell 1
64
+ * Row 2 Cell 2
65
+ {% else /%}
66
+ * Alt Row 2 Cell 1
67
+ * Alt Row 2 Cell 2
68
+ {% /if %}
69
+ {% /table %}`;
70
+
71
+ const errors = validate(input);
72
+ const tableSyntaxErrors = errors.filter(
73
+ (e) => e.error.id === 'table-syntax'
74
+ );
75
+
76
+ expect(tableSyntaxErrors).toEqual([]);
77
+ });
78
+
79
+ it('does not produce errors for valid conditionals within a cell', function () {
80
+ const input = `{% table %}
81
+ * Heading 1
82
+ * Heading 2
83
+ ---
84
+ * Cell 1
85
+ * Cell 2
86
+ {% if $foo %}
87
+ This is a conditional paragraph inside cell 2.
88
+ {% else /%}
89
+ This is an alternate paragraph inside cell 2.
90
+ {% /if %}
91
+ {% /table %}`;
92
+
93
+ const errors = validate(input);
94
+ const tableSyntaxErrors = errors.filter(
95
+ (e) => e.error.id === 'table-syntax'
96
+ );
97
+
98
+ expect(tableSyntaxErrors).toEqual([]);
99
+ });
100
+
101
+ it('does not produce errors for valid conditional with multiple rows and hr separators', function () {
102
+ const input = `{% table %}
103
+ * Heading 1
104
+ * Heading 2
105
+ ---
106
+ {% if $foo %}
107
+ * Row 1 Cell 1
108
+ * Row 1 Cell 2
109
+ ---
110
+ * Row 2 Cell 1
111
+ * Row 2 Cell 2
112
+ {% /if %}
113
+ {% /table %}`;
114
+
115
+ const errors = validate(input);
116
+ const tableSyntaxErrors = errors.filter(
117
+ (e) => e.error.id === 'table-syntax'
118
+ );
119
+
120
+ expect(tableSyntaxErrors).toEqual([]);
121
+ });
122
+
123
+ it('does not produce errors for comments in a table', function () {
124
+ function validateWithComments(string: string, config = {}) {
125
+ const tokenizer = new Markdoc.Tokenizer({ allowComments: true });
126
+ const tokens = tokenizer.tokenize(string);
127
+ return Markdoc.validate(Markdoc.parse(tokens), config);
128
+ }
129
+ const input = `{% table %}
130
+ * Heading 1
131
+ * Heading 2
132
+ ---
133
+ {% comment %}
134
+ comment row
135
+ {% /comment %}
136
+ * Cell 1
137
+ * Cell 2
138
+ ---
139
+ {% if $foo %}
140
+ {% comment %}
141
+ comment inside conditional
142
+ {% /comment %}
143
+ * Row Cell 1
144
+ * Row Cell 2
145
+ {% /if %}
146
+ {% /table %}`;
147
+
148
+ const errors = validateWithComments(input);
149
+ const tableSyntaxErrors = errors.filter(
150
+ (e) => e.error.id === 'table-syntax'
151
+ );
152
+
153
+ expect(tableSyntaxErrors).toEqual([]);
154
+ });
155
+
156
+ it('produces an error for invalid tags inside a table conditional', function () {
157
+ const input = `{% table %}
158
+ * Heading 1
159
+ * Heading 2
160
+ ---
161
+ {% if $foo %}
162
+ {% callout %}
163
+ This is not a valid row
164
+ {% /callout %}
165
+ {% /if %}
166
+ {% /table %}`;
167
+
168
+ const config = {
169
+ tags: { callout: { render: 'div' } },
170
+ };
171
+
172
+ const errors = validate(input, config);
173
+ const tableSyntaxErrors = errors.filter(
174
+ (e) => e.error.id === 'table-syntax'
175
+ );
176
+
177
+ expect(tableSyntaxErrors.length).toBe(1);
178
+ expect(tableSyntaxErrors[0].error.level).toBe('critical');
179
+ expect(tableSyntaxErrors[0].error.message).toContain('tag callout');
180
+ // Error should point to the callout tag, not the if tag or table
181
+ expect(tableSyntaxErrors[0].location?.start.line).toBe(5);
182
+ expect(tableSyntaxErrors[0].location?.end.line).toBe(6);
183
+ expect(tableSyntaxErrors[0].lines).toEqual([5, 6]);
184
+ });
185
+
186
+ it('produces an error for non-conditional tags at the row level of a table', function () {
187
+ const input = `{% table %}
188
+ * Heading 1
189
+ * Heading 2
190
+ ---
191
+ {% callout %}
192
+ This is not a valid row
193
+ {% /callout %}
194
+ {% /table %}`;
195
+
196
+ const config = {
197
+ tags: { callout: { render: 'div' } },
198
+ };
199
+
200
+ const errors = validate(input, config);
201
+ const tableSyntaxErrors = errors.filter(
202
+ (e) => e.error.id === 'table-syntax'
203
+ );
204
+
205
+ expect(tableSyntaxErrors.length).toBe(1);
206
+ expect(tableSyntaxErrors[0].error.level).toBe('critical');
207
+ expect(tableSyntaxErrors[0].error.message).toContain('tag');
208
+ // Error should point to the callout tag, not the table
209
+ expect(tableSyntaxErrors[0].location?.start.line).toBe(4);
210
+ expect(tableSyntaxErrors[0].location?.end.line).toBe(5);
211
+ expect(tableSyntaxErrors[0].lines).toEqual([4, 5]);
212
+ });
213
+ });
@@ -1,6 +1,6 @@
1
1
  import Ast from '../ast/index';
2
2
 
3
- import type { Node, NodeType } from '../types';
3
+ import type { Node, NodeType, ValidationError } from '../types';
4
4
 
5
5
  function convertToRow(node: Node, cellType: NodeType = 'td') {
6
6
  node.type = 'tr';
@@ -11,6 +11,29 @@ function convertToRow(node: Node, cellType: NodeType = 'td') {
11
11
  return node;
12
12
  }
13
13
 
14
+ function isConditionalTag(node: Node, conditionalTags: string[]) {
15
+ return (
16
+ node.type === 'tag' && !!node.tag && conditionalTags.includes(node.tag)
17
+ );
18
+ }
19
+
20
+ function isComment(node: Node) {
21
+ return (
22
+ node.type === 'comment' || (node.type === 'tag' && node.tag === 'comment')
23
+ );
24
+ }
25
+
26
+ function unexpectedNodeError(node: Node): ValidationError {
27
+ return {
28
+ id: 'table-syntax',
29
+ level: 'critical',
30
+ message: `Found ${node.type}${
31
+ node.tag ? ` ${node.tag}` : ''
32
+ } where a list was expected. Make sure all content inside table cells is indented.`,
33
+ location: node.location,
34
+ };
35
+ }
36
+
14
37
  export default function transform(
15
38
  document: Node,
16
39
  conditionalTags: string[] = ['if']
@@ -34,21 +57,30 @@ export default function transform(
34
57
  // Convert lists to rows with special-case support for conditionals
35
58
  // When a conditional is encountered, convert all of its top-level lists to rows
36
59
  if (row.type === 'list') convertToRow(row);
37
- else if (
38
- row.type === 'tag' &&
39
- row.tag &&
40
- conditionalTags.includes(row.tag)
41
- ) {
60
+ else if (isConditionalTag(row, conditionalTags)) {
42
61
  const children = [];
43
62
 
44
63
  for (const child of row.children) {
45
64
  // Replace children and skip HRs in order to support conditionals with multiple rows
46
65
  if (child.type === 'hr') continue;
47
66
  if (child.type === 'list') convertToRow(child);
67
+ else if (
68
+ isComment(child) ||
69
+ child.tag === 'else' ||
70
+ isConditionalTag(child, conditionalTags)
71
+ ) {
72
+ // Allow structural tags: else, nested conditionals, and comments
73
+ } else {
74
+ row.errors.push(unexpectedNodeError(child));
75
+ continue;
76
+ }
48
77
  children.push(child);
49
78
  }
50
79
 
51
80
  row.children = children;
81
+ } else if (row.type !== 'hr' && !isComment(row)) {
82
+ node.errors.push(unexpectedNodeError(row));
83
+ continue;
52
84
  } else continue;
53
85
  tbody.push(row);
54
86
  }
package/src/utils.test.ts CHANGED
@@ -75,7 +75,7 @@ describe('Templating', function () {
75
75
 
76
76
  it('for an invalid container', function () {
77
77
  const end = findTagEnd('{% foo .bar#baz');
78
- expect(end).toBeUndefined;
78
+ expect(end).toBeNull();
79
79
  });
80
80
 
81
81
  it('in a complex container', function () {
package/src/validator.ts CHANGED
@@ -3,10 +3,12 @@ import Ast from './ast/index';
3
3
  import { isPromise } from './utils';
4
4
 
5
5
  import type {
6
+ Location,
6
7
  Node,
7
8
  Function,
8
9
  Config,
9
10
  SchemaAttribute,
11
+ ValidateError,
10
12
  ValidationError,
11
13
  Value,
12
14
  } from './types';
@@ -303,9 +305,35 @@ export function* walkWithParents(
303
305
  yield* walkWithParents(child, [...parents, node]);
304
306
  }
305
307
 
308
+ function hasValidLocation(
309
+ error: ValidationError
310
+ ): error is ValidationError & { location: Location } {
311
+ const loc = error.location;
312
+ return (
313
+ loc != null &&
314
+ typeof loc.start?.line === 'number' &&
315
+ typeof loc.end?.line === 'number' &&
316
+ (loc.file === undefined || typeof loc.file === 'string')
317
+ );
318
+ }
319
+
320
+ function toValidateError(
321
+ node: { type: string; lines: number[]; location?: Location },
322
+ error: ValidationError
323
+ ): ValidateError {
324
+ if (hasValidLocation(error)) {
325
+ return {
326
+ type: node.type,
327
+ lines: [error.location.start.line, error.location.end.line],
328
+ location: { file: node.location?.file, ...error.location },
329
+ error,
330
+ };
331
+ }
332
+ return { type: node.type, lines: node.lines, location: node.location, error };
333
+ }
334
+
306
335
  export function validateTree(content: Node, config: Config) {
307
336
  const output = [...walkWithParents(content)].map(([node, parents]) => {
308
- const { type, lines, location } = node;
309
337
  const updatedConfig = {
310
338
  ...config,
311
339
  validation: { ...config.validation, parents },
@@ -313,12 +341,10 @@ export function validateTree(content: Node, config: Config) {
313
341
  const errors = validator(node, updatedConfig);
314
342
 
315
343
  if (isPromise(errors)) {
316
- return errors.then((e) =>
317
- e.map((error) => ({ type, lines, location, error }))
318
- );
344
+ return errors.then((e) => e.map((error) => toValidateError(node, error)));
319
345
  }
320
346
 
321
- return errors.map((error) => ({ type, lines, location, error }));
347
+ return errors.map((error) => toValidateError(node, error));
322
348
  });
323
349
 
324
350
  if (output.some(isPromise)) {