@markdoc/markdoc 0.5.12 → 0.5.14
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 +35 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +472 -448
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +472 -448
- package/dist/index.mjs.map +4 -4
- package/dist/react.js +4 -4
- 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 +2 -2
- 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.ts +21 -6
- package/src/ast/function.ts +1 -1
- package/src/ast/index.ts +3 -4
- package/src/ast/node.test.ts +126 -36
- package/src/ast/node.ts +4 -4
- package/src/ast/variable.test.ts +5 -4
- package/src/ast/variable.ts +1 -1
- package/src/formatter.test.ts +4 -3
- package/src/formatter.ts +11 -11
- 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 +8 -8
- package/src/grammar/tag.test.ts +80 -48
- package/src/parser.test.ts +124 -66
- package/src/parser.ts +29 -7
- package/src/renderers/html.test.ts +56 -16
- package/src/renderers/html.ts +10 -2
- package/src/renderers/react/react.test.ts +39 -27
- package/src/renderers/react/react.ts +9 -10
- package/src/renderers/react/static.ts +5 -6
- package/src/schema-types/class.ts +1 -1
- package/src/schema-types/conditional.ts +1 -1
- package/src/schema.ts +9 -4
- 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 +11 -7
- 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 +205 -31
- package/src/validator.ts +21 -13
package/src/ast/node.test.ts
CHANGED
|
@@ -1,13 +1,90 @@
|
|
|
1
|
-
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import markdoc from '../../index';
|
|
3
|
+
import Tag from '../tag';
|
|
4
|
+
import { getAstValues } from './base';
|
|
5
|
+
import FunctionNode from './function';
|
|
3
6
|
import Node from './node';
|
|
4
7
|
import Variable from './variable';
|
|
5
8
|
|
|
6
|
-
|
|
9
|
+
describe('Node object', () => {
|
|
10
|
+
it('copies only own attributes into a prototype-free dictionary', () => {
|
|
11
|
+
const attributes = Object.assign(Object.create({ inherited: 'value' }), {
|
|
12
|
+
own: 'value',
|
|
13
|
+
});
|
|
14
|
+
const node = new Node('tag', attributes);
|
|
15
|
+
expect(Object.getPrototypeOf(node.attributes)).toBeNull();
|
|
16
|
+
expect(Object.keys(node.attributes)).toEqual(['own']);
|
|
17
|
+
expect(node.attributes.own).toBe('value');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('preserves prototype-free attributes and resolves nested values', () => {
|
|
21
|
+
const attributes = Object.fromEntries([
|
|
22
|
+
['__proto__', { src: new Variable(['url']) }],
|
|
23
|
+
['src', new Variable(['url'])],
|
|
24
|
+
]);
|
|
25
|
+
const node = new Node('tag', attributes);
|
|
26
|
+
const resolved = node.resolve({
|
|
27
|
+
variables: { url: 'https://example.com/' },
|
|
28
|
+
});
|
|
29
|
+
expect(Object.getPrototypeOf(resolved.attributes)).toBeNull();
|
|
30
|
+
expect(Object.keys(resolved.attributes)).toEqual(['__proto__', 'src']);
|
|
31
|
+
// biome-ignore lint/suspicious/noProto: Verify the own __proto__ data property preserved by the security fix.
|
|
32
|
+
expect(resolved.attributes.__proto__.src).toBe('https://example.com/');
|
|
33
|
+
expect(resolved.attributes.src).toBe('https://example.com/');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('traverses AST values in prototype-free attribute dictionaries', () => {
|
|
37
|
+
const variable = new Variable(['url']);
|
|
38
|
+
const node = new Node('tag', { src: variable });
|
|
39
|
+
expect([...getAstValues(node.attributes)]).toEqual([variable]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('resolves object values without invoking prototype setters', () => {
|
|
43
|
+
const value = Object.fromEntries([
|
|
44
|
+
['safe', new Variable(['url'])],
|
|
45
|
+
['__proto__', { src: new Variable(['url']) }],
|
|
46
|
+
]);
|
|
47
|
+
const node = new Node('tag', { value });
|
|
48
|
+
const resolved = node.resolve({
|
|
49
|
+
variables: { url: 'https://example.com/' },
|
|
50
|
+
});
|
|
51
|
+
expect(Object.getPrototypeOf(resolved.attributes.value)).toBe(
|
|
52
|
+
Object.prototype,
|
|
53
|
+
);
|
|
54
|
+
expect(Object.keys(resolved.attributes.value)).toEqual([
|
|
55
|
+
'safe',
|
|
56
|
+
'__proto__',
|
|
57
|
+
]);
|
|
58
|
+
expect(resolved.attributes.value.src).toBeUndefined();
|
|
59
|
+
// biome-ignore lint/suspicious/noProto: Verify the own __proto__ data property preserved by the security fix.
|
|
60
|
+
expect(resolved.attributes.value.__proto__.src).toBe(
|
|
61
|
+
'https://example.com/',
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('resolves serialized function and variable attributes', () => {
|
|
66
|
+
const serialized = JSON.parse(
|
|
67
|
+
JSON.stringify({
|
|
68
|
+
direct: new Variable(['answer']),
|
|
69
|
+
computed: new FunctionNode('identity', {
|
|
70
|
+
0: new Variable(['answer']),
|
|
71
|
+
}),
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
const node = new Node('tag', serialized);
|
|
75
|
+
const resolved = node.resolve({
|
|
76
|
+
variables: { answer: 'resolved' },
|
|
77
|
+
functions: {
|
|
78
|
+
identity: { transform: (parameters) => parameters[0] },
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
expect(resolved.attributes.direct).toBe('resolved');
|
|
83
|
+
expect(resolved.attributes.computed).toBe('resolved');
|
|
84
|
+
});
|
|
7
85
|
|
|
8
|
-
describe('
|
|
9
|
-
|
|
10
|
-
it('with a simple document', function () {
|
|
86
|
+
describe('traversal', () => {
|
|
87
|
+
it('with a simple document', () => {
|
|
11
88
|
const example = new Node('document', {}, [
|
|
12
89
|
new Node('heading', { level: 1 }, [
|
|
13
90
|
new Node('inline', {}, [
|
|
@@ -29,7 +106,7 @@ describe('Node object', function () {
|
|
|
29
106
|
});
|
|
30
107
|
});
|
|
31
108
|
|
|
32
|
-
it('without slots',
|
|
109
|
+
it('without slots', () => {
|
|
33
110
|
const example = `
|
|
34
111
|
{% example %}
|
|
35
112
|
# bar
|
|
@@ -51,7 +128,7 @@ baz
|
|
|
51
128
|
]);
|
|
52
129
|
});
|
|
53
130
|
|
|
54
|
-
it('with slots',
|
|
131
|
+
it('with slots', () => {
|
|
55
132
|
const example = `
|
|
56
133
|
{% example %}
|
|
57
134
|
# bar
|
|
@@ -77,13 +154,13 @@ baz
|
|
|
77
154
|
});
|
|
78
155
|
});
|
|
79
156
|
|
|
80
|
-
describe('transform',
|
|
157
|
+
describe('transform', () => {
|
|
81
158
|
function transform(content, config = {}) {
|
|
82
159
|
return markdoc.transform(content, config);
|
|
83
160
|
}
|
|
84
161
|
|
|
85
|
-
describe('built-in nodes',
|
|
86
|
-
it('a heading node',
|
|
162
|
+
describe('built-in nodes', () => {
|
|
163
|
+
it('a heading node', () => {
|
|
87
164
|
const example = new Node('heading', { level: 1 }, [
|
|
88
165
|
new Node('inline', {}, [
|
|
89
166
|
new Node('text', { content: 'This is a heading' }),
|
|
@@ -94,22 +171,35 @@ describe('transform', function () {
|
|
|
94
171
|
expect(output).toDeepEqual(new Tag('h1', {}, ['This is a heading']));
|
|
95
172
|
});
|
|
96
173
|
|
|
97
|
-
|
|
98
|
-
|
|
174
|
+
it('a heading node with an invalid level', () => {
|
|
175
|
+
const levels = ['1>x', '2', 0, 7, 1.5];
|
|
176
|
+
for (const level of levels) {
|
|
177
|
+
const example = new Node('heading', { level }, [
|
|
178
|
+
new Node('inline', {}, [
|
|
179
|
+
new Node('text', { content: 'This is a heading' }),
|
|
180
|
+
]),
|
|
181
|
+
]);
|
|
182
|
+
|
|
183
|
+
expect(transform(example)).toDeepEqual(['This is a heading']);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe('fenced code blocks', () => {
|
|
188
|
+
it('fenced code block with language', () => {
|
|
99
189
|
const example = new Node('fence', {
|
|
100
190
|
language: 'ruby',
|
|
101
191
|
content: 'test',
|
|
102
192
|
});
|
|
103
193
|
const output = transform(example);
|
|
104
194
|
expect(output).toDeepEqual(
|
|
105
|
-
new Tag('pre', { 'data-language': 'ruby' }, ['test'])
|
|
195
|
+
new Tag('pre', { 'data-language': 'ruby' }, ['test']),
|
|
106
196
|
);
|
|
107
197
|
});
|
|
108
198
|
});
|
|
109
199
|
});
|
|
110
200
|
|
|
111
|
-
describe('transform functions',
|
|
112
|
-
it('with a simple render function',
|
|
201
|
+
describe('transform functions', () => {
|
|
202
|
+
it('with a simple render function', () => {
|
|
113
203
|
const example = new Node('foo', {});
|
|
114
204
|
|
|
115
205
|
const foo = {
|
|
@@ -122,7 +212,7 @@ describe('transform', function () {
|
|
|
122
212
|
expect(output).toDeepEqual(new Tag('foo', {}, []));
|
|
123
213
|
});
|
|
124
214
|
|
|
125
|
-
it('with a render function that renders attributes',
|
|
215
|
+
it('with a render function that renders attributes', () => {
|
|
126
216
|
const example = new Node('foo', { bar: 'baz', test: 100 });
|
|
127
217
|
const foo = {
|
|
128
218
|
attributes: {
|
|
@@ -145,7 +235,7 @@ describe('transform', function () {
|
|
|
145
235
|
});
|
|
146
236
|
});
|
|
147
237
|
|
|
148
|
-
describe('tags',
|
|
238
|
+
describe('tags', () => {
|
|
149
239
|
const tags = {
|
|
150
240
|
foo: {
|
|
151
241
|
render: 'foo',
|
|
@@ -155,32 +245,32 @@ describe('transform', function () {
|
|
|
155
245
|
},
|
|
156
246
|
};
|
|
157
247
|
|
|
158
|
-
it('with a tag',
|
|
248
|
+
it('with a tag', () => {
|
|
159
249
|
const example = new Node(
|
|
160
250
|
'tag',
|
|
161
251
|
{ bar: 'baz' },
|
|
162
252
|
[new Node('inline', {}, [new Node('text', { content: 'test' })])],
|
|
163
|
-
'foo'
|
|
253
|
+
'foo',
|
|
164
254
|
);
|
|
165
255
|
|
|
166
256
|
const output = transform(example, { tags });
|
|
167
257
|
expect(output).toDeepEqual(new Tag('foo', { bar: 'baz' }, ['test']));
|
|
168
258
|
});
|
|
169
259
|
|
|
170
|
-
it('with a non-existing tag',
|
|
260
|
+
it('with a non-existing tag', () => {
|
|
171
261
|
const example = new Node('container', { bar: 'baz' }, [
|
|
172
262
|
new Node('inline', {}, [new Node('text', { content: 'test' })]),
|
|
173
263
|
]);
|
|
174
264
|
|
|
175
|
-
// @ts-
|
|
265
|
+
// @ts-expect-error test mutating node in userland
|
|
176
266
|
example.container = 'bar';
|
|
177
267
|
const output = transform(example, { tags });
|
|
178
268
|
expect(output).toDeepEqual(['test']);
|
|
179
269
|
});
|
|
180
270
|
});
|
|
181
271
|
|
|
182
|
-
describe('tag name',
|
|
183
|
-
it('with a string value',
|
|
272
|
+
describe('tag name', () => {
|
|
273
|
+
it('with a string value', () => {
|
|
184
274
|
const example = new Node('foo', {});
|
|
185
275
|
const foo = { render: 'foo' };
|
|
186
276
|
|
|
@@ -188,7 +278,7 @@ describe('transform', function () {
|
|
|
188
278
|
expect(output.name).toEqual('foo');
|
|
189
279
|
});
|
|
190
280
|
|
|
191
|
-
it('with no value',
|
|
281
|
+
it('with no value', () => {
|
|
192
282
|
const example = new Node('foo', {}, [new Node('bar', {})]);
|
|
193
283
|
|
|
194
284
|
const output = transform(example, {
|
|
@@ -198,8 +288,8 @@ describe('transform', function () {
|
|
|
198
288
|
});
|
|
199
289
|
});
|
|
200
290
|
|
|
201
|
-
describe('attributes',
|
|
202
|
-
it('with an id',
|
|
291
|
+
describe('attributes', () => {
|
|
292
|
+
it('with an id', () => {
|
|
203
293
|
const example = new Node('paragraph', { id: 'bar' });
|
|
204
294
|
const output = transform(example);
|
|
205
295
|
expect(output).toDeepEqualSubset({
|
|
@@ -208,7 +298,7 @@ describe('transform', function () {
|
|
|
208
298
|
});
|
|
209
299
|
});
|
|
210
300
|
|
|
211
|
-
it('with a class',
|
|
301
|
+
it('with a class', () => {
|
|
212
302
|
const example = new Node('paragraph', {
|
|
213
303
|
class: { foo: true, bar: false },
|
|
214
304
|
});
|
|
@@ -216,7 +306,7 @@ describe('transform', function () {
|
|
|
216
306
|
expect(output).toDeepEqual(new Tag('p', { class: 'foo' }, []));
|
|
217
307
|
});
|
|
218
308
|
|
|
219
|
-
it('with boolean render attribute',
|
|
309
|
+
it('with boolean render attribute', () => {
|
|
220
310
|
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
221
311
|
const foo = {
|
|
222
312
|
render: 'foo',
|
|
@@ -230,7 +320,7 @@ describe('transform', function () {
|
|
|
230
320
|
expect(output).toDeepEqual(new Tag('foo', { bar: 1, baz: 'test' }));
|
|
231
321
|
});
|
|
232
322
|
|
|
233
|
-
it('with string render attribute',
|
|
323
|
+
it('with string render attribute', () => {
|
|
234
324
|
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
235
325
|
const foo = {
|
|
236
326
|
render: 'foo',
|
|
@@ -242,11 +332,11 @@ describe('transform', function () {
|
|
|
242
332
|
|
|
243
333
|
const output = transform(example, { nodes: { foo } });
|
|
244
334
|
expect(output).toDeepEqual(
|
|
245
|
-
new Tag('foo', { 'data-bar': 1, 'data-baz': 'test' })
|
|
335
|
+
new Tag('foo', { 'data-bar': 1, 'data-baz': 'test' }),
|
|
246
336
|
);
|
|
247
337
|
});
|
|
248
338
|
|
|
249
|
-
it('with a non-rendered attribute',
|
|
339
|
+
it('with a non-rendered attribute', () => {
|
|
250
340
|
const example = new Node('foo', { bar: 1, baz: 'test' });
|
|
251
341
|
const foo = {
|
|
252
342
|
render: 'foo',
|
|
@@ -256,11 +346,11 @@ describe('transform', function () {
|
|
|
256
346
|
};
|
|
257
347
|
|
|
258
348
|
const output = transform(example, { nodes: { foo } }) as Tag;
|
|
259
|
-
expect(Object.keys(output.attributes).includes('baz')).
|
|
349
|
+
expect(Object.keys(output.attributes).includes('baz')).toBe(false);
|
|
260
350
|
expect(output).toDeepEqual(new Tag('foo', { bar: 1 }, []));
|
|
261
351
|
});
|
|
262
352
|
|
|
263
|
-
it('with a default attribute value',
|
|
353
|
+
it('with a default attribute value', () => {
|
|
264
354
|
const example = new Node('foo', {});
|
|
265
355
|
const foo = {
|
|
266
356
|
render: 'foo',
|
|
@@ -273,7 +363,7 @@ describe('transform', function () {
|
|
|
273
363
|
expect(output.attributes.bar).toEqual(10);
|
|
274
364
|
});
|
|
275
365
|
|
|
276
|
-
it('with an overriden default attribute value',
|
|
366
|
+
it('with an overriden default attribute value', () => {
|
|
277
367
|
const example = new Node('foo', { bar: 1 });
|
|
278
368
|
const foo = {
|
|
279
369
|
render: 'foo',
|
|
@@ -286,7 +376,7 @@ describe('transform', function () {
|
|
|
286
376
|
expect(output.attributes.bar).toEqual(1);
|
|
287
377
|
});
|
|
288
378
|
|
|
289
|
-
it('variable nested in a hash',
|
|
379
|
+
it('variable nested in a hash', () => {
|
|
290
380
|
const bar = new Variable(['a', 'b', 'c']);
|
|
291
381
|
const example = new Node('foo', { bar });
|
|
292
382
|
const foo = {
|
|
@@ -355,7 +445,7 @@ describe('transform', function () {
|
|
|
355
445
|
describe('annotations', () => {
|
|
356
446
|
it('multiple values should be ordered correctly', () => {
|
|
357
447
|
const example = markdoc.parse(
|
|
358
|
-
`\`\`\`js {% z=true .class y=2 x="1" #id %} \nContent\n
|
|
448
|
+
`\`\`\`js {% z=true .class y=2 x="1" #id %} \nContent\n\`\`\``,
|
|
359
449
|
);
|
|
360
450
|
|
|
361
451
|
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,9 +32,9 @@ 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
|
-
this.attributes = attributes;
|
|
37
|
+
this.attributes = Object.assign(Object.create(null), attributes);
|
|
38
38
|
this.children = children;
|
|
39
39
|
this.type = type;
|
|
40
40
|
this.tag = tag;
|
|
@@ -61,7 +61,7 @@ export default class Node implements AstType {
|
|
|
61
61
|
Object.entries(this.slots).map(([name, slot]) => [
|
|
62
62
|
name,
|
|
63
63
|
slot.resolve(config),
|
|
64
|
-
])
|
|
64
|
+
]),
|
|
65
65
|
),
|
|
66
66
|
});
|
|
67
67
|
}
|
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
package/src/formatter.test.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { diff } from 'jest-diff';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
2
3
|
|
|
3
4
|
import Markdoc from '../index';
|
|
4
5
|
import format from './formatter';
|
|
@@ -86,7 +87,7 @@ function check(source, expected, options = {}) {
|
|
|
86
87
|
const b = format(Markdoc.parse(tokenizer.tokenize(source)), options);
|
|
87
88
|
// console.log(a, b);
|
|
88
89
|
const d = diff(a, b);
|
|
89
|
-
if (d
|
|
90
|
+
if (d?.includes('Compared values have no visual difference.')) return;
|
|
90
91
|
throw d;
|
|
91
92
|
}
|
|
92
93
|
|
|
@@ -896,14 +897,14 @@ ${'`'.repeat(4)}
|
|
|
896
897
|
validAttribute: true,
|
|
897
898
|
},
|
|
898
899
|
[],
|
|
899
|
-
'tag'
|
|
900
|
+
'tag',
|
|
900
901
|
);
|
|
901
902
|
|
|
902
903
|
const expected = `{% tag validAttribute=true /%}`;
|
|
903
904
|
|
|
904
905
|
const b = format(sourceNode);
|
|
905
906
|
const d = diff(expected, b.trim());
|
|
906
|
-
if (d
|
|
907
|
+
if (d?.includes('Compared values have no visual difference.')) return;
|
|
907
908
|
});
|
|
908
909
|
|
|
909
910
|
it('makes sure fences are formatted correctly if content has no ending newline', () => {
|
package/src/formatter.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Ast from './ast';
|
|
2
|
-
import { OPEN, CLOSE, isIdentifier } from './utils';
|
|
3
2
|
import type { AttributeValue, Function, Node, Value, Variable } from './types';
|
|
3
|
+
import { CLOSE, isIdentifier, OPEN } from './utils';
|
|
4
4
|
|
|
5
5
|
type Options = {
|
|
6
6
|
allowIndentation?: boolean;
|
|
@@ -59,7 +59,7 @@ function formatScalar(v: Value): string | undefined {
|
|
|
59
59
|
Object.entries(v)
|
|
60
60
|
.map(
|
|
61
61
|
([key, value]) =>
|
|
62
|
-
`${isIdentifier(key) ? key : `"${key}"`}: ${formatScalar(value)}
|
|
62
|
+
`${isIdentifier(key) ? key : `"${key}"`}: ${formatScalar(value)}`,
|
|
63
63
|
)
|
|
64
64
|
.join(SEP) +
|
|
65
65
|
'}'
|
|
@@ -122,7 +122,7 @@ function* formatFunction(f: Function) {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
function* trimStart(g: Generator<string>) {
|
|
125
|
-
let n;
|
|
125
|
+
let n: string;
|
|
126
126
|
do {
|
|
127
127
|
const { value, done } = g.next();
|
|
128
128
|
if (done) return;
|
|
@@ -137,7 +137,7 @@ function* escapeMarkdownCharacters(s: string, characters: RegExp) {
|
|
|
137
137
|
.replace(characters, '\\$&')
|
|
138
138
|
// TODO keep as entity in the AST?
|
|
139
139
|
// Non-breaking space (0xA0)
|
|
140
|
-
.replace(
|
|
140
|
+
.replace(/\xa0/g, ' ');
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
function* formatNode(n: Node, o: Options = {}) {
|
|
@@ -146,7 +146,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
146
146
|
|
|
147
147
|
switch (n.type) {
|
|
148
148
|
case 'document': {
|
|
149
|
-
if (n.attributes.frontmatter
|
|
149
|
+
if (n.attributes.frontmatter?.length) {
|
|
150
150
|
yield '---' + NL + n.attributes.frontmatter + NL + '---' + NL + NL;
|
|
151
151
|
}
|
|
152
152
|
yield* trimStart(formatChildren(n, no));
|
|
@@ -279,7 +279,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
279
279
|
}
|
|
280
280
|
const open = OPEN + SPACE;
|
|
281
281
|
const attributes = [...formatAttributes(n)].filter(
|
|
282
|
-
(v) => v !== undefined
|
|
282
|
+
(v) => v !== undefined,
|
|
283
283
|
);
|
|
284
284
|
const tag = [open + n.tag, ...attributes];
|
|
285
285
|
const inlineTag = tag.join(SPACE);
|
|
@@ -311,7 +311,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
311
311
|
}
|
|
312
312
|
case 'list': {
|
|
313
313
|
const isLoose = n.children.some((n) =>
|
|
314
|
-
n.children.some((c) => c.type === 'paragraph')
|
|
314
|
+
n.children.some((c) => c.type === 'paragraph'),
|
|
315
315
|
);
|
|
316
316
|
|
|
317
317
|
for (let i = 0; i < n.children.length; i++) {
|
|
@@ -418,14 +418,14 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
418
418
|
|
|
419
419
|
yield NL;
|
|
420
420
|
yield* formatTableRow(
|
|
421
|
-
head.map((cell, i) => cell + SPACE.repeat(widths[i] - cell.length))
|
|
421
|
+
head.map((cell, i) => cell + SPACE.repeat(widths[i] - cell.length)),
|
|
422
422
|
);
|
|
423
423
|
yield NL;
|
|
424
|
-
yield* formatTableRow(head.map((
|
|
424
|
+
yield* formatTableRow(head.map((_cell, i) => '-'.repeat(widths[i])));
|
|
425
425
|
yield NL;
|
|
426
426
|
for (const row of rows) {
|
|
427
427
|
yield* formatTableRow(
|
|
428
|
-
row.map((cell, i) => cell + SPACE.repeat(widths[i] - cell.length))
|
|
428
|
+
row.map((cell, i) => cell + SPACE.repeat(widths[i] - cell.length)),
|
|
429
429
|
);
|
|
430
430
|
yield NL;
|
|
431
431
|
}
|
|
@@ -462,7 +462,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
462
462
|
|
|
463
463
|
function* formatValue(
|
|
464
464
|
v: Value | Value[],
|
|
465
|
-
o: Options = {}
|
|
465
|
+
o: Options = {},
|
|
466
466
|
): Generator<string, void, unknown> {
|
|
467
467
|
switch (typeof v) {
|
|
468
468
|
case 'undefined':
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import Ast from '../ast';
|
|
3
|
+
import functions from './index';
|
|
3
4
|
|
|
4
|
-
describe('built-in functions',
|
|
5
|
-
describe('equals function',
|
|
6
|
-
it('true match with variable',
|
|
5
|
+
describe('built-in functions', () => {
|
|
6
|
+
describe('equals function', () => {
|
|
7
|
+
it('true match with variable', () => {
|
|
7
8
|
const example = new Ast.Function('equals', {
|
|
8
9
|
0: new Ast.Variable(['foo']),
|
|
9
10
|
1: 'bar',
|
|
@@ -14,10 +15,10 @@ describe('built-in functions', function () {
|
|
|
14
15
|
variables: { foo: 'bar' },
|
|
15
16
|
});
|
|
16
17
|
|
|
17
|
-
expect(output).
|
|
18
|
+
expect(output).toBe(true);
|
|
18
19
|
});
|
|
19
20
|
|
|
20
|
-
it('false match with variable',
|
|
21
|
+
it('false match with variable', () => {
|
|
21
22
|
const example = new Ast.Function('equals', {
|
|
22
23
|
0: new Ast.Variable(['foo']),
|
|
23
24
|
1: 'bar',
|
|
@@ -28,7 +29,7 @@ describe('built-in functions', function () {
|
|
|
28
29
|
variables: { foo: 'baz' },
|
|
29
30
|
});
|
|
30
31
|
|
|
31
|
-
expect(output).
|
|
32
|
+
expect(output).toBe(false);
|
|
32
33
|
});
|
|
33
34
|
});
|
|
34
35
|
});
|
package/src/functions/index.ts
CHANGED
package/src/grammar/tag.d.ts
CHANGED