@markdoc/markdoc 0.5.13 → 0.6.0
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 +29 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1228 -1143
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +1228 -1143
- package/dist/index.mjs.map +4 -4
- package/dist/react.js +1 -1
- package/dist/react.js.map +2 -2
- package/dist/src/ast/base.d.ts +1 -1
- package/dist/src/ast/base.d.ts.map +1 -1
- package/dist/src/ast/function.d.ts +1 -1
- package/dist/src/ast/function.d.ts.map +1 -1
- package/dist/src/ast/index.d.ts +2 -2
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/ast/variable.d.ts +1 -1
- package/dist/src/ast/variable.d.ts.map +1 -1
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/functions/index.d.ts.map +1 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/renderers/html.d.ts.map +1 -1
- package/dist/src/renderers/react/react.d.ts +3 -3
- package/dist/src/renderers/react/react.d.ts.map +1 -1
- package/dist/src/renderers/react/static.d.ts +1 -1
- package/dist/src/renderers/react/static.d.ts.map +1 -1
- package/dist/src/schema-types/class.d.ts +1 -1
- package/dist/src/schema-types/class.d.ts.map +1 -1
- package/dist/src/schema-types/conditional.d.ts +1 -1
- package/dist/src/schema-types/conditional.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/conditional.d.ts +1 -1
- package/dist/src/tags/conditional.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.map +1 -1
- package/dist/src/validator.d.ts +1 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/index.ts +21 -22
- package/package.json +9 -14
- package/spec/support/matchers.d.ts +6 -4
- package/src/ast/base.test.ts +120 -0
- package/src/ast/base.ts +14 -9
- package/src/ast/function.ts +1 -1
- package/src/ast/index.ts +3 -4
- package/src/ast/node.test.ts +140 -45
- package/src/ast/node.ts +32 -13
- package/src/ast/variable.test.ts +5 -4
- package/src/ast/variable.ts +1 -1
- package/src/ast/walk.test.ts +283 -0
- package/src/formatter.test.ts +31 -3
- package/src/formatter.ts +12 -12
- package/src/functions/index.test.ts +8 -7
- package/src/functions/index.ts +1 -1
- package/src/grammar/tag.d.ts +1 -1
- package/src/grammar/tag.js +208 -285
- package/src/grammar/tag.pegjs +4 -4
- package/src/grammar/tag.test.ts +56 -52
- package/src/parser.test.ts +93 -70
- package/src/parser.ts +19 -6
- package/src/renderers/html.test.ts +56 -16
- package/src/renderers/html.ts +10 -2
- package/src/renderers/react/react.test.ts +49 -28
- package/src/renderers/react/react.ts +8 -9
- package/src/renderers/react/static.ts +7 -12
- package/src/schema-types/class.ts +1 -1
- package/src/schema-types/conditional.ts +1 -1
- package/src/schema.ts +13 -6
- package/src/tag.test.ts +2 -1
- package/src/tag.ts +1 -1
- package/src/tags/conditional.ts +4 -4
- package/src/tags/index.ts +2 -2
- package/src/tags/partial.ts +1 -1
- package/src/tokenizer/index.ts +6 -5
- package/src/tokenizer/plugins/annotations.test.ts +29 -28
- package/src/tokenizer/plugins/annotations.ts +12 -12
- package/src/tokenizer/plugins/comments.test.ts +11 -10
- package/src/tokenizer/plugins/comments.ts +2 -2
- package/src/tokenizer/plugins/frontmatter.test.ts +7 -6
- package/src/tokenizer/plugins/frontmatter.ts +2 -2
- package/src/tokenizer/plugins/link.test.ts +14 -13
- package/src/transformer.ts +7 -5
- package/src/transforms/table.test.ts +18 -17
- package/src/transforms/table.ts +1 -1
- package/src/utils.test.ts +23 -22
- package/src/utils.ts +3 -3
- package/src/validator.test.ts +60 -59
- package/src/validator.ts +43 -14
package/src/ast/node.test.ts
CHANGED
|
@@ -1,14 +1,72 @@
|
|
|
1
|
-
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import markdoc from '../../index';
|
|
3
|
+
import Tag from '../tag';
|
|
4
|
+
import { getAstValues } from './base';
|
|
3
5
|
import FunctionNode from './function';
|
|
4
6
|
import Node from './node';
|
|
5
7
|
import Variable from './variable';
|
|
6
|
-
import { getAstValues } from './base';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
describe('Node object', () => {
|
|
10
|
+
it('resolves into independent children, slots, and nested attributes', () => {
|
|
11
|
+
const node = new Node('tag', {
|
|
12
|
+
nested: { values: [new Variable(['value'])] },
|
|
13
|
+
});
|
|
14
|
+
node.children.push(new Node('text', { content: new Variable(['value']) }));
|
|
15
|
+
node.slots.title = new Node('text', {
|
|
16
|
+
content: new FunctionNode('identity', { 0: new Variable(['value']) }),
|
|
17
|
+
});
|
|
18
|
+
const config = {
|
|
19
|
+
variables: { value: 'resolved' },
|
|
20
|
+
functions: { identity: { transform: (parameters) => parameters[0] } },
|
|
21
|
+
};
|
|
22
|
+
const resolved = node.resolve(config);
|
|
23
|
+
|
|
24
|
+
expect(resolved).not.toBe(node);
|
|
25
|
+
expect(Object.getPrototypeOf(resolved)).toBe(Node.prototype);
|
|
26
|
+
expect(Object.keys(resolved)).toEqual(Object.keys(node));
|
|
27
|
+
expect(resolved.attributes.nested).toEqual({ values: ['resolved'] });
|
|
28
|
+
expect(resolved.children[0].attributes.content).toBe('resolved');
|
|
29
|
+
expect(resolved.slots.title.attributes.content).toBe('resolved');
|
|
30
|
+
expect(resolved.children).not.toBe(node.children);
|
|
31
|
+
expect(resolved.children[0]).not.toBe(node.children[0]);
|
|
32
|
+
expect(resolved.slots).not.toBe(node.slots);
|
|
33
|
+
expect(resolved.slots.title).not.toBe(node.slots.title);
|
|
34
|
+
expect(resolved.attributes).not.toBe(node.attributes);
|
|
35
|
+
expect(resolved.attributes.nested).not.toBe(node.attributes.nested);
|
|
36
|
+
expect(resolved.attributes.nested.values).not.toBe(
|
|
37
|
+
node.attributes.nested.values,
|
|
38
|
+
);
|
|
9
39
|
|
|
10
|
-
|
|
11
|
-
|
|
40
|
+
resolved.children[0].attributes.content = 'changed';
|
|
41
|
+
resolved.slots.title.attributes.content = 'changed';
|
|
42
|
+
resolved.attributes.nested.values.push('changed');
|
|
43
|
+
const again = node.resolve(config);
|
|
44
|
+
expect(again.children[0].attributes.content).toBe('resolved');
|
|
45
|
+
expect(again.slots.title.attributes.content).toBe('resolved');
|
|
46
|
+
expect(again.attributes.nested.values).toEqual(['resolved']);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('restores missing defaults and preserves metadata while resolving', () => {
|
|
50
|
+
const node = new Node('text', { content: 'example' });
|
|
51
|
+
const initialKeys = Object.keys(node);
|
|
52
|
+
Reflect.deleteProperty(node, 'inline');
|
|
53
|
+
Reflect.deleteProperty(node, 'errors');
|
|
54
|
+
Reflect.deleteProperty(node, 'annotations');
|
|
55
|
+
node.lines = [1, 2];
|
|
56
|
+
|
|
57
|
+
const resolved = node.resolve();
|
|
58
|
+
const another = node.resolve();
|
|
59
|
+
expect(Object.keys(resolved)).toEqual(initialKeys);
|
|
60
|
+
expect(resolved.inline).toBe(false);
|
|
61
|
+
expect(resolved.errors).toEqual([]);
|
|
62
|
+
expect(resolved.annotations).toEqual([]);
|
|
63
|
+
expect(resolved.errors).not.toBe(another.errors);
|
|
64
|
+
expect(resolved.annotations).not.toBe(another.annotations);
|
|
65
|
+
expect(resolved.lines).toBe(node.lines);
|
|
66
|
+
expect(resolved.slots).not.toBe(node.slots);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('copies only own attributes into a prototype-free dictionary', () => {
|
|
12
70
|
const attributes = Object.assign(Object.create({ inherited: 'value' }), {
|
|
13
71
|
own: 'value',
|
|
14
72
|
});
|
|
@@ -18,7 +76,7 @@ describe('Node object', function () {
|
|
|
18
76
|
expect(node.attributes.own).toBe('value');
|
|
19
77
|
});
|
|
20
78
|
|
|
21
|
-
it('preserves prototype-free attributes and resolves nested values',
|
|
79
|
+
it('preserves prototype-free attributes and resolves nested values', () => {
|
|
22
80
|
const attributes = Object.fromEntries([
|
|
23
81
|
['__proto__', { src: new Variable(['url']) }],
|
|
24
82
|
['src', new Variable(['url'])],
|
|
@@ -29,17 +87,18 @@ describe('Node object', function () {
|
|
|
29
87
|
});
|
|
30
88
|
expect(Object.getPrototypeOf(resolved.attributes)).toBeNull();
|
|
31
89
|
expect(Object.keys(resolved.attributes)).toEqual(['__proto__', 'src']);
|
|
90
|
+
// biome-ignore lint/suspicious/noProto: Verify the own __proto__ data property preserved by the security fix.
|
|
32
91
|
expect(resolved.attributes.__proto__.src).toBe('https://example.com/');
|
|
33
92
|
expect(resolved.attributes.src).toBe('https://example.com/');
|
|
34
93
|
});
|
|
35
94
|
|
|
36
|
-
it('traverses AST values in prototype-free attribute dictionaries',
|
|
95
|
+
it('traverses AST values in prototype-free attribute dictionaries', () => {
|
|
37
96
|
const variable = new Variable(['url']);
|
|
38
97
|
const node = new Node('tag', { src: variable });
|
|
39
98
|
expect([...getAstValues(node.attributes)]).toEqual([variable]);
|
|
40
99
|
});
|
|
41
100
|
|
|
42
|
-
it('resolves object values without invoking prototype setters',
|
|
101
|
+
it('resolves object values without invoking prototype setters', () => {
|
|
43
102
|
const value = Object.fromEntries([
|
|
44
103
|
['safe', new Variable(['url'])],
|
|
45
104
|
['__proto__', { src: new Variable(['url']) }],
|
|
@@ -49,26 +108,27 @@ describe('Node object', function () {
|
|
|
49
108
|
variables: { url: 'https://example.com/' },
|
|
50
109
|
});
|
|
51
110
|
expect(Object.getPrototypeOf(resolved.attributes.value)).toBe(
|
|
52
|
-
Object.prototype
|
|
111
|
+
Object.prototype,
|
|
53
112
|
);
|
|
54
113
|
expect(Object.keys(resolved.attributes.value)).toEqual([
|
|
55
114
|
'safe',
|
|
56
115
|
'__proto__',
|
|
57
116
|
]);
|
|
58
117
|
expect(resolved.attributes.value.src).toBeUndefined();
|
|
118
|
+
// biome-ignore lint/suspicious/noProto: Verify the own __proto__ data property preserved by the security fix.
|
|
59
119
|
expect(resolved.attributes.value.__proto__.src).toBe(
|
|
60
|
-
'https://example.com/'
|
|
120
|
+
'https://example.com/',
|
|
61
121
|
);
|
|
62
122
|
});
|
|
63
123
|
|
|
64
|
-
it('resolves serialized function and variable attributes',
|
|
124
|
+
it('resolves serialized function and variable attributes', () => {
|
|
65
125
|
const serialized = JSON.parse(
|
|
66
126
|
JSON.stringify({
|
|
67
127
|
direct: new Variable(['answer']),
|
|
68
128
|
computed: new FunctionNode('identity', {
|
|
69
129
|
0: new Variable(['answer']),
|
|
70
130
|
}),
|
|
71
|
-
})
|
|
131
|
+
}),
|
|
72
132
|
);
|
|
73
133
|
const node = new Node('tag', serialized);
|
|
74
134
|
const resolved = node.resolve({
|
|
@@ -82,8 +142,8 @@ describe('Node object', function () {
|
|
|
82
142
|
expect(resolved.attributes.computed).toBe('resolved');
|
|
83
143
|
});
|
|
84
144
|
|
|
85
|
-
describe('traversal',
|
|
86
|
-
it('with a simple document',
|
|
145
|
+
describe('traversal', () => {
|
|
146
|
+
it('with a simple document', () => {
|
|
87
147
|
const example = new Node('document', {}, [
|
|
88
148
|
new Node('heading', { level: 1 }, [
|
|
89
149
|
new Node('inline', {}, [
|
|
@@ -105,7 +165,7 @@ describe('Node object', function () {
|
|
|
105
165
|
});
|
|
106
166
|
});
|
|
107
167
|
|
|
108
|
-
it('without slots',
|
|
168
|
+
it('without slots', () => {
|
|
109
169
|
const example = `
|
|
110
170
|
{% example %}
|
|
111
171
|
# bar
|
|
@@ -127,7 +187,7 @@ baz
|
|
|
127
187
|
]);
|
|
128
188
|
});
|
|
129
189
|
|
|
130
|
-
it('with slots',
|
|
190
|
+
it('with slots', () => {
|
|
131
191
|
const example = `
|
|
132
192
|
{% example %}
|
|
133
193
|
# bar
|
|
@@ -153,13 +213,13 @@ baz
|
|
|
153
213
|
});
|
|
154
214
|
});
|
|
155
215
|
|
|
156
|
-
describe('transform',
|
|
216
|
+
describe('transform', () => {
|
|
157
217
|
function transform(content, config = {}) {
|
|
158
218
|
return markdoc.transform(content, config);
|
|
159
219
|
}
|
|
160
220
|
|
|
161
|
-
describe('built-in nodes',
|
|
162
|
-
it('a heading node',
|
|
221
|
+
describe('built-in nodes', () => {
|
|
222
|
+
it('a heading node', () => {
|
|
163
223
|
const example = new Node('heading', { level: 1 }, [
|
|
164
224
|
new Node('inline', {}, [
|
|
165
225
|
new Node('text', { content: 'This is a heading' }),
|
|
@@ -170,22 +230,57 @@ describe('transform', function () {
|
|
|
170
230
|
expect(output).toDeepEqual(new Tag('h1', {}, ['This is a heading']));
|
|
171
231
|
});
|
|
172
232
|
|
|
173
|
-
|
|
174
|
-
|
|
233
|
+
it('a heading node with an invalid level', () => {
|
|
234
|
+
const levels = ['1>x', '2', 0, 7, 1.5];
|
|
235
|
+
for (const level of levels) {
|
|
236
|
+
const example = new Node('heading', { level }, [
|
|
237
|
+
new Node('inline', {}, [
|
|
238
|
+
new Node('text', { content: 'This is a heading' }),
|
|
239
|
+
]),
|
|
240
|
+
]);
|
|
241
|
+
|
|
242
|
+
expect(transform(example)).toDeepEqual(['This is a heading']);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe('fenced code blocks', () => {
|
|
247
|
+
it('fenced code block with language', () => {
|
|
175
248
|
const example = new Node('fence', {
|
|
176
249
|
language: 'ruby',
|
|
177
250
|
content: 'test',
|
|
178
251
|
});
|
|
179
252
|
const output = transform(example);
|
|
180
253
|
expect(output).toDeepEqual(
|
|
181
|
-
new Tag('pre', { 'data-language': 'ruby' }, ['test'])
|
|
254
|
+
new Tag('pre', { 'data-language': 'ruby' }, ['test']),
|
|
182
255
|
);
|
|
183
256
|
});
|
|
257
|
+
|
|
258
|
+
it('fenced code block with a custom render', () => {
|
|
259
|
+
const example = new Node('fence', {
|
|
260
|
+
language: 'ruby',
|
|
261
|
+
content: 'test',
|
|
262
|
+
process: false,
|
|
263
|
+
});
|
|
264
|
+
const output = transform(example, {
|
|
265
|
+
nodes: { fence: { ...markdoc.nodes.fence, render: 'Code' } },
|
|
266
|
+
});
|
|
267
|
+
expect(output).toDeepEqual(
|
|
268
|
+
new Tag('Code', { 'data-language': 'ruby' }, ['test']),
|
|
269
|
+
);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('an inline code node with a custom render', () => {
|
|
274
|
+
const example = new Node('code', { content: 'test' });
|
|
275
|
+
const output = transform(example, {
|
|
276
|
+
nodes: { code: { ...markdoc.nodes.code, render: 'InlineCode' } },
|
|
277
|
+
});
|
|
278
|
+
expect(output).toDeepEqual(new Tag('InlineCode', {}, ['test']));
|
|
184
279
|
});
|
|
185
280
|
});
|
|
186
281
|
|
|
187
|
-
describe('transform functions',
|
|
188
|
-
it('with a simple render function',
|
|
282
|
+
describe('transform functions', () => {
|
|
283
|
+
it('with a simple render function', () => {
|
|
189
284
|
const example = new Node('foo', {});
|
|
190
285
|
|
|
191
286
|
const foo = {
|
|
@@ -198,7 +293,7 @@ describe('transform', function () {
|
|
|
198
293
|
expect(output).toDeepEqual(new Tag('foo', {}, []));
|
|
199
294
|
});
|
|
200
295
|
|
|
201
|
-
it('with a render function that renders attributes',
|
|
296
|
+
it('with a render function that renders attributes', () => {
|
|
202
297
|
const example = new Node('foo', { bar: 'baz', test: 100 });
|
|
203
298
|
const foo = {
|
|
204
299
|
attributes: {
|
|
@@ -221,7 +316,7 @@ describe('transform', function () {
|
|
|
221
316
|
});
|
|
222
317
|
});
|
|
223
318
|
|
|
224
|
-
describe('tags',
|
|
319
|
+
describe('tags', () => {
|
|
225
320
|
const tags = {
|
|
226
321
|
foo: {
|
|
227
322
|
render: 'foo',
|
|
@@ -231,32 +326,32 @@ describe('transform', function () {
|
|
|
231
326
|
},
|
|
232
327
|
};
|
|
233
328
|
|
|
234
|
-
it('with a tag',
|
|
329
|
+
it('with a tag', () => {
|
|
235
330
|
const example = new Node(
|
|
236
331
|
'tag',
|
|
237
332
|
{ bar: 'baz' },
|
|
238
333
|
[new Node('inline', {}, [new Node('text', { content: 'test' })])],
|
|
239
|
-
'foo'
|
|
334
|
+
'foo',
|
|
240
335
|
);
|
|
241
336
|
|
|
242
337
|
const output = transform(example, { tags });
|
|
243
338
|
expect(output).toDeepEqual(new Tag('foo', { bar: 'baz' }, ['test']));
|
|
244
339
|
});
|
|
245
340
|
|
|
246
|
-
it('with a non-existing tag',
|
|
341
|
+
it('with a non-existing tag', () => {
|
|
247
342
|
const example = new Node('container', { bar: 'baz' }, [
|
|
248
343
|
new Node('inline', {}, [new Node('text', { content: 'test' })]),
|
|
249
344
|
]);
|
|
250
345
|
|
|
251
|
-
// @ts-
|
|
346
|
+
// @ts-expect-error test mutating node in userland
|
|
252
347
|
example.container = 'bar';
|
|
253
348
|
const output = transform(example, { tags });
|
|
254
349
|
expect(output).toDeepEqual(['test']);
|
|
255
350
|
});
|
|
256
351
|
});
|
|
257
352
|
|
|
258
|
-
describe('tag name',
|
|
259
|
-
it('with a string value',
|
|
353
|
+
describe('tag name', () => {
|
|
354
|
+
it('with a string value', () => {
|
|
260
355
|
const example = new Node('foo', {});
|
|
261
356
|
const foo = { render: 'foo' };
|
|
262
357
|
|
|
@@ -264,7 +359,7 @@ describe('transform', function () {
|
|
|
264
359
|
expect(output.name).toEqual('foo');
|
|
265
360
|
});
|
|
266
361
|
|
|
267
|
-
it('with no value',
|
|
362
|
+
it('with no value', () => {
|
|
268
363
|
const example = new Node('foo', {}, [new Node('bar', {})]);
|
|
269
364
|
|
|
270
365
|
const output = transform(example, {
|
|
@@ -274,8 +369,8 @@ describe('transform', function () {
|
|
|
274
369
|
});
|
|
275
370
|
});
|
|
276
371
|
|
|
277
|
-
describe('attributes',
|
|
278
|
-
it('with an id',
|
|
372
|
+
describe('attributes', () => {
|
|
373
|
+
it('with an id', () => {
|
|
279
374
|
const example = new Node('paragraph', { id: 'bar' });
|
|
280
375
|
const output = transform(example);
|
|
281
376
|
expect(output).toDeepEqualSubset({
|
|
@@ -284,7 +379,7 @@ describe('transform', function () {
|
|
|
284
379
|
});
|
|
285
380
|
});
|
|
286
381
|
|
|
287
|
-
it('with a class',
|
|
382
|
+
it('with a class', () => {
|
|
288
383
|
const example = new Node('paragraph', {
|
|
289
384
|
class: { foo: true, bar: false },
|
|
290
385
|
});
|
|
@@ -292,7 +387,7 @@ describe('transform', function () {
|
|
|
292
387
|
expect(output).toDeepEqual(new Tag('p', { class: 'foo' }, []));
|
|
293
388
|
});
|
|
294
389
|
|
|
295
|
-
it('with boolean render attribute',
|
|
390
|
+
it('with boolean render attribute', () => {
|
|
296
391
|
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
297
392
|
const foo = {
|
|
298
393
|
render: 'foo',
|
|
@@ -306,7 +401,7 @@ describe('transform', function () {
|
|
|
306
401
|
expect(output).toDeepEqual(new Tag('foo', { bar: 1, baz: 'test' }));
|
|
307
402
|
});
|
|
308
403
|
|
|
309
|
-
it('with string render attribute',
|
|
404
|
+
it('with string render attribute', () => {
|
|
310
405
|
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
311
406
|
const foo = {
|
|
312
407
|
render: 'foo',
|
|
@@ -318,11 +413,11 @@ describe('transform', function () {
|
|
|
318
413
|
|
|
319
414
|
const output = transform(example, { nodes: { foo } });
|
|
320
415
|
expect(output).toDeepEqual(
|
|
321
|
-
new Tag('foo', { 'data-bar': 1, 'data-baz': 'test' })
|
|
416
|
+
new Tag('foo', { 'data-bar': 1, 'data-baz': 'test' }),
|
|
322
417
|
);
|
|
323
418
|
});
|
|
324
419
|
|
|
325
|
-
it('with a non-rendered attribute',
|
|
420
|
+
it('with a non-rendered attribute', () => {
|
|
326
421
|
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
327
422
|
const foo = {
|
|
328
423
|
render: 'foo',
|
|
@@ -332,11 +427,11 @@ describe('transform', function () {
|
|
|
332
427
|
};
|
|
333
428
|
|
|
334
429
|
const output = transform(example, { nodes: { foo } }) as Tag;
|
|
335
|
-
expect(Object.keys(output.attributes).includes('baz')).
|
|
430
|
+
expect(Object.keys(output.attributes).includes('baz')).toBe(false);
|
|
336
431
|
expect(output).toDeepEqual(new Tag('foo', { bar: 1 }, []));
|
|
337
432
|
});
|
|
338
433
|
|
|
339
|
-
it('with a default attribute value',
|
|
434
|
+
it('with a default attribute value', () => {
|
|
340
435
|
const example = new Node('foo', {});
|
|
341
436
|
const foo = {
|
|
342
437
|
render: 'foo',
|
|
@@ -349,7 +444,7 @@ describe('transform', function () {
|
|
|
349
444
|
expect(output.attributes.bar).toEqual(10);
|
|
350
445
|
});
|
|
351
446
|
|
|
352
|
-
it('with an overriden default attribute value',
|
|
447
|
+
it('with an overriden default attribute value', () => {
|
|
353
448
|
const example = new Node('foo', { bar: 1 });
|
|
354
449
|
const foo = {
|
|
355
450
|
render: 'foo',
|
|
@@ -362,7 +457,7 @@ describe('transform', function () {
|
|
|
362
457
|
expect(output.attributes.bar).toEqual(1);
|
|
363
458
|
});
|
|
364
459
|
|
|
365
|
-
it('variable nested in a hash',
|
|
460
|
+
it('variable nested in a hash', () => {
|
|
366
461
|
const bar = new Variable(['a', 'b', 'c']);
|
|
367
462
|
const example = new Node('foo', { bar });
|
|
368
463
|
const foo = {
|
|
@@ -431,7 +526,7 @@ describe('transform', function () {
|
|
|
431
526
|
describe('annotations', () => {
|
|
432
527
|
it('multiple values should be ordered correctly', () => {
|
|
433
528
|
const example = markdoc.parse(
|
|
434
|
-
`\`\`\`js {% z=true .class y=2 x="1" #id %} \nContent\n
|
|
529
|
+
`\`\`\`js {% z=true .class y=2 x="1" #id %} \nContent\n\`\`\``,
|
|
435
530
|
);
|
|
436
531
|
|
|
437
532
|
const fence = example.children[0];
|
package/src/ast/node.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { resolve } from './base';
|
|
2
1
|
import transformer from '../transformer';
|
|
3
2
|
import type {
|
|
4
3
|
AstType,
|
|
@@ -12,6 +11,7 @@ import type {
|
|
|
12
11
|
Schema,
|
|
13
12
|
ValidationError,
|
|
14
13
|
} from '../types';
|
|
14
|
+
import { resolve } from './base';
|
|
15
15
|
|
|
16
16
|
export default class Node implements AstType {
|
|
17
17
|
readonly $$mdtype = 'Node';
|
|
@@ -32,7 +32,7 @@ export default class Node implements AstType {
|
|
|
32
32
|
type: NodeType = 'node',
|
|
33
33
|
attributes: Record<string, any> = {},
|
|
34
34
|
children: Node[] = [],
|
|
35
|
-
tag?: string
|
|
35
|
+
tag?: string,
|
|
36
36
|
) {
|
|
37
37
|
this.attributes = Object.assign(Object.create(null), attributes);
|
|
38
38
|
this.children = children;
|
|
@@ -43,9 +43,21 @@ export default class Node implements AstType {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
*walk(): Generator<Node, void, unknown> {
|
|
46
|
-
|
|
46
|
+
const stack = [...Object.values(this.slots), ...this.children].reverse();
|
|
47
|
+
while (stack.length) {
|
|
48
|
+
const child = stack.pop()!;
|
|
47
49
|
yield child;
|
|
48
|
-
|
|
50
|
+
|
|
51
|
+
const walk = child.walk;
|
|
52
|
+
if (walk !== defaultWalk) {
|
|
53
|
+
yield* Reflect.apply(walk, child, []);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Snapshot descendants after yielding their parent, as recursive walk did.
|
|
58
|
+
const children = [...Object.values(child.slots), ...child.children];
|
|
59
|
+
for (let index = children.length - 1; index >= 0; index--)
|
|
60
|
+
stack.push(children[index]);
|
|
49
61
|
}
|
|
50
62
|
}
|
|
51
63
|
|
|
@@ -54,15 +66,19 @@ export default class Node implements AstType {
|
|
|
54
66
|
}
|
|
55
67
|
|
|
56
68
|
resolve(config: Config = {}): Node {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
const node = new Node();
|
|
70
|
+
const children = this.children.map((child) => child.resolve(config));
|
|
71
|
+
const attributes = resolve(this.attributes, config);
|
|
72
|
+
const slots = Object.entries(this.slots);
|
|
73
|
+
|
|
74
|
+
return Object.assign(node, this, {
|
|
75
|
+
children,
|
|
76
|
+
attributes,
|
|
77
|
+
slots: slots.length
|
|
78
|
+
? Object.fromEntries(
|
|
79
|
+
slots.map(([name, slot]) => [name, slot.resolve(config)]),
|
|
80
|
+
)
|
|
81
|
+
: {},
|
|
66
82
|
});
|
|
67
83
|
}
|
|
68
84
|
|
|
@@ -82,3 +98,6 @@ export default class Node implements AstType {
|
|
|
82
98
|
return transformer.node(this, config);
|
|
83
99
|
}
|
|
84
100
|
}
|
|
101
|
+
|
|
102
|
+
// Keep the original method so replacements on the prototype are also delegated.
|
|
103
|
+
const defaultWalk = Node.prototype.walk;
|
package/src/ast/variable.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
1
2
|
import Variable from './variable';
|
|
2
3
|
|
|
3
|
-
describe('variable',
|
|
4
|
+
describe('variable', () => {
|
|
4
5
|
const config = {
|
|
5
6
|
variables: {
|
|
6
7
|
foo: {
|
|
@@ -10,19 +11,19 @@ describe('variable', function () {
|
|
|
10
11
|
},
|
|
11
12
|
};
|
|
12
13
|
|
|
13
|
-
it('basic resolution',
|
|
14
|
+
it('basic resolution', () => {
|
|
14
15
|
const example = new Variable(['foo', 'bar']);
|
|
15
16
|
const output = example.resolve(config);
|
|
16
17
|
expect(output).toEqual('example');
|
|
17
18
|
});
|
|
18
19
|
|
|
19
|
-
it('resolution with a number in the path',
|
|
20
|
+
it('resolution with a number in the path', () => {
|
|
20
21
|
const example = new Variable(['foo', 'baz', 1, 'qux']);
|
|
21
22
|
const output = example.resolve(config);
|
|
22
23
|
expect(output).toEqual('value');
|
|
23
24
|
});
|
|
24
25
|
|
|
25
|
-
it('resolution with a function',
|
|
26
|
+
it('resolution with a function', () => {
|
|
26
27
|
function variables(path) {
|
|
27
28
|
expect(path).toDeepEqual(['foo', 'bar']);
|
|
28
29
|
return 'example';
|
package/src/ast/variable.ts
CHANGED