@markdoc/markdoc 0.1.1 → 0.1.4
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/README.md +7 -4
- package/dist/index.d.ts +38 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +108 -36
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +108 -36
- package/dist/index.mjs.map +2 -2
- package/dist/src/ast/node.d.ts +2 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/partial.d.ts.map +1 -1
- package/dist/src/tokenizer/index.d.ts.map +1 -1
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
- package/dist/src/transformer.d.ts +1 -7
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/types.d.ts +10 -5
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils.d.ts +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/global.d.ts +7 -0
- package/index.ts +37 -19
- package/package.json +6 -4
- package/src/ast/node.test.ts +52 -0
- package/src/ast/node.ts +3 -0
- package/src/parser.test.ts +42 -0
- package/src/parser.ts +33 -3
- package/src/schema.ts +1 -0
- package/src/tags/partial.ts +20 -3
- package/src/tokenizer/index.ts +5 -1
- package/src/tokenizer/plugins/annotations.test.ts +60 -0
- package/src/tokenizer/plugins/annotations.ts +4 -0
- package/src/transformer.ts +17 -16
- package/src/transforms/table.ts +1 -1
- package/src/types.ts +11 -5
- package/src/utils.test.ts +15 -2
- package/src/utils.ts +10 -1
- package/src/validator.test.ts +36 -10
- package/src/validator.ts +11 -11
package/src/transformer.ts
CHANGED
|
@@ -1,28 +1,18 @@
|
|
|
1
1
|
import Tag from './ast/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 type { Config, Node, NodeType, Schema, Transformer } from './types';
|
|
11
4
|
|
|
12
5
|
type AttributesSchema = Schema['attributes'];
|
|
13
6
|
|
|
7
|
+
function isPromise(a: any): a is Promise<any> {
|
|
8
|
+
return a && typeof a === 'object' && typeof a.then === 'function';
|
|
9
|
+
}
|
|
10
|
+
|
|
14
11
|
export const globalAttributes: AttributesSchema = {
|
|
15
12
|
class: { type: Class, render: true },
|
|
16
13
|
id: { type: String, render: true },
|
|
17
14
|
};
|
|
18
15
|
|
|
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
16
|
export default {
|
|
27
17
|
findSchema(node: Node, { nodes = {}, tags = {} }: Config = {}) {
|
|
28
18
|
return node.tag ? tags[node.tag] : nodes[node.type as NodeType];
|
|
@@ -55,7 +45,11 @@ export default {
|
|
|
55
45
|
},
|
|
56
46
|
|
|
57
47
|
children(node: Node, config: Config = {}) {
|
|
58
|
-
|
|
48
|
+
const children = node.children.flatMap((child) => this.node(child, config));
|
|
49
|
+
if (children.some(isPromise)) {
|
|
50
|
+
return Promise.all(children);
|
|
51
|
+
}
|
|
52
|
+
return children;
|
|
59
53
|
},
|
|
60
54
|
|
|
61
55
|
node(node: Node, config: Config = {}) {
|
|
@@ -67,6 +61,13 @@ export default {
|
|
|
67
61
|
if (!schema || !schema.render) return children;
|
|
68
62
|
|
|
69
63
|
const attributes = this.attributes(node, config);
|
|
64
|
+
|
|
65
|
+
if (isPromise(attributes) || isPromise(children)) {
|
|
66
|
+
return Promise.all([attributes, children]).then(
|
|
67
|
+
(values) => new Tag(schema.render, ...values)
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
70
71
|
return new Tag(schema.render, attributes, children);
|
|
71
72
|
},
|
|
72
73
|
} as Transformer;
|
package/src/transforms/table.ts
CHANGED
|
@@ -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
|
|
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,9 @@
|
|
|
1
|
+
import type Func from './ast/function';
|
|
1
2
|
import type Node from './ast/node';
|
|
2
|
-
import type Function from './ast/function';
|
|
3
3
|
import type Tag from './ast/tag';
|
|
4
4
|
|
|
5
|
-
export type {
|
|
6
|
-
export type
|
|
7
|
-
export type { default as Tag } from './ast/tag';
|
|
5
|
+
export type { Node, Tag };
|
|
6
|
+
export declare type Function = Func;
|
|
8
7
|
|
|
9
8
|
export interface AstType {
|
|
10
9
|
readonly $$mdtype: 'Function' | 'Node' | 'Variable';
|
|
@@ -34,7 +33,7 @@ export type ConfigFunction = {
|
|
|
34
33
|
returns?: ValidationType | ValidationType[];
|
|
35
34
|
parameters?: Record<string, SchemaAttribute>;
|
|
36
35
|
transform?(parameters: Record<string, any>, config: Config): any;
|
|
37
|
-
validate?(fn:
|
|
36
|
+
validate?(fn: Func, config: Config): ValidationError[];
|
|
38
37
|
};
|
|
39
38
|
|
|
40
39
|
export interface CustomAttributeTypeInterface {
|
|
@@ -112,6 +111,13 @@ export type SchemaAttribute = {
|
|
|
112
111
|
|
|
113
112
|
export type SchemaMatches = RegExp | string[] | null;
|
|
114
113
|
|
|
114
|
+
export interface Transformer {
|
|
115
|
+
findSchema(node: Node, config: Config): Schema | undefined;
|
|
116
|
+
node(node: Node, config: Config): RenderableTreeNodes;
|
|
117
|
+
attributes(node: Node, config: Config): Record<string, any>;
|
|
118
|
+
children(node: Node, config: Config): RenderableTreeNode[];
|
|
119
|
+
}
|
|
120
|
+
|
|
115
121
|
export type ValidationError = {
|
|
116
122
|
id: string;
|
|
117
123
|
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(
|
|
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
|
@@ -37,6 +37,8 @@ export function findTagEnd(content: string, start = 0) {
|
|
|
37
37
|
else if (content.startsWith(CLOSE, pos)) return pos;
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
return null;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
function parseTag(content: string, line: number, contentStart: number) {
|
|
@@ -70,7 +72,14 @@ export function parseTags(content: string, firstLine = 0): Token[] {
|
|
|
70
72
|
|
|
71
73
|
if (!content.startsWith(OPEN, pos)) continue;
|
|
72
74
|
|
|
73
|
-
const end = findTagEnd(content, pos)
|
|
75
|
+
const end = findTagEnd(content, pos);
|
|
76
|
+
|
|
77
|
+
if (end == null) {
|
|
78
|
+
// If we cannot find the closing tag, we skip over it
|
|
79
|
+
pos = pos + OPEN.length;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
74
83
|
const text = content.slice(pos, end + CLOSE.length);
|
|
75
84
|
const inner = content.slice(pos + OPEN.length, end);
|
|
76
85
|
const lineStart = content.lastIndexOf('\n', pos);
|
package/src/validator.test.ts
CHANGED
|
@@ -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,30 @@ describe('validate', function () {
|
|
|
295
295
|
expect(output).toEqual([]);
|
|
296
296
|
});
|
|
297
297
|
});
|
|
298
|
+
|
|
299
|
+
it('should not error for missing support for code_block', () => {
|
|
300
|
+
const example = ` # https://spec.commonmark.org/0.30/#indented-code-block
|
|
301
|
+
4-space indented code`;
|
|
302
|
+
const output = validate(example, {});
|
|
303
|
+
|
|
304
|
+
expect(output).toEqual([]);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// https://github.com/markdoc/markdoc/issues/122
|
|
308
|
+
it('should validate partial file attributes', () => {
|
|
309
|
+
const example = `{% partial file="non-existent.md" /%}`;
|
|
310
|
+
const output = validate(example, {});
|
|
311
|
+
|
|
312
|
+
expect(output).toDeepEqualSubset([
|
|
313
|
+
{
|
|
314
|
+
type: 'tag',
|
|
315
|
+
error: {
|
|
316
|
+
id: 'attribute-value-invalid',
|
|
317
|
+
level: 'error',
|
|
318
|
+
message:
|
|
319
|
+
"Partial `non-existent.md` not found. The 'file' attribute must be set in `config.partials`",
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
]);
|
|
323
|
+
});
|
|
298
324
|
});
|
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: `
|
|
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;
|
|
@@ -176,7 +176,7 @@ export default function validate(node: Node, config: Config) {
|
|
|
176
176
|
errors.push({
|
|
177
177
|
id: 'attribute-value-invalid',
|
|
178
178
|
level: 'error',
|
|
179
|
-
message: 'The
|
|
179
|
+
message: 'The id attribute must not start with a number',
|
|
180
180
|
});
|
|
181
181
|
|
|
182
182
|
if (type) {
|
|
@@ -185,7 +185,7 @@ export default function validate(node: Node, config: Config) {
|
|
|
185
185
|
errors.push({
|
|
186
186
|
id: 'attribute-type-invalid',
|
|
187
187
|
level: errorLevel || 'error',
|
|
188
|
-
message: `Attribute '${key}' must be type '${typeToString(type)}'`,
|
|
188
|
+
message: `Attribute '${key}' must be type of '${typeToString(type)}'`,
|
|
189
189
|
});
|
|
190
190
|
}
|
|
191
191
|
if (Array.isArray(valid)) {
|
|
@@ -217,7 +217,7 @@ export default function validate(node: Node, config: Config) {
|
|
|
217
217
|
errors.push({
|
|
218
218
|
id: 'attribute-missing-required',
|
|
219
219
|
level: 'error',
|
|
220
|
-
message: `Missing required attribute '${key}'`,
|
|
220
|
+
message: `Missing required attribute: '${key}'`,
|
|
221
221
|
});
|
|
222
222
|
|
|
223
223
|
for (const { type } of node.children) {
|