@markdoc/markdoc 0.1.4 → 0.1.5
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 +6785 -6408
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +6785 -6408
- 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 +9 -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/{ast/tag.d.ts → tag.d.ts} +1 -1
- 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 +403 -0
- package/src/formatter.ts +377 -0
- package/src/schema.ts +2 -1
- package/src/{ast/tag.ts → tag.ts} +1 -1
- 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 +34 -0
- package/src/validator.ts +20 -1
- package/dist/src/ast/tag.d.ts.map +0 -1
package/src/formatter.ts
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
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
|
+
parent?: Node;
|
|
8
|
+
indent?: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const SPACE = ' ';
|
|
12
|
+
const NL = '\n'; // Newline
|
|
13
|
+
const OL = '1. '; // Ordered list
|
|
14
|
+
const UL = '- '; // Unordered list
|
|
15
|
+
|
|
16
|
+
const MAX_TAG_HEAD_LENGTH = 80;
|
|
17
|
+
|
|
18
|
+
const max = (a: number, b: number) => Math.max(a, b);
|
|
19
|
+
const increment = (o: Options, n = 2) => ({
|
|
20
|
+
...o,
|
|
21
|
+
indent: (o.indent || 0) + n,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function* renderChildren(a: Node, options: Options) {
|
|
25
|
+
for (const child of a.children) {
|
|
26
|
+
yield* render(child, options);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function* renderTableRow(items: Array<string>) {
|
|
31
|
+
yield `| ${items.join(' | ')} |`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function renderValue(v: Value): string {
|
|
35
|
+
if (v === null) {
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
if (Ast.isAst(v)) {
|
|
39
|
+
return format(v);
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(v)) {
|
|
42
|
+
return '[' + v.map(renderValue).join(', ') + ']';
|
|
43
|
+
}
|
|
44
|
+
if (typeof v === 'object') {
|
|
45
|
+
return (
|
|
46
|
+
'{' +
|
|
47
|
+
Object.entries(v)
|
|
48
|
+
.map(([key, value]) => `${key}: ${renderValue(value)}`)
|
|
49
|
+
.join(', ') +
|
|
50
|
+
'}'
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return JSON.stringify(v);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function renderAnnotationValue(a: AttributeValue): string {
|
|
57
|
+
if (a.name === 'primary') return a.value;
|
|
58
|
+
if (a.name === 'id' && typeof a.value === 'string') return '#' + a.value;
|
|
59
|
+
if (a.type === 'class') return '.' + a.name;
|
|
60
|
+
return `${a.name}=${renderValue(a.value)}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function* renderAttributes(n: Node) {
|
|
64
|
+
for (const [key, value] of Object.entries(n.attributes)) {
|
|
65
|
+
if (key === 'class' && !Ast.isAst(value))
|
|
66
|
+
for (const name of Object.keys(value)) {
|
|
67
|
+
yield renderAnnotationValue({ type: 'class', name, value });
|
|
68
|
+
}
|
|
69
|
+
else yield renderAnnotationValue({ type: 'attribute', name: key, value });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function* renderAnnotations(n: Node) {
|
|
74
|
+
if (n.annotations.length) {
|
|
75
|
+
yield OPEN + SPACE;
|
|
76
|
+
yield n.annotations.map(renderAnnotationValue).join(SPACE);
|
|
77
|
+
yield SPACE + CLOSE;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function* renderVariable(v: Variable) {
|
|
82
|
+
yield '$';
|
|
83
|
+
yield v.path.join('.');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function* renderFunction(f: Function) {
|
|
87
|
+
yield f.name;
|
|
88
|
+
yield '(';
|
|
89
|
+
yield Object.values(f.parameters).map(renderValue).join(', ');
|
|
90
|
+
yield ')';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function* trimStart(g: Generator) {
|
|
94
|
+
let n;
|
|
95
|
+
do {
|
|
96
|
+
n = g.next().value.trimStart();
|
|
97
|
+
} while (!n.length);
|
|
98
|
+
yield n;
|
|
99
|
+
yield* g;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function* renderNode(n: Node, o: Options = {}) {
|
|
103
|
+
const no = { ...o, parent: n };
|
|
104
|
+
const indent = SPACE.repeat(no.indent || 0);
|
|
105
|
+
|
|
106
|
+
switch (n.type) {
|
|
107
|
+
case 'document': {
|
|
108
|
+
if (n.attributes.frontmatter && n.attributes.frontmatter.length) {
|
|
109
|
+
yield '---' + NL + n.attributes.frontmatter + NL + '---' + NL + NL;
|
|
110
|
+
}
|
|
111
|
+
yield* trimStart(renderChildren(n, no));
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case 'heading': {
|
|
115
|
+
yield NL;
|
|
116
|
+
yield indent;
|
|
117
|
+
yield '#'.repeat(n.attributes.level || 1);
|
|
118
|
+
yield SPACE;
|
|
119
|
+
yield* renderChildren(n, no);
|
|
120
|
+
yield* renderAnnotations(n);
|
|
121
|
+
yield NL;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case 'paragraph': {
|
|
125
|
+
yield NL;
|
|
126
|
+
yield indent;
|
|
127
|
+
yield* renderChildren(n, no);
|
|
128
|
+
yield* renderAnnotations(n);
|
|
129
|
+
yield NL;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case 'inline': {
|
|
133
|
+
yield* renderChildren(n, no);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
case 'link': {
|
|
137
|
+
yield '[';
|
|
138
|
+
yield* renderChildren(n, no);
|
|
139
|
+
yield ']';
|
|
140
|
+
yield '(';
|
|
141
|
+
yield* render(n.attributes.href, no);
|
|
142
|
+
yield ')';
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case 'text': {
|
|
146
|
+
if (Ast.isAst(n.attributes.content)) yield OPEN + SPACE;
|
|
147
|
+
yield* render(n.attributes.content, no);
|
|
148
|
+
if (Ast.isAst(n.attributes.content)) yield SPACE + CLOSE;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case 'blockquote': {
|
|
152
|
+
yield NL;
|
|
153
|
+
yield indent;
|
|
154
|
+
yield '> ';
|
|
155
|
+
yield* trimStart(renderChildren(n, no));
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'hr': {
|
|
159
|
+
yield NL;
|
|
160
|
+
yield indent;
|
|
161
|
+
yield '---';
|
|
162
|
+
yield NL;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case 'image': {
|
|
166
|
+
yield '!';
|
|
167
|
+
yield '[';
|
|
168
|
+
yield* render(n.attributes.alt, no);
|
|
169
|
+
yield ']';
|
|
170
|
+
yield '(';
|
|
171
|
+
yield* render(n.attributes.src, no);
|
|
172
|
+
yield ')';
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'fence': {
|
|
176
|
+
yield NL;
|
|
177
|
+
yield indent;
|
|
178
|
+
yield '```';
|
|
179
|
+
yield (n.attributes.language || '').toLowerCase();
|
|
180
|
+
if (n.annotations.length) yield SPACE;
|
|
181
|
+
yield* renderAnnotations(n);
|
|
182
|
+
yield NL;
|
|
183
|
+
yield indent;
|
|
184
|
+
yield* renderChildren(n, no);
|
|
185
|
+
yield indent;
|
|
186
|
+
yield '```';
|
|
187
|
+
yield NL;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
case 'tag': {
|
|
191
|
+
if (!n.inline) {
|
|
192
|
+
yield NL;
|
|
193
|
+
yield indent;
|
|
194
|
+
}
|
|
195
|
+
const open = OPEN + SPACE;
|
|
196
|
+
const tag = [open + n.tag, ...renderAttributes(n)];
|
|
197
|
+
const inlineTag = tag.join(SPACE);
|
|
198
|
+
if (inlineTag.length + open.length * 2 > MAX_TAG_HEAD_LENGTH) {
|
|
199
|
+
yield tag.join('\n' + SPACE.repeat(open.length) + indent);
|
|
200
|
+
} else {
|
|
201
|
+
yield inlineTag;
|
|
202
|
+
}
|
|
203
|
+
yield SPACE + (n.children.length ? '' : '/') + CLOSE;
|
|
204
|
+
if (n.children.length) {
|
|
205
|
+
yield* renderChildren(n, no.allowIndentation ? increment(no) : no);
|
|
206
|
+
if (!n.inline) {
|
|
207
|
+
yield indent;
|
|
208
|
+
}
|
|
209
|
+
yield OPEN + SPACE + '/';
|
|
210
|
+
yield n.tag;
|
|
211
|
+
yield SPACE + CLOSE;
|
|
212
|
+
}
|
|
213
|
+
if (!n.inline) {
|
|
214
|
+
yield NL;
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
case 'list': {
|
|
219
|
+
yield NL;
|
|
220
|
+
for (let i = 0; i < n.children.length; i++) {
|
|
221
|
+
yield indent;
|
|
222
|
+
const prefix = n.attributes.ordered ? OL : UL;
|
|
223
|
+
yield prefix;
|
|
224
|
+
yield* render(n.children[i], increment(no, prefix.length));
|
|
225
|
+
// TODO do we need this newline?
|
|
226
|
+
if (!no.indent) yield NL;
|
|
227
|
+
}
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case 'item': {
|
|
231
|
+
yield* trimStart(renderChildren(n, no));
|
|
232
|
+
yield* renderAnnotations(n);
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
case 'strong': {
|
|
236
|
+
yield '**';
|
|
237
|
+
yield* renderChildren(n, no);
|
|
238
|
+
yield '**';
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
case 'em': {
|
|
242
|
+
yield '_';
|
|
243
|
+
yield* renderChildren(n, no);
|
|
244
|
+
yield '_';
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case 'code': {
|
|
248
|
+
yield '`';
|
|
249
|
+
yield* render(n.attributes.content, no);
|
|
250
|
+
yield '`';
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
case 's': {
|
|
254
|
+
yield '~~';
|
|
255
|
+
yield* renderChildren(n, no);
|
|
256
|
+
yield '~~';
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
case 'hardbreak': {
|
|
260
|
+
yield '\\\n';
|
|
261
|
+
yield indent;
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
case 'softbreak': {
|
|
265
|
+
yield NL;
|
|
266
|
+
yield indent;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
case 'table': {
|
|
270
|
+
const table = [...renderChildren(n, increment(no))] as any as string[][];
|
|
271
|
+
if (o.parent && o.parent.type === 'tag' && o.parent.tag === 'table') {
|
|
272
|
+
for (const row of table) {
|
|
273
|
+
yield NL;
|
|
274
|
+
for (const d of row) {
|
|
275
|
+
// TODO see if we should move trim() to `td`
|
|
276
|
+
yield indent + UL + d.trim();
|
|
277
|
+
yield NL;
|
|
278
|
+
}
|
|
279
|
+
if (row !== table[table.length - 1]) {
|
|
280
|
+
yield indent + '---';
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
yield NL;
|
|
285
|
+
const [head, ...rows] = table;
|
|
286
|
+
|
|
287
|
+
const ml = table
|
|
288
|
+
.map((arr) => arr.map((s) => s.length).reduce(max))
|
|
289
|
+
.reduce(max);
|
|
290
|
+
|
|
291
|
+
yield* renderTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
|
|
292
|
+
yield NL;
|
|
293
|
+
yield* renderTableRow(head.map(() => '-'.repeat(ml)));
|
|
294
|
+
yield NL;
|
|
295
|
+
for (const row of rows) {
|
|
296
|
+
yield* renderTableRow(
|
|
297
|
+
row.map((r) => r + SPACE.repeat(ml - r.length))
|
|
298
|
+
);
|
|
299
|
+
yield NL;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
case 'thead': {
|
|
305
|
+
const [head] = [...renderChildren(n, no)];
|
|
306
|
+
yield head || [];
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
case 'tr': {
|
|
310
|
+
yield [...renderChildren(n, no)];
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
case 'td':
|
|
314
|
+
case 'th': {
|
|
315
|
+
yield [...renderChildren(n, no), ...renderAnnotations(n)].join('');
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
case 'tbody': {
|
|
319
|
+
yield* renderChildren(n, no);
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
case 'error': {
|
|
323
|
+
console.error(n);
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
case 'node':
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function* render(
|
|
332
|
+
v: Value,
|
|
333
|
+
o: Options = {}
|
|
334
|
+
): Generator<string, boolean, unknown> {
|
|
335
|
+
switch (typeof v) {
|
|
336
|
+
case 'undefined':
|
|
337
|
+
break;
|
|
338
|
+
case 'boolean':
|
|
339
|
+
case 'number':
|
|
340
|
+
case 'string': {
|
|
341
|
+
yield v.toString();
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
case 'object': {
|
|
345
|
+
if (v === null) break;
|
|
346
|
+
if (Array.isArray(v)) {
|
|
347
|
+
for (const n of v) {
|
|
348
|
+
yield* render(n, o);
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
switch (v.$$mdtype) {
|
|
353
|
+
case 'Function': {
|
|
354
|
+
yield* renderFunction(v as Function);
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
case 'Node':
|
|
358
|
+
yield* renderNode(v as Node, o);
|
|
359
|
+
break;
|
|
360
|
+
case 'Variable': {
|
|
361
|
+
yield* renderVariable(v as Variable);
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
default:
|
|
365
|
+
throw new Error(`Unimplemented: "${v.$$mdtype}"`);
|
|
366
|
+
}
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export default function format(v: Value, options?: Options): string {
|
|
374
|
+
let doc = '';
|
|
375
|
+
for (const s of trimStart(render(v, options))) doc += s;
|
|
376
|
+
return doc;
|
|
377
|
+
}
|
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',
|
|
@@ -232,3 +232,4 @@ export const softbreak: Schema = {
|
|
|
232
232
|
};
|
|
233
233
|
|
|
234
234
|
export const error = {};
|
|
235
|
+
export const node = {};
|
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
|
@@ -296,6 +296,40 @@ describe('validate', function () {
|
|
|
296
296
|
});
|
|
297
297
|
});
|
|
298
298
|
|
|
299
|
+
describe('variable validation', () => {
|
|
300
|
+
it('should only validate if the variables config is passed', () => {
|
|
301
|
+
const example = `{% $valid.variable %}`;
|
|
302
|
+
const output = validate(example, {});
|
|
303
|
+
|
|
304
|
+
expect(output).toEqual([]);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('should warn against missing variables', () => {
|
|
308
|
+
const example = `{% $undefinedVariable %}`;
|
|
309
|
+
const output = validate(example, { variables: {} });
|
|
310
|
+
|
|
311
|
+
expect(output).toDeepEqualSubset([
|
|
312
|
+
{
|
|
313
|
+
type: 'text',
|
|
314
|
+
error: {
|
|
315
|
+
id: 'variable-undefined',
|
|
316
|
+
level: 'error',
|
|
317
|
+
message: "Undefined variable: 'undefinedVariable'",
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
]);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('should not warn if variable exists', () => {
|
|
324
|
+
const example = `{% $valid.variable %}`;
|
|
325
|
+
const output = validate(example, {
|
|
326
|
+
variables: { valid: { variable: false } },
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
expect(output).toEqual([]);
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
299
333
|
it('should not error for missing support for code_block', () => {
|
|
300
334
|
const example = ` # https://spec.commonmark.org/0.30/#indented-code-block
|
|
301
335
|
4-space indented code`;
|
package/src/validator.ts
CHANGED
|
@@ -168,7 +168,26 @@ export default function validate(node: Node, config: Config) {
|
|
|
168
168
|
if (Ast.isAst(value)) {
|
|
169
169
|
if (Ast.isFunction(value) && config.validation?.validateFunctions)
|
|
170
170
|
errors.push(...validateFunction(value, config));
|
|
171
|
-
else
|
|
171
|
+
else if (Ast.isVariable(value) && config.variables) {
|
|
172
|
+
let missing = false;
|
|
173
|
+
let variables = config.variables;
|
|
174
|
+
|
|
175
|
+
for (const key of value.path) {
|
|
176
|
+
if (!Object.prototype.hasOwnProperty.call(variables, key)) {
|
|
177
|
+
missing = true;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
variables = variables[key];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (missing) {
|
|
184
|
+
errors.push({
|
|
185
|
+
id: 'variable-undefined',
|
|
186
|
+
level: 'error',
|
|
187
|
+
message: `Undefined variable: '${value.path.join('.')}'`,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
} else continue;
|
|
172
191
|
}
|
|
173
192
|
|
|
174
193
|
value = value as string;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tag.d.ts","sourceRoot":"","sources":["../../../src/ast/tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,QAAQ,CAAC,QAAQ,QAAkB;IAEnC,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,QAAQ,EAAE,kBAAkB,EAAE,CAAC;gBAG7B,IAAI,SAAQ,EACZ,UAAU,KAAK,EACf,QAAQ,GAAE,kBAAkB,EAAO;CAMtC"}
|