@markdoc/markdoc 0.1.4 → 0.1.7
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.d.ts +14 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6787 -6409
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +6787 -6409
- package/dist/index.mjs.map +3 -3
- package/dist/src/ast/base.d.ts +2 -1
- package/dist/src/ast/base.d.ts.map +1 -1
- package/dist/src/ast/index.d.ts +1 -0
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts +4 -4
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/formatter.d.ts +10 -0
- package/dist/src/formatter.d.ts.map +1 -0
- package/dist/src/schema.d.ts +1 -0
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tag.d.ts +9 -0
- package/dist/src/tag.d.ts.map +1 -0
- package/dist/src/tags/conditional.d.ts.map +1 -1
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/transforms/table.d.ts.map +1 -1
- package/dist/src/types.d.ts +7 -5
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils.d.ts +1 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/index.ts +12 -9
- package/package.json +2 -1
- package/src/ast/base.ts +5 -0
- package/src/ast/node.test.ts +21 -21
- package/src/ast/node.ts +6 -4
- package/src/formatter.test.ts +475 -0
- package/src/formatter.ts +391 -0
- package/src/schema.ts +3 -2
- package/src/tag.ts +22 -0
- package/src/tags/conditional.ts +10 -3
- package/src/transformer.ts +2 -5
- package/src/transforms/table.ts +2 -2
- package/src/types.ts +25 -20
- package/src/utils.ts +4 -0
- package/src/validator.test.ts +80 -18
- package/src/validator.ts +22 -3
- package/dist/src/ast/tag.d.ts +0 -9
- package/dist/src/ast/tag.d.ts.map +0 -1
- package/src/ast/tag.ts +0 -19
package/src/formatter.ts
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import Ast from './ast';
|
|
2
|
+
import { OPEN, CLOSE } from './utils';
|
|
3
|
+
import type { AttributeValue, Function, Node, Value, Variable } from './types';
|
|
4
|
+
|
|
5
|
+
type Options = {
|
|
6
|
+
allowIndentation?: boolean;
|
|
7
|
+
maxTagOpeningWidth?: number;
|
|
8
|
+
parent?: Node;
|
|
9
|
+
indent?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const SPACE = ' ';
|
|
13
|
+
const SEP = ', '; // Value separator
|
|
14
|
+
const NL = '\n'; // Newline
|
|
15
|
+
const OL = '1. '; // Ordered list
|
|
16
|
+
const UL = '- '; // Unordered list
|
|
17
|
+
|
|
18
|
+
const MAX_TAG_OPENING_WIDTH = 80;
|
|
19
|
+
|
|
20
|
+
const max = (a: number, b: number) => Math.max(a, b);
|
|
21
|
+
const increment = (o: Options, n = 2) => ({
|
|
22
|
+
...o,
|
|
23
|
+
indent: (o.indent || 0) + n,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
function* formatChildren(a: Node, options: Options) {
|
|
27
|
+
for (const child of a.children) {
|
|
28
|
+
yield* formatValue(child, options);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function* formatTableRow(items: Array<string>) {
|
|
33
|
+
yield `| ${items.join(' | ')} |`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function formatScalar(v: Value): string {
|
|
37
|
+
if (Ast.isAst(v)) {
|
|
38
|
+
return format(v);
|
|
39
|
+
}
|
|
40
|
+
if (v === null) {
|
|
41
|
+
return '';
|
|
42
|
+
}
|
|
43
|
+
if (Array.isArray(v)) {
|
|
44
|
+
return '[' + v.map(formatScalar).join(SEP) + ']';
|
|
45
|
+
}
|
|
46
|
+
if (typeof v === 'object') {
|
|
47
|
+
return (
|
|
48
|
+
'{' +
|
|
49
|
+
Object.entries(v)
|
|
50
|
+
.map(([key, value]) => `${key}: ${formatScalar(value)}`)
|
|
51
|
+
.join(SEP) +
|
|
52
|
+
'}'
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return JSON.stringify(v);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function formatAnnotationValue(a: AttributeValue): string {
|
|
59
|
+
if (a.name === 'primary') return formatScalar(a.value);
|
|
60
|
+
if (a.name === 'id' && typeof a.value === 'string') return '#' + a.value;
|
|
61
|
+
if (a.type === 'class') return '.' + a.name;
|
|
62
|
+
return `${a.name}=${formatScalar(a.value)}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function* formatAttributes(n: Node) {
|
|
66
|
+
for (const [key, value] of Object.entries(n.attributes)) {
|
|
67
|
+
if (key === 'class' && !Ast.isAst(value))
|
|
68
|
+
for (const name of Object.keys(value)) {
|
|
69
|
+
yield formatAnnotationValue({ type: 'class', name, value });
|
|
70
|
+
}
|
|
71
|
+
else yield formatAnnotationValue({ type: 'attribute', name: key, value });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function* formatAnnotations(n: Node) {
|
|
76
|
+
if (n.annotations.length) {
|
|
77
|
+
yield OPEN + SPACE;
|
|
78
|
+
yield n.annotations.map(formatAnnotationValue).join(SPACE);
|
|
79
|
+
yield SPACE + CLOSE;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function* formatVariable(v: Variable) {
|
|
84
|
+
yield '$';
|
|
85
|
+
yield v.path.join('.');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function* formatFunction(f: Function) {
|
|
89
|
+
yield f.name;
|
|
90
|
+
yield '(';
|
|
91
|
+
yield Object.values(f.parameters).map(formatScalar).join(SEP);
|
|
92
|
+
yield ')';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function* trimStart(g: Generator) {
|
|
96
|
+
let n;
|
|
97
|
+
do {
|
|
98
|
+
n = g.next().value.trimStart();
|
|
99
|
+
} while (!n.length);
|
|
100
|
+
yield n;
|
|
101
|
+
yield* g;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function* formatNode(n: Node, o: Options = {}) {
|
|
105
|
+
const no = { ...o, parent: n };
|
|
106
|
+
const indent = SPACE.repeat(no.indent || 0);
|
|
107
|
+
|
|
108
|
+
switch (n.type) {
|
|
109
|
+
case 'document': {
|
|
110
|
+
if (n.attributes.frontmatter && n.attributes.frontmatter.length) {
|
|
111
|
+
yield '---' + NL + n.attributes.frontmatter + NL + '---' + NL + NL;
|
|
112
|
+
}
|
|
113
|
+
yield* trimStart(formatChildren(n, no));
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case 'heading': {
|
|
117
|
+
yield NL;
|
|
118
|
+
yield indent;
|
|
119
|
+
yield '#'.repeat(n.attributes.level || 1);
|
|
120
|
+
yield SPACE;
|
|
121
|
+
yield* formatChildren(n, no);
|
|
122
|
+
yield* formatAnnotations(n);
|
|
123
|
+
yield NL;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case 'paragraph': {
|
|
127
|
+
yield NL;
|
|
128
|
+
yield indent;
|
|
129
|
+
yield* formatChildren(n, no);
|
|
130
|
+
yield* formatAnnotations(n);
|
|
131
|
+
yield NL;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 'inline': {
|
|
135
|
+
yield* formatChildren(n, no);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case 'link': {
|
|
139
|
+
yield '[';
|
|
140
|
+
yield* formatChildren(n, no);
|
|
141
|
+
yield ']';
|
|
142
|
+
yield '(';
|
|
143
|
+
yield* formatValue(n.attributes.href, no);
|
|
144
|
+
yield ')';
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case 'text': {
|
|
148
|
+
if (Ast.isAst(n.attributes.content)) yield OPEN + SPACE;
|
|
149
|
+
yield* formatValue(n.attributes.content, no);
|
|
150
|
+
if (Ast.isAst(n.attributes.content)) yield SPACE + CLOSE;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case 'blockquote': {
|
|
154
|
+
yield NL;
|
|
155
|
+
yield indent;
|
|
156
|
+
yield '> ';
|
|
157
|
+
yield* trimStart(formatChildren(n, no));
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
case 'hr': {
|
|
161
|
+
yield NL;
|
|
162
|
+
yield indent;
|
|
163
|
+
yield '---';
|
|
164
|
+
yield NL;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
case 'image': {
|
|
168
|
+
yield '!';
|
|
169
|
+
yield '[';
|
|
170
|
+
yield* formatValue(n.attributes.alt, no);
|
|
171
|
+
yield ']';
|
|
172
|
+
yield '(';
|
|
173
|
+
yield* formatValue(n.attributes.src, no);
|
|
174
|
+
yield ')';
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case 'fence': {
|
|
178
|
+
yield NL;
|
|
179
|
+
yield indent;
|
|
180
|
+
yield '```';
|
|
181
|
+
yield (n.attributes.language || '').toLowerCase();
|
|
182
|
+
if (n.annotations.length) yield SPACE;
|
|
183
|
+
yield* formatAnnotations(n);
|
|
184
|
+
yield NL;
|
|
185
|
+
yield indent;
|
|
186
|
+
yield* formatChildren(n, no);
|
|
187
|
+
yield indent;
|
|
188
|
+
yield '```';
|
|
189
|
+
yield NL;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
case 'tag': {
|
|
193
|
+
if (!n.inline) {
|
|
194
|
+
yield NL;
|
|
195
|
+
yield indent;
|
|
196
|
+
}
|
|
197
|
+
const open = OPEN + SPACE;
|
|
198
|
+
const tag = [open + n.tag, ...formatAttributes(n)];
|
|
199
|
+
const inlineTag = tag.join(SPACE);
|
|
200
|
+
|
|
201
|
+
const isLongTagOpening =
|
|
202
|
+
inlineTag.length + open.length * 2 >
|
|
203
|
+
(o.maxTagOpeningWidth || MAX_TAG_OPENING_WIDTH);
|
|
204
|
+
|
|
205
|
+
// {% tag attributes={...} %}
|
|
206
|
+
yield (isLongTagOpening
|
|
207
|
+
? tag.join(NL + SPACE.repeat(open.length) + indent)
|
|
208
|
+
: inlineTag) +
|
|
209
|
+
SPACE +
|
|
210
|
+
(n.children.length ? '' : '/') +
|
|
211
|
+
CLOSE;
|
|
212
|
+
|
|
213
|
+
if (n.children.length) {
|
|
214
|
+
yield* formatChildren(n, no.allowIndentation ? increment(no) : no);
|
|
215
|
+
if (!n.inline) {
|
|
216
|
+
yield indent;
|
|
217
|
+
}
|
|
218
|
+
// {% /tag %}
|
|
219
|
+
yield OPEN + SPACE + '/' + n.tag + SPACE + CLOSE;
|
|
220
|
+
}
|
|
221
|
+
if (!n.inline) {
|
|
222
|
+
yield NL;
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
case 'list': {
|
|
227
|
+
const prefix = n.attributes.ordered ? OL : UL;
|
|
228
|
+
yield NL;
|
|
229
|
+
for (const child of n.children) {
|
|
230
|
+
const d = format(child, increment(no, prefix.length)).trim();
|
|
231
|
+
yield indent + prefix + d;
|
|
232
|
+
yield NL;
|
|
233
|
+
}
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
case 'item': {
|
|
237
|
+
yield* formatChildren(n, no);
|
|
238
|
+
yield* formatAnnotations(n);
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
case 'strong': {
|
|
242
|
+
yield '**';
|
|
243
|
+
yield* formatChildren(n, no);
|
|
244
|
+
yield '**';
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case 'em': {
|
|
248
|
+
yield '_';
|
|
249
|
+
yield* formatChildren(n, no);
|
|
250
|
+
yield '_';
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
case 'code': {
|
|
254
|
+
yield '`';
|
|
255
|
+
yield* formatValue(n.attributes.content, no);
|
|
256
|
+
yield '`';
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
case 's': {
|
|
260
|
+
yield '~~';
|
|
261
|
+
yield* formatChildren(n, no);
|
|
262
|
+
yield '~~';
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
case 'hardbreak': {
|
|
266
|
+
yield '\\' + NL;
|
|
267
|
+
yield indent;
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
case 'softbreak': {
|
|
271
|
+
yield NL;
|
|
272
|
+
yield indent;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
case 'table': {
|
|
276
|
+
const table = [...formatChildren(n, increment(no))] as any as any[];
|
|
277
|
+
if (o.parent && o.parent.type === 'tag' && o.parent.tag === 'table') {
|
|
278
|
+
for (let i = 0; i < table.length; i++) {
|
|
279
|
+
const row = table[i];
|
|
280
|
+
// format tags like "if" in the middle of a table list
|
|
281
|
+
if (typeof row === 'string') {
|
|
282
|
+
if (row.trim().length) {
|
|
283
|
+
yield NL;
|
|
284
|
+
yield row;
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
if (i !== 0) {
|
|
288
|
+
yield NL;
|
|
289
|
+
yield indent + '---';
|
|
290
|
+
}
|
|
291
|
+
for (let j = 0; j < row.length; j++) {
|
|
292
|
+
const d = row[j];
|
|
293
|
+
yield NL;
|
|
294
|
+
// TODO see if we should move trim() to `td`
|
|
295
|
+
yield indent + UL + d.trim();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
yield NL;
|
|
300
|
+
} else {
|
|
301
|
+
yield NL;
|
|
302
|
+
const [head, ...rows] = table as string[][];
|
|
303
|
+
|
|
304
|
+
const ml = table
|
|
305
|
+
.map((arr) => arr.map((s: string) => s.length).reduce(max))
|
|
306
|
+
.reduce(max);
|
|
307
|
+
|
|
308
|
+
yield* formatTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
|
|
309
|
+
yield NL;
|
|
310
|
+
yield* formatTableRow(head.map(() => '-'.repeat(ml)));
|
|
311
|
+
yield NL;
|
|
312
|
+
for (const row of rows) {
|
|
313
|
+
yield* formatTableRow(
|
|
314
|
+
row.map((r) => r + SPACE.repeat(ml - r.length))
|
|
315
|
+
);
|
|
316
|
+
yield NL;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
case 'thead': {
|
|
322
|
+
const [head] = [...formatChildren(n, no)];
|
|
323
|
+
yield head || [];
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
case 'tr': {
|
|
327
|
+
yield [...formatChildren(n, no)];
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
case 'td':
|
|
331
|
+
case 'th': {
|
|
332
|
+
yield [...formatChildren(n, no), ...formatAnnotations(n)].join('');
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
case 'tbody': {
|
|
336
|
+
yield* formatChildren(n, no);
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
case 'error': {
|
|
340
|
+
console.error(n);
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case 'node':
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function* formatValue(
|
|
349
|
+
v: Value,
|
|
350
|
+
o: Options = {}
|
|
351
|
+
): Generator<string, void, unknown> {
|
|
352
|
+
switch (typeof v) {
|
|
353
|
+
case 'undefined':
|
|
354
|
+
break;
|
|
355
|
+
case 'boolean':
|
|
356
|
+
case 'number':
|
|
357
|
+
case 'string': {
|
|
358
|
+
yield v.toString();
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case 'object': {
|
|
362
|
+
if (v === null) break;
|
|
363
|
+
if (Array.isArray(v)) {
|
|
364
|
+
for (const n of v) yield* formatValue(n, o);
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
switch (v.$$mdtype) {
|
|
368
|
+
case 'Function': {
|
|
369
|
+
yield* formatFunction(v as Function);
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
case 'Node':
|
|
373
|
+
yield* formatNode(v as Node, o);
|
|
374
|
+
break;
|
|
375
|
+
case 'Variable': {
|
|
376
|
+
yield* formatVariable(v as Variable);
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
default:
|
|
380
|
+
throw new Error(`Unimplemented: "${v.$$mdtype}"`);
|
|
381
|
+
}
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export default function format(v: Value, options?: Options): string {
|
|
388
|
+
let doc = '';
|
|
389
|
+
for (const s of formatValue(v, options)) doc += s;
|
|
390
|
+
return doc.trimStart();
|
|
391
|
+
}
|
package/src/schema.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Schema } from './types';
|
|
2
|
-
import Tag from './
|
|
2
|
+
import Tag from './tag';
|
|
3
3
|
|
|
4
4
|
export const document: Schema = {
|
|
5
5
|
render: 'article',
|
|
@@ -226,9 +226,10 @@ export const hardbreak: Schema = {
|
|
|
226
226
|
};
|
|
227
227
|
|
|
228
228
|
export const softbreak: Schema = {
|
|
229
|
-
transform(
|
|
229
|
+
transform() {
|
|
230
230
|
return ' ';
|
|
231
231
|
},
|
|
232
232
|
};
|
|
233
233
|
|
|
234
234
|
export const error = {};
|
|
235
|
+
export const node = {};
|
package/src/tag.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { RenderableTreeNode } from './types';
|
|
2
|
+
|
|
3
|
+
export default class Tag<
|
|
4
|
+
N extends string = string,
|
|
5
|
+
A extends Record<string, any> = Record<string, any>
|
|
6
|
+
> {
|
|
7
|
+
readonly $$mdtype = 'Tag' as const;
|
|
8
|
+
|
|
9
|
+
name: N;
|
|
10
|
+
attributes: A;
|
|
11
|
+
children: RenderableTreeNode[];
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
name = 'div' as N,
|
|
15
|
+
attributes = {} as A,
|
|
16
|
+
children: RenderableTreeNode[] = []
|
|
17
|
+
) {
|
|
18
|
+
this.name = name;
|
|
19
|
+
this.attributes = attributes;
|
|
20
|
+
this.children = children;
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/tags/conditional.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { isPromise } from '../utils';
|
|
2
|
+
|
|
3
|
+
import type { Node, RenderableTreeNode, Schema, Value } from '../types';
|
|
2
4
|
|
|
3
5
|
type Condition = { condition: Value; children: Node[] };
|
|
4
6
|
|
|
@@ -31,8 +33,13 @@ export const tagIf: Schema = {
|
|
|
31
33
|
transform(node, config) {
|
|
32
34
|
const conditions = renderConditions(node);
|
|
33
35
|
for (const { condition, children } of conditions)
|
|
34
|
-
if (truthy(condition))
|
|
35
|
-
|
|
36
|
+
if (truthy(condition)) {
|
|
37
|
+
const nodes = children.flatMap((child) => child.transform(config));
|
|
38
|
+
if (nodes.some(isPromise)) {
|
|
39
|
+
return Promise.all(nodes).then((nodes) => nodes.flat());
|
|
40
|
+
}
|
|
41
|
+
return nodes as RenderableTreeNode[];
|
|
42
|
+
}
|
|
36
43
|
return [];
|
|
37
44
|
},
|
|
38
45
|
};
|
package/src/transformer.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import Tag from './
|
|
1
|
+
import Tag from './tag';
|
|
2
2
|
import { Class } from './schema-types/class';
|
|
3
|
+
import { isPromise } from './utils';
|
|
3
4
|
import type { Config, Node, NodeType, Schema, Transformer } from './types';
|
|
4
5
|
|
|
5
6
|
type AttributesSchema = Schema['attributes'];
|
|
6
7
|
|
|
7
|
-
function isPromise(a: any): a is Promise<any> {
|
|
8
|
-
return a && typeof a === 'object' && typeof a.then === 'function';
|
|
9
|
-
}
|
|
10
|
-
|
|
11
8
|
export const globalAttributes: AttributesSchema = {
|
|
12
9
|
class: { type: Class, render: true },
|
|
13
10
|
id: { type: String, render: true },
|
package/src/transforms/table.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import Ast from '../ast/index';
|
|
2
2
|
|
|
3
|
-
import type { Node } from '../types';
|
|
3
|
+
import type { Node, NodeType } from '../types';
|
|
4
4
|
|
|
5
|
-
function convertToRow(node: Node, cellType = 'td') {
|
|
5
|
+
function convertToRow(node: Node, cellType: NodeType = 'td') {
|
|
6
6
|
node.type = 'tr';
|
|
7
7
|
node.attributes = {};
|
|
8
8
|
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type Func from './ast/function';
|
|
2
2
|
import type Node from './ast/node';
|
|
3
|
-
import type
|
|
3
|
+
import type Variable from './ast/variable';
|
|
4
|
+
import type Tag from './tag';
|
|
4
5
|
|
|
5
|
-
export type { Node, Tag };
|
|
6
|
+
export type { Node, Tag, Variable };
|
|
6
7
|
export declare type Function = Func;
|
|
7
8
|
|
|
9
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
10
|
+
|
|
8
11
|
export interface AstType {
|
|
9
12
|
readonly $$mdtype: 'Function' | 'Node' | 'Variable';
|
|
10
13
|
resolve(config: Config): any;
|
|
@@ -58,31 +61,33 @@ export type LocationEdge = {
|
|
|
58
61
|
};
|
|
59
62
|
|
|
60
63
|
export type NodeType =
|
|
64
|
+
| 'blockquote'
|
|
65
|
+
| 'code'
|
|
61
66
|
| 'document'
|
|
67
|
+
| 'em'
|
|
68
|
+
| 'error'
|
|
69
|
+
| 'fence'
|
|
70
|
+
| 'hardbreak'
|
|
62
71
|
| 'heading'
|
|
63
|
-
| 'paragraph'
|
|
64
|
-
| 'blockquote'
|
|
65
72
|
| 'hr'
|
|
66
73
|
| 'image'
|
|
67
|
-
| '
|
|
68
|
-
| 'tag'
|
|
69
|
-
| 'list'
|
|
74
|
+
| 'inline'
|
|
70
75
|
| 'item'
|
|
76
|
+
| 'link'
|
|
77
|
+
| 'list'
|
|
78
|
+
| 'node'
|
|
79
|
+
| 'paragraph'
|
|
80
|
+
| 's'
|
|
81
|
+
| 'softbreak'
|
|
82
|
+
| 'strong'
|
|
71
83
|
| 'table'
|
|
72
|
-
| '
|
|
84
|
+
| 'tag'
|
|
73
85
|
| 'tbody'
|
|
74
|
-
| 'tr'
|
|
75
86
|
| 'td'
|
|
76
|
-
| 'th'
|
|
77
|
-
| 'inline'
|
|
78
|
-
| 'strong'
|
|
79
|
-
| 'em'
|
|
80
|
-
| 's'
|
|
81
|
-
| 'link'
|
|
82
|
-
| 'code'
|
|
83
87
|
| 'text'
|
|
84
|
-
| '
|
|
85
|
-
| '
|
|
88
|
+
| 'th'
|
|
89
|
+
| 'thead'
|
|
90
|
+
| 'tr';
|
|
86
91
|
|
|
87
92
|
export type Primitive = null | boolean | number | string;
|
|
88
93
|
|
|
@@ -96,7 +101,7 @@ export type Schema<C extends Config = Config, R = string> = {
|
|
|
96
101
|
children?: string[];
|
|
97
102
|
attributes?: Record<string, SchemaAttribute>;
|
|
98
103
|
selfClosing?: boolean;
|
|
99
|
-
transform?(node: Node, config: C): RenderableTreeNodes
|
|
104
|
+
transform?(node: Node, config: C): MaybePromise<RenderableTreeNodes>;
|
|
100
105
|
validate?(node: Node, config: C): ValidationError[];
|
|
101
106
|
};
|
|
102
107
|
|
|
@@ -113,7 +118,7 @@ export type SchemaMatches = RegExp | string[] | null;
|
|
|
113
118
|
|
|
114
119
|
export interface Transformer {
|
|
115
120
|
findSchema(node: Node, config: Config): Schema | undefined;
|
|
116
|
-
node(node: Node, config: Config): RenderableTreeNodes
|
|
121
|
+
node(node: Node, config: Config): MaybePromise<RenderableTreeNodes>;
|
|
117
122
|
attributes(node: Node, config: Config): Record<string, any>;
|
|
118
123
|
children(node: Node, config: Config): RenderableTreeNode[];
|
|
119
124
|
}
|
package/src/utils.ts
CHANGED
|
@@ -13,6 +13,10 @@ enum STATES {
|
|
|
13
13
|
export const OPEN = '{%';
|
|
14
14
|
export const CLOSE = '%}';
|
|
15
15
|
|
|
16
|
+
export function isPromise(a: any): a is Promise<any> {
|
|
17
|
+
return a && typeof a === 'object' && typeof a.then === 'function';
|
|
18
|
+
}
|
|
19
|
+
|
|
16
20
|
export function findTagEnd(content: string, start = 0) {
|
|
17
21
|
let state = STATES.normal;
|
|
18
22
|
for (let pos = start; pos < content.length; pos++) {
|
package/src/validator.test.ts
CHANGED
|
@@ -232,6 +232,52 @@ describe('validate', function () {
|
|
|
232
232
|
});
|
|
233
233
|
});
|
|
234
234
|
|
|
235
|
+
describe('attribute validation', () => {
|
|
236
|
+
it('should return error on failure to match array', () => {
|
|
237
|
+
const example = '{% foo jawn="cat" /%}';
|
|
238
|
+
const schema = {
|
|
239
|
+
tags: {
|
|
240
|
+
foo: {
|
|
241
|
+
attributes: {
|
|
242
|
+
jawn: {
|
|
243
|
+
type: String,
|
|
244
|
+
matches: ['bar', 'baz', 'bat'],
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
expect(validate(example, schema)).toDeepEqualSubset([
|
|
252
|
+
{
|
|
253
|
+
type: 'tag',
|
|
254
|
+
error: {
|
|
255
|
+
id: 'attribute-value-invalid',
|
|
256
|
+
message: `Attribute 'jawn' must match one of ["bar","baz","bat"]. Got 'cat' instead.`,
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
]);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// https://github.com/markdoc/markdoc/issues/122
|
|
263
|
+
it('should validate partial file attributes', () => {
|
|
264
|
+
const example = `{% partial file="non-existent.md" /%}`;
|
|
265
|
+
const output = validate(example, {});
|
|
266
|
+
|
|
267
|
+
expect(output).toDeepEqualSubset([
|
|
268
|
+
{
|
|
269
|
+
type: 'tag',
|
|
270
|
+
error: {
|
|
271
|
+
id: 'attribute-value-invalid',
|
|
272
|
+
level: 'error',
|
|
273
|
+
message:
|
|
274
|
+
"Partial `non-existent.md` not found. The 'file' attribute must be set in `config.partials`",
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
]);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
235
281
|
describe('custom type registration example', () => {
|
|
236
282
|
class Link {
|
|
237
283
|
validate(value) {
|
|
@@ -296,6 +342,40 @@ describe('validate', function () {
|
|
|
296
342
|
});
|
|
297
343
|
});
|
|
298
344
|
|
|
345
|
+
describe('variable validation', () => {
|
|
346
|
+
it('should only validate if the variables config is passed', () => {
|
|
347
|
+
const example = `{% $valid.variable %}`;
|
|
348
|
+
const output = validate(example, {});
|
|
349
|
+
|
|
350
|
+
expect(output).toEqual([]);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('should warn against missing variables', () => {
|
|
354
|
+
const example = `{% $undefinedVariable %}`;
|
|
355
|
+
const output = validate(example, { variables: {} });
|
|
356
|
+
|
|
357
|
+
expect(output).toDeepEqualSubset([
|
|
358
|
+
{
|
|
359
|
+
type: 'text',
|
|
360
|
+
error: {
|
|
361
|
+
id: 'variable-undefined',
|
|
362
|
+
level: 'error',
|
|
363
|
+
message: "Undefined variable: 'undefinedVariable'",
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
]);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('should not warn if variable exists', () => {
|
|
370
|
+
const example = `{% $valid.variable %}`;
|
|
371
|
+
const output = validate(example, {
|
|
372
|
+
variables: { valid: { variable: false } },
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
expect(output).toEqual([]);
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
|
|
299
379
|
it('should not error for missing support for code_block', () => {
|
|
300
380
|
const example = ` # https://spec.commonmark.org/0.30/#indented-code-block
|
|
301
381
|
4-space indented code`;
|
|
@@ -303,22 +383,4 @@ describe('validate', function () {
|
|
|
303
383
|
|
|
304
384
|
expect(output).toEqual([]);
|
|
305
385
|
});
|
|
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
|
-
});
|
|
324
386
|
});
|