@markdoc/markdoc 0.5.14 → 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 +18 -1
- package/dist/index.js +635 -555
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +635 -555
- package/dist/index.mjs.map +4 -4
- package/dist/react.js.map +1 -1
- package/dist/src/ast/base.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/renderers/react/react.d.ts +1 -1
- package/dist/src/renderers/react/react.d.ts.map +1 -1
- package/dist/src/renderers/react/static.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/ast/base.test.ts +120 -0
- package/src/ast/base.ts +12 -7
- package/src/ast/node.test.ts +81 -0
- package/src/ast/node.ts +30 -11
- package/src/ast/walk.test.ts +283 -0
- package/src/formatter.test.ts +27 -0
- package/src/formatter.ts +1 -1
- package/src/renderers/react/react.test.ts +20 -0
- package/src/renderers/react/react.ts +1 -1
- package/src/renderers/react/static.ts +2 -6
- package/src/schema.ts +4 -2
- package/src/validator.ts +30 -3
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import Markdoc from '../../index';
|
|
3
|
+
import { walkWithParents } from '../validator';
|
|
4
|
+
import Node from './node';
|
|
5
|
+
|
|
6
|
+
function expectNodes(actual: Node[], expected: Node[]) {
|
|
7
|
+
expect(actual).toHaveLength(expected.length);
|
|
8
|
+
for (const [index, node] of expected.entries())
|
|
9
|
+
expect(actual[index]).toBe(node);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function expectVisit(
|
|
13
|
+
actual: [Node, Node[]] | undefined,
|
|
14
|
+
expected: [Node, Node[]],
|
|
15
|
+
) {
|
|
16
|
+
expect(actual).toBeDefined();
|
|
17
|
+
expect(actual![0]).toBe(expected[0]);
|
|
18
|
+
expectNodes(actual![1], expected[1]);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function expectVisits(actual: [Node, Node[]][], expected: [Node, Node[]][]) {
|
|
22
|
+
expect(actual).toHaveLength(expected.length);
|
|
23
|
+
for (const [index, entry] of expected.entries())
|
|
24
|
+
expectVisit(actual[index], entry);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('Node.walk', () => {
|
|
28
|
+
it('visits slots before children in preorder, including repeated nodes', () => {
|
|
29
|
+
const shared = new Node();
|
|
30
|
+
const slot = new Node('slot', {}, [shared]);
|
|
31
|
+
const child = new Node('paragraph', {}, [shared]);
|
|
32
|
+
const childSlot = new Node('slot');
|
|
33
|
+
child.slots.title = childSlot;
|
|
34
|
+
const root = new Node('document', {}, [child]);
|
|
35
|
+
root.slots.title = slot;
|
|
36
|
+
|
|
37
|
+
expectNodes([...root.walk()], [slot, shared, child, childSlot, shared]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('snapshots siblings but reads descendants after yielding their parent', () => {
|
|
41
|
+
const first = new Node();
|
|
42
|
+
const second = new Node();
|
|
43
|
+
const root = new Node('document', {}, [first, second]);
|
|
44
|
+
const iter = root.walk();
|
|
45
|
+
const beforeStart = new Node();
|
|
46
|
+
root.children.push(beforeStart);
|
|
47
|
+
expect(iter.next().value).toBe(first);
|
|
48
|
+
|
|
49
|
+
root.children = [];
|
|
50
|
+
const descendant = new Node();
|
|
51
|
+
first.slots.title = descendant;
|
|
52
|
+
expect(iter.next().value).toBe(descendant);
|
|
53
|
+
|
|
54
|
+
first.children.push(new Node());
|
|
55
|
+
const laterDescendant = new Node();
|
|
56
|
+
second.children.push(laterDescendant);
|
|
57
|
+
expectNodes([...iter], [second, laterDescendant, beforeStart]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('delegates to custom child walkers with the child as receiver', () => {
|
|
61
|
+
const replacement = new Node();
|
|
62
|
+
class CustomNode extends Node {
|
|
63
|
+
*walk() {
|
|
64
|
+
expect(this).toBe(custom);
|
|
65
|
+
yield replacement;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const custom = new CustomNode('node', {}, [new Node()]);
|
|
69
|
+
Object.defineProperty(custom.walk, 'call', { value: undefined });
|
|
70
|
+
const sibling = new Node();
|
|
71
|
+
const root = new Node('document', {}, [custom, sibling]);
|
|
72
|
+
|
|
73
|
+
expectNodes([...root.walk()], [custom, replacement, sibling]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('reads a child walker after yielding the child', () => {
|
|
77
|
+
const child = new Node();
|
|
78
|
+
const replacement = new Node();
|
|
79
|
+
const iter = new Node('document', {}, [child]).walk();
|
|
80
|
+
expect(iter.next().value).toBe(child);
|
|
81
|
+
child.walk = function* () {
|
|
82
|
+
yield replacement;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
expectNodes([...iter], [replacement]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('honors prototype walker replacements during iteration', () => {
|
|
89
|
+
const child = new Node();
|
|
90
|
+
const replacement = new Node();
|
|
91
|
+
const iter = new Node('document', {}, [child]).walk();
|
|
92
|
+
expect(iter.next().value).toBe(child);
|
|
93
|
+
const original = Node.prototype.walk;
|
|
94
|
+
try {
|
|
95
|
+
Node.prototype.walk = function* () {
|
|
96
|
+
yield replacement;
|
|
97
|
+
};
|
|
98
|
+
expectNodes([...iter], [replacement]);
|
|
99
|
+
} finally {
|
|
100
|
+
Node.prototype.walk = original;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('closes a delegated child walker when iteration stops', () => {
|
|
105
|
+
let closed = false;
|
|
106
|
+
const child = new Node();
|
|
107
|
+
child.walk = function* () {
|
|
108
|
+
try {
|
|
109
|
+
yield new Node();
|
|
110
|
+
} finally {
|
|
111
|
+
closed = true;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const iter = new Node('document', {}, [child]).walk();
|
|
115
|
+
iter.next();
|
|
116
|
+
iter.next();
|
|
117
|
+
iter.return();
|
|
118
|
+
|
|
119
|
+
expect(closed).toBe(true);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('walks deeply nested trees without recursive generator calls', () => {
|
|
123
|
+
let root = new Node();
|
|
124
|
+
for (let index = 0; index < 4000; index++)
|
|
125
|
+
root = new Node('node', {}, [root]);
|
|
126
|
+
|
|
127
|
+
let count = 0;
|
|
128
|
+
for (const _node of root.walk()) count++;
|
|
129
|
+
expect(count).toBe(4000);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('walkWithParents', () => {
|
|
134
|
+
it('includes the root and visits slots before children with fresh parent arrays', () => {
|
|
135
|
+
const leaf = new Node();
|
|
136
|
+
const slot = new Node('slot', {}, [leaf]);
|
|
137
|
+
const child = new Node();
|
|
138
|
+
const root = new Node('document', {}, [child]);
|
|
139
|
+
root.slots.title = slot;
|
|
140
|
+
const entries = [...walkWithParents(root)];
|
|
141
|
+
|
|
142
|
+
expectVisits(entries, [
|
|
143
|
+
[root, []],
|
|
144
|
+
[slot, [root]],
|
|
145
|
+
[leaf, [root, slot]],
|
|
146
|
+
[child, [root]],
|
|
147
|
+
]);
|
|
148
|
+
expect(new Set(entries.map(([, parents]) => parents)).size).toBe(4);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('reads descendants after yielding and snapshots each sibling list', () => {
|
|
152
|
+
const first = new Node();
|
|
153
|
+
const second = new Node();
|
|
154
|
+
const root = new Node('document', {}, [first]);
|
|
155
|
+
const iter = walkWithParents(root);
|
|
156
|
+
expectVisit(iter.next().value, [root, []]);
|
|
157
|
+
root.children.push(second);
|
|
158
|
+
expectVisit(iter.next().value, [first, [root]]);
|
|
159
|
+
|
|
160
|
+
root.children = [];
|
|
161
|
+
const descendant = new Node();
|
|
162
|
+
first.slots.title = descendant;
|
|
163
|
+
expectVisit(iter.next().value, [descendant, [root, first]]);
|
|
164
|
+
first.children.push(new Node());
|
|
165
|
+
expectVisits([...iter], [[second, [root]]]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('copies the current parent array separately when each child is reached', () => {
|
|
169
|
+
const grandchild = new Node();
|
|
170
|
+
const first = new Node('node', {}, [grandchild]);
|
|
171
|
+
const second = new Node();
|
|
172
|
+
const root = new Node('document', {}, [first, second]);
|
|
173
|
+
const ancestor = new Node();
|
|
174
|
+
const parents = [ancestor];
|
|
175
|
+
const iter = walkWithParents(root, parents);
|
|
176
|
+
const rootEntry = iter.next().value as [Node, Node[]];
|
|
177
|
+
expectVisit(rootEntry, [root, parents]);
|
|
178
|
+
expect(rootEntry[1]).toBe(parents);
|
|
179
|
+
const before = new Node();
|
|
180
|
+
parents.push(before);
|
|
181
|
+
const firstEntry = iter.next().value as [Node, Node[]];
|
|
182
|
+
expectVisit(firstEntry, [first, [ancestor, before, root]]);
|
|
183
|
+
|
|
184
|
+
const local = new Node();
|
|
185
|
+
firstEntry[1].push(local);
|
|
186
|
+
const later = new Node();
|
|
187
|
+
parents.push(later);
|
|
188
|
+
expectVisit(iter.next().value, [
|
|
189
|
+
grandchild,
|
|
190
|
+
[ancestor, before, root, local, first],
|
|
191
|
+
]);
|
|
192
|
+
const secondEntry = iter.next().value as [Node, Node[]];
|
|
193
|
+
expectVisit(secondEntry, [second, [ancestor, before, later, root]]);
|
|
194
|
+
expect(secondEntry[1]).not.toBe(firstEntry[1]);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('does not use replacements made to the yielded tuple', () => {
|
|
198
|
+
const child = new Node();
|
|
199
|
+
const root = new Node('document', {}, [child]);
|
|
200
|
+
const iter = walkWithParents(root);
|
|
201
|
+
const entry = iter.next().value as [Node, Node[]];
|
|
202
|
+
entry[0] = new Node();
|
|
203
|
+
entry[1] = [new Node()];
|
|
204
|
+
|
|
205
|
+
expectVisits([...iter], [[child, [root]]]);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('walks deeply nested trees without recursive generator calls', () => {
|
|
209
|
+
let root = new Node();
|
|
210
|
+
for (let index = 0; index < 4000; index++)
|
|
211
|
+
root = new Node('node', {}, [root]);
|
|
212
|
+
|
|
213
|
+
let count = 0;
|
|
214
|
+
let depth = 0;
|
|
215
|
+
for (const [, parents] of walkWithParents(root)) {
|
|
216
|
+
count++;
|
|
217
|
+
depth = parents.length;
|
|
218
|
+
}
|
|
219
|
+
expect(count).toBe(4001);
|
|
220
|
+
expect(depth).toBe(4000);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
describe('validation traversal', () => {
|
|
225
|
+
it('snapshots the tree and gives each validation hook its own parent array', () => {
|
|
226
|
+
const first = new Node();
|
|
227
|
+
const second = new Node();
|
|
228
|
+
const root = new Node('node', {}, [first, second]);
|
|
229
|
+
const seen: [Node, Node[]][] = [];
|
|
230
|
+
const parentArrays: Node[][] = [];
|
|
231
|
+
const poison = new Node();
|
|
232
|
+
|
|
233
|
+
expect(
|
|
234
|
+
Markdoc.validate(root, {
|
|
235
|
+
nodes: {
|
|
236
|
+
node: {
|
|
237
|
+
validate(node, config) {
|
|
238
|
+
const parents = config.validation!.parents!;
|
|
239
|
+
seen.push([node, [...parents]]);
|
|
240
|
+
parentArrays.push(parents);
|
|
241
|
+
parents.push(poison);
|
|
242
|
+
if (node === root) root.children = [new Node()];
|
|
243
|
+
return [];
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
}),
|
|
248
|
+
).toEqual([]);
|
|
249
|
+
|
|
250
|
+
expectVisits(seen, [
|
|
251
|
+
[root, []],
|
|
252
|
+
[first, [root]],
|
|
253
|
+
[second, [root]],
|
|
254
|
+
]);
|
|
255
|
+
expect(new Set(parentArrays).size).toBe(3);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('keeps parent arrays stable for asynchronous validation hooks', async () => {
|
|
259
|
+
const first = new Node();
|
|
260
|
+
const second = new Node();
|
|
261
|
+
const root = new Node('node', {}, [first, second]);
|
|
262
|
+
const seen: [Node, Node[]][] = [];
|
|
263
|
+
|
|
264
|
+
await Markdoc.validate(root, {
|
|
265
|
+
nodes: {
|
|
266
|
+
node: {
|
|
267
|
+
async validate(node, config) {
|
|
268
|
+
await Promise.resolve();
|
|
269
|
+
seen.push([node, [...config.validation!.parents!]]);
|
|
270
|
+
config.validation!.parents!.push(new Node());
|
|
271
|
+
return [];
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
expectVisits(seen, [
|
|
278
|
+
[root, []],
|
|
279
|
+
[first, [root]],
|
|
280
|
+
[second, [root]],
|
|
281
|
+
]);
|
|
282
|
+
});
|
|
283
|
+
});
|
package/src/formatter.test.ts
CHANGED
|
@@ -186,6 +186,33 @@ paragraph 2
|
|
|
186
186
|
stable(source);
|
|
187
187
|
});
|
|
188
188
|
|
|
189
|
+
it('escapes only the first run of heading marks', () => {
|
|
190
|
+
const examples = {
|
|
191
|
+
'a ## b': 'a \\## b',
|
|
192
|
+
'C# and C# again': 'C\\# and C# again',
|
|
193
|
+
'##x ## y': '##x \\## y',
|
|
194
|
+
'Issue #': 'Issue #',
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
for (const [content, expected] of Object.entries(examples)) {
|
|
198
|
+
const ast = new Markdoc.Ast.Node('paragraph', {}, [
|
|
199
|
+
new Markdoc.Ast.Node('text', { content }),
|
|
200
|
+
]);
|
|
201
|
+
expect(Markdoc.format(ast).trim()).toEqual(expected);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('escapes long runs of heading marks in linear time', () => {
|
|
206
|
+
const content = '#'.repeat(100_000) + 'x';
|
|
207
|
+
const ast = new Markdoc.Ast.Node('paragraph', {}, [
|
|
208
|
+
new Markdoc.Ast.Node('text', { content }),
|
|
209
|
+
]);
|
|
210
|
+
|
|
211
|
+
const start = performance.now();
|
|
212
|
+
expect(Markdoc.format(ast).trim()).toEqual(content);
|
|
213
|
+
expect(performance.now() - start).toBeLessThan(500);
|
|
214
|
+
});
|
|
215
|
+
|
|
189
216
|
it('emphasis marks', () => {
|
|
190
217
|
const examples = [
|
|
191
218
|
'*foo* bar baz',
|
package/src/formatter.ts
CHANGED
|
@@ -224,7 +224,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
224
224
|
yield* escapeMarkdownCharacters(content, /[*_~]/g);
|
|
225
225
|
} else {
|
|
226
226
|
// Escape > blockquote, * list item, and heading
|
|
227
|
-
yield* escapeMarkdownCharacters(content,
|
|
227
|
+
yield* escapeMarkdownCharacters(content, /^\*|(?<!#)#+\s|^>/);
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
|
|
@@ -140,6 +140,26 @@ describe('React dynamic renderer', () => {
|
|
|
140
140
|
});
|
|
141
141
|
|
|
142
142
|
describe('React static renderer', () => {
|
|
143
|
+
it.each([
|
|
144
|
+
[
|
|
145
|
+
'a renamed function',
|
|
146
|
+
function renamedResolver(_name, components) {
|
|
147
|
+
return components.Foo;
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
['an arrow function', (_name, components) => components.Foo],
|
|
151
|
+
])('rendering with %s as the tag resolver', (_label, resolveTagName) => {
|
|
152
|
+
const example = new Tag('Custom', undefined, ['test']);
|
|
153
|
+
const code = renderStatic(example, { resolveTagName });
|
|
154
|
+
const output = eval(code)({ components: { Foo: 'bar' } });
|
|
155
|
+
|
|
156
|
+
expect(output).toDeepEqual({
|
|
157
|
+
name: 'bar',
|
|
158
|
+
attributes: null,
|
|
159
|
+
children: ['test'],
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
143
163
|
it('rendering a null node', () => {
|
|
144
164
|
const example = { name: 'h1', children: ['test', null] };
|
|
145
165
|
const code = renderStatic(example);
|
|
@@ -63,14 +63,10 @@ export default function reactStatic(
|
|
|
63
63
|
node: RenderableTreeNodes,
|
|
64
64
|
{ resolveTagName = tagName }: { resolveTagName?: typeof tagName } = {},
|
|
65
65
|
): string {
|
|
66
|
-
// the
|
|
67
|
-
// throw an error if it does not
|
|
68
|
-
if (resolveTagName.name !== 'tagName') {
|
|
69
|
-
throw new Error('resolveTagName must be named tagName');
|
|
70
|
-
}
|
|
66
|
+
// Bind the serialized resolver explicitly because bundlers can rename it.
|
|
71
67
|
return `
|
|
72
68
|
(({components = {}} = {}) => {
|
|
73
|
-
${resolveTagName}
|
|
69
|
+
const tagName = (${resolveTagName});
|
|
74
70
|
return ${render(node)};
|
|
75
71
|
})
|
|
76
72
|
`;
|
package/src/schema.ts
CHANGED
|
@@ -67,7 +67,7 @@ export const fence: Schema = {
|
|
|
67
67
|
? node.transformChildren(config)
|
|
68
68
|
: [node.attributes.content];
|
|
69
69
|
|
|
70
|
-
return new Tag('pre', attributes, children);
|
|
70
|
+
return new Tag(this.render ?? 'pre', attributes, children);
|
|
71
71
|
},
|
|
72
72
|
};
|
|
73
73
|
|
|
@@ -225,7 +225,9 @@ export const code: Schema = {
|
|
|
225
225
|
},
|
|
226
226
|
transform(node, config) {
|
|
227
227
|
const attributes = node.transformAttributes(config);
|
|
228
|
-
return new Tag('code', attributes, [
|
|
228
|
+
return new Tag(this.render ?? 'code', attributes, [
|
|
229
|
+
node.attributes.content,
|
|
230
|
+
]);
|
|
229
231
|
},
|
|
230
232
|
};
|
|
231
233
|
|
package/src/validator.ts
CHANGED
|
@@ -308,9 +308,36 @@ export function* walkWithParents(
|
|
|
308
308
|
node: Node,
|
|
309
309
|
parents: Node[] = [],
|
|
310
310
|
): Generator<[Node, Node[]]> {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
311
|
+
const stack: {
|
|
312
|
+
node: Node;
|
|
313
|
+
parents: Node[];
|
|
314
|
+
children: Node[];
|
|
315
|
+
index: number;
|
|
316
|
+
}[] = [];
|
|
317
|
+
let current: [Node, Node[]] = [node, parents];
|
|
318
|
+
|
|
319
|
+
while (true) {
|
|
320
|
+
const [currentNode, currentParents] = current;
|
|
321
|
+
yield current;
|
|
322
|
+
|
|
323
|
+
const children = [
|
|
324
|
+
...Object.values(currentNode.slots),
|
|
325
|
+
...currentNode.children,
|
|
326
|
+
];
|
|
327
|
+
if (children.length)
|
|
328
|
+
stack.push({
|
|
329
|
+
node: currentNode,
|
|
330
|
+
parents: currentParents,
|
|
331
|
+
children,
|
|
332
|
+
index: 0,
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
if (!stack.length) return;
|
|
336
|
+
const frame = stack[stack.length - 1];
|
|
337
|
+
// Copy when this child is reached: consumers can mutate yielded parent arrays.
|
|
338
|
+
current = [frame.children[frame.index++], [...frame.parents, frame.node]];
|
|
339
|
+
if (frame.index === frame.children.length) stack.pop();
|
|
340
|
+
}
|
|
314
341
|
}
|
|
315
342
|
|
|
316
343
|
function hasValidLocation(
|