@markdoc/markdoc 0.5.5 → 0.5.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.
- package/dist/index.js +25 -1
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +25 -1
- package/dist/index.mjs.map +2 -2
- package/dist/src/transforms/table.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/transforms/table.test.ts +202 -0
- package/src/transforms/table.ts +45 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../../src/transforms/table.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../../src/transforms/table.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAA6B,MAAM,UAAU,CAAC;AAuChE,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,QAAQ,EAAE,IAAI,EACd,eAAe,GAAE,MAAM,EAAW,QAqDnC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,202 @@
|
|
|
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 (on the if tag),
|
|
27
|
+
// one for the bare paragraph (on the table tag since the node is discarded)
|
|
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
|
+
// The conditional error should point to the {% if %} tag, not the table
|
|
36
|
+
const conditionalError = tableSyntaxErrors.find(
|
|
37
|
+
(e) => e.type === 'tag' && e.location?.start.line !== 0
|
|
38
|
+
);
|
|
39
|
+
expect(conditionalError).toBeDefined();
|
|
40
|
+
expect(conditionalError?.location?.start.line).toBeGreaterThan(0);
|
|
41
|
+
|
|
42
|
+
// The row-level error should point to the table tag (paragraph is discarded)
|
|
43
|
+
const rowLevelError = tableSyntaxErrors.find(
|
|
44
|
+
(e) => e.location?.start.line === 0
|
|
45
|
+
);
|
|
46
|
+
expect(rowLevelError).toBeDefined();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('does not produce errors for valid conditional rows', function () {
|
|
50
|
+
const input = `{% table %}
|
|
51
|
+
* Heading 1
|
|
52
|
+
* Heading 2
|
|
53
|
+
---
|
|
54
|
+
{% if $foo %}
|
|
55
|
+
* Row 1 Cell 1
|
|
56
|
+
* Row 1 Cell 2
|
|
57
|
+
{% /if %}
|
|
58
|
+
---
|
|
59
|
+
{% if $bar %}
|
|
60
|
+
* Row 2 Cell 1
|
|
61
|
+
* Row 2 Cell 2
|
|
62
|
+
{% else /%}
|
|
63
|
+
* Alt Row 2 Cell 1
|
|
64
|
+
* Alt Row 2 Cell 2
|
|
65
|
+
{% /if %}
|
|
66
|
+
{% /table %}`;
|
|
67
|
+
|
|
68
|
+
const errors = validate(input);
|
|
69
|
+
const tableSyntaxErrors = errors.filter(
|
|
70
|
+
(e) => e.error.id === 'table-syntax'
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
expect(tableSyntaxErrors).toEqual([]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('does not produce errors for valid conditionals within a cell', function () {
|
|
77
|
+
const input = `{% table %}
|
|
78
|
+
* Heading 1
|
|
79
|
+
* Heading 2
|
|
80
|
+
---
|
|
81
|
+
* Cell 1
|
|
82
|
+
* Cell 2
|
|
83
|
+
{% if $foo %}
|
|
84
|
+
This is a conditional paragraph inside cell 2.
|
|
85
|
+
{% else /%}
|
|
86
|
+
This is an alternate paragraph inside cell 2.
|
|
87
|
+
{% /if %}
|
|
88
|
+
{% /table %}`;
|
|
89
|
+
|
|
90
|
+
const errors = validate(input);
|
|
91
|
+
const tableSyntaxErrors = errors.filter(
|
|
92
|
+
(e) => e.error.id === 'table-syntax'
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(tableSyntaxErrors).toEqual([]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('does not produce errors for valid conditional with multiple rows and hr separators', function () {
|
|
99
|
+
const input = `{% table %}
|
|
100
|
+
* Heading 1
|
|
101
|
+
* Heading 2
|
|
102
|
+
---
|
|
103
|
+
{% if $foo %}
|
|
104
|
+
* Row 1 Cell 1
|
|
105
|
+
* Row 1 Cell 2
|
|
106
|
+
---
|
|
107
|
+
* Row 2 Cell 1
|
|
108
|
+
* Row 2 Cell 2
|
|
109
|
+
{% /if %}
|
|
110
|
+
{% /table %}`;
|
|
111
|
+
|
|
112
|
+
const errors = validate(input);
|
|
113
|
+
const tableSyntaxErrors = errors.filter(
|
|
114
|
+
(e) => e.error.id === 'table-syntax'
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(tableSyntaxErrors).toEqual([]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('does not produce errors for comments in a table', function () {
|
|
121
|
+
function validateWithComments(string: string, config = {}) {
|
|
122
|
+
const tokenizer = new Markdoc.Tokenizer({ allowComments: true });
|
|
123
|
+
const tokens = tokenizer.tokenize(string);
|
|
124
|
+
return Markdoc.validate(Markdoc.parse(tokens), config);
|
|
125
|
+
}
|
|
126
|
+
const input = `{% table %}
|
|
127
|
+
* Heading 1
|
|
128
|
+
* Heading 2
|
|
129
|
+
---
|
|
130
|
+
{% comment %}
|
|
131
|
+
comment row
|
|
132
|
+
{% /comment %}
|
|
133
|
+
* Cell 1
|
|
134
|
+
* Cell 2
|
|
135
|
+
---
|
|
136
|
+
{% if $foo %}
|
|
137
|
+
{% comment %}
|
|
138
|
+
comment inside conditional
|
|
139
|
+
{% /comment %}
|
|
140
|
+
* Row Cell 1
|
|
141
|
+
* Row Cell 2
|
|
142
|
+
{% /if %}
|
|
143
|
+
{% /table %}`;
|
|
144
|
+
|
|
145
|
+
const errors = validateWithComments(input);
|
|
146
|
+
const tableSyntaxErrors = errors.filter(
|
|
147
|
+
(e) => e.error.id === 'table-syntax'
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
expect(tableSyntaxErrors).toEqual([]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('produces an error for invalid tags inside a table conditional', function () {
|
|
154
|
+
const input = `{% table %}
|
|
155
|
+
* Heading 1
|
|
156
|
+
* Heading 2
|
|
157
|
+
---
|
|
158
|
+
{% if $foo %}
|
|
159
|
+
{% callout %}
|
|
160
|
+
This is not a valid row
|
|
161
|
+
{% /callout %}
|
|
162
|
+
{% /if %}
|
|
163
|
+
{% /table %}`;
|
|
164
|
+
|
|
165
|
+
const config = {
|
|
166
|
+
tags: { callout: { render: 'div' } },
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const errors = validate(input, config);
|
|
170
|
+
const tableSyntaxErrors = errors.filter(
|
|
171
|
+
(e) => e.error.id === 'table-syntax'
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
expect(tableSyntaxErrors.length).toBe(1);
|
|
175
|
+
expect(tableSyntaxErrors[0].error.level).toBe('critical');
|
|
176
|
+
expect(tableSyntaxErrors[0].error.message).toContain('tag callout');
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('produces an error for non-conditional tags at the row level of a table', function () {
|
|
180
|
+
const input = `{% table %}
|
|
181
|
+
* Heading 1
|
|
182
|
+
* Heading 2
|
|
183
|
+
---
|
|
184
|
+
{% callout %}
|
|
185
|
+
This is not a valid row
|
|
186
|
+
{% /callout %}
|
|
187
|
+
{% /table %}`;
|
|
188
|
+
|
|
189
|
+
const config = {
|
|
190
|
+
tags: { callout: { render: 'div' } },
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const errors = validate(input, config);
|
|
194
|
+
const tableSyntaxErrors = errors.filter(
|
|
195
|
+
(e) => e.error.id === 'table-syntax'
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
expect(tableSyntaxErrors.length).toBe(1);
|
|
199
|
+
expect(tableSyntaxErrors[0].error.level).toBe('critical');
|
|
200
|
+
expect(tableSyntaxErrors[0].error.message).toContain('tag');
|
|
201
|
+
});
|
|
202
|
+
});
|
package/src/transforms/table.ts
CHANGED
|
@@ -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,34 @@ 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({
|
|
27
|
+
type,
|
|
28
|
+
tag,
|
|
29
|
+
}: {
|
|
30
|
+
type: NodeType;
|
|
31
|
+
tag?: string;
|
|
32
|
+
}): ValidationError {
|
|
33
|
+
return {
|
|
34
|
+
id: 'table-syntax',
|
|
35
|
+
level: 'critical',
|
|
36
|
+
message: `Found ${type}${
|
|
37
|
+
tag ? ` ${tag}` : ''
|
|
38
|
+
} where a list was expected. Make sure all content inside table cells is indented.`,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
14
42
|
export default function transform(
|
|
15
43
|
document: Node,
|
|
16
44
|
conditionalTags: string[] = ['if']
|
|
@@ -34,21 +62,32 @@ export default function transform(
|
|
|
34
62
|
// Convert lists to rows with special-case support for conditionals
|
|
35
63
|
// When a conditional is encountered, convert all of its top-level lists to rows
|
|
36
64
|
if (row.type === 'list') convertToRow(row);
|
|
37
|
-
else if (
|
|
38
|
-
row.type === 'tag' &&
|
|
39
|
-
row.tag &&
|
|
40
|
-
conditionalTags.includes(row.tag)
|
|
41
|
-
) {
|
|
65
|
+
else if (isConditionalTag(row, conditionalTags)) {
|
|
42
66
|
const children = [];
|
|
43
67
|
|
|
44
68
|
for (const child of row.children) {
|
|
45
69
|
// Replace children and skip HRs in order to support conditionals with multiple rows
|
|
46
70
|
if (child.type === 'hr') continue;
|
|
47
71
|
if (child.type === 'list') convertToRow(child);
|
|
72
|
+
else if (
|
|
73
|
+
isComment(child) ||
|
|
74
|
+
child.tag === 'else' ||
|
|
75
|
+
isConditionalTag(child, conditionalTags)
|
|
76
|
+
) {
|
|
77
|
+
// Allow structural tags: else, nested conditionals, and comments
|
|
78
|
+
} else {
|
|
79
|
+
row.errors.push(
|
|
80
|
+
unexpectedNodeError({ type: child.type, tag: child.tag })
|
|
81
|
+
);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
48
84
|
children.push(child);
|
|
49
85
|
}
|
|
50
86
|
|
|
51
87
|
row.children = children;
|
|
88
|
+
} else if (row.type !== 'hr' && !isComment(row)) {
|
|
89
|
+
node.errors.push(unexpectedNodeError({ type: row.type, tag: row.tag }));
|
|
90
|
+
continue;
|
|
52
91
|
} else continue;
|
|
53
92
|
tbody.push(row);
|
|
54
93
|
}
|