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