@mintlify/common 1.0.1325 → 1.0.1327
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/mdx/server-only/emit/emit.d.ts +5 -0
- package/dist/mdx/server-only/emit/emit.js +726 -0
- package/dist/mdx/server-only/emit/rewrite.d.ts +28 -0
- package/dist/mdx/server-only/emit/rewrite.js +312 -0
- package/dist/mdx/server-only/getMdx/fastCompile.d.ts +5 -0
- package/dist/mdx/server-only/getMdx/fastCompile.js +7 -0
- package/dist/mdx/server-only/getMdx.js +7 -6
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +13 -4
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
import { GENERATOR, generate } from 'astring';
|
|
2
|
+
import { stringify as commas } from 'comma-separated-tokens';
|
|
3
|
+
import { attachComments } from 'estree-util-attach-comments';
|
|
4
|
+
import { name as isIdentifierName } from 'estree-util-is-identifier-name';
|
|
5
|
+
import { find, hastToReact, html, svg } from 'property-information';
|
|
6
|
+
import { stringify as spaces } from 'space-separated-tokens';
|
|
7
|
+
import styleToObject from 'style-to-object';
|
|
8
|
+
import { addPartialReferences, newInfo, Rewriter } from './rewrite.js';
|
|
9
|
+
const detach = (value) => (' ' + value).slice(1);
|
|
10
|
+
export class Unsupported extends Error {
|
|
11
|
+
constructor(construct) {
|
|
12
|
+
super(`Unsupported MDX construct: ${construct}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const INDENT = ' ';
|
|
16
|
+
const indents = [];
|
|
17
|
+
const indent = (level) => { var _a; return ((_a = indents[level]) !== null && _a !== void 0 ? _a : (indents[level] = INDENT.repeat(level))); };
|
|
18
|
+
const tableElements = new Set(['table', 'tbody', 'thead', 'tfoot', 'tr']);
|
|
19
|
+
const tableCellElement = new Set(['td', 'th']);
|
|
20
|
+
const dashSomething = /-([a-z])/g;
|
|
21
|
+
const whitespaceOnly = /^[ \t\n\f\r]*$/;
|
|
22
|
+
const styleCache = new Map();
|
|
23
|
+
const styleFields = new Map();
|
|
24
|
+
const infoCache = new Map();
|
|
25
|
+
function propertyInfo(schema, prop) {
|
|
26
|
+
let cache = infoCache.get(schema);
|
|
27
|
+
if (!cache) {
|
|
28
|
+
cache = new Map();
|
|
29
|
+
infoCache.set(schema, cache);
|
|
30
|
+
}
|
|
31
|
+
let info = cache.get(prop);
|
|
32
|
+
if (!info) {
|
|
33
|
+
const name = detach(prop);
|
|
34
|
+
info = find(schema, name);
|
|
35
|
+
if (cache.size < 10000)
|
|
36
|
+
cache.set(name, info);
|
|
37
|
+
}
|
|
38
|
+
return info;
|
|
39
|
+
}
|
|
40
|
+
const str = (value) => JSON.stringify(value);
|
|
41
|
+
const key = (name) => (isIdentifierName(name) ? name : str(name));
|
|
42
|
+
function parseStyle(value) {
|
|
43
|
+
const cached = styleCache.get(value);
|
|
44
|
+
if (cached)
|
|
45
|
+
return cached;
|
|
46
|
+
const source = detach(value);
|
|
47
|
+
const result = {};
|
|
48
|
+
styleToObject(source, (name, styleValue) => {
|
|
49
|
+
let property = name;
|
|
50
|
+
if (property.slice(0, 2) !== '--') {
|
|
51
|
+
if (property.slice(0, 4) === '-ms-')
|
|
52
|
+
property = 'ms-' + property.slice(4);
|
|
53
|
+
property = property.replace(dashSomething, (_, $1) => $1.toUpperCase());
|
|
54
|
+
}
|
|
55
|
+
result[property] = styleValue;
|
|
56
|
+
});
|
|
57
|
+
if (styleCache.size >= 10000)
|
|
58
|
+
styleCache.clear();
|
|
59
|
+
styleCache.set(source, result);
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
function objectCode(fields, level) {
|
|
63
|
+
if (fields.length === 0)
|
|
64
|
+
return '{}';
|
|
65
|
+
const pad = indent(level + 1);
|
|
66
|
+
let code = '{\n' + pad + fields[0];
|
|
67
|
+
for (let index = 1; index < fields.length; index++)
|
|
68
|
+
code += ',\n' + pad + fields[index];
|
|
69
|
+
return code + '\n' + indent(level) + '}';
|
|
70
|
+
}
|
|
71
|
+
function styleField(value, align, level) {
|
|
72
|
+
const cacheKey = typeof value === 'string' && align === undefined ? level + '\0' + value : '';
|
|
73
|
+
const cached = cacheKey && styleFields.get(cacheKey);
|
|
74
|
+
if (cached)
|
|
75
|
+
return cached;
|
|
76
|
+
const object = value === undefined
|
|
77
|
+
? {}
|
|
78
|
+
: typeof value === 'object'
|
|
79
|
+
? value
|
|
80
|
+
: parseStyle(String(value));
|
|
81
|
+
const fields = [];
|
|
82
|
+
for (const cssProp in object) {
|
|
83
|
+
if (Object.hasOwn(object, cssProp))
|
|
84
|
+
fields.push(key(cssProp) + ': ' + str(String(object[cssProp])));
|
|
85
|
+
}
|
|
86
|
+
if (align !== undefined)
|
|
87
|
+
fields.push('textAlign: ' + str(align));
|
|
88
|
+
const field = 'style: ' + objectCode(fields, level);
|
|
89
|
+
if (cacheKey) {
|
|
90
|
+
if (styleFields.size >= 10000)
|
|
91
|
+
styleFields.clear();
|
|
92
|
+
styleFields.set(detach(cacheKey), field);
|
|
93
|
+
}
|
|
94
|
+
return field;
|
|
95
|
+
}
|
|
96
|
+
function checkUserEstree(node, inFunction) {
|
|
97
|
+
if (!node || typeof node !== 'object')
|
|
98
|
+
return false;
|
|
99
|
+
if (Array.isArray(node)) {
|
|
100
|
+
let awaits = false;
|
|
101
|
+
for (const child of node)
|
|
102
|
+
awaits = checkUserEstree(child, inFunction) || awaits;
|
|
103
|
+
return awaits;
|
|
104
|
+
}
|
|
105
|
+
const value = node;
|
|
106
|
+
let nested = inFunction;
|
|
107
|
+
let awaits = false;
|
|
108
|
+
if (typeof value.type === 'string') {
|
|
109
|
+
const type = value.type;
|
|
110
|
+
if (type === 'AwaitExpression' && !inFunction)
|
|
111
|
+
awaits = true;
|
|
112
|
+
if (type === 'JSXSpreadChild')
|
|
113
|
+
throw new Unsupported(type);
|
|
114
|
+
if (type === 'ArrowFunctionExpression' ||
|
|
115
|
+
type === 'FunctionExpression' ||
|
|
116
|
+
type === 'FunctionDeclaration')
|
|
117
|
+
nested = true;
|
|
118
|
+
}
|
|
119
|
+
for (const k in value) {
|
|
120
|
+
if (k === 'loc' || k === 'range' || k === 'data' || k === 'comments')
|
|
121
|
+
continue;
|
|
122
|
+
const child = value[k];
|
|
123
|
+
if (child && typeof child === 'object')
|
|
124
|
+
awaits = checkUserEstree(child, nested) || awaits;
|
|
125
|
+
}
|
|
126
|
+
return awaits;
|
|
127
|
+
}
|
|
128
|
+
function printEstree(node, level) {
|
|
129
|
+
return generate(node, {
|
|
130
|
+
comments: true,
|
|
131
|
+
generator: GENERATOR,
|
|
132
|
+
startingIndentLevel: level,
|
|
133
|
+
indent: INDENT,
|
|
134
|
+
lineEnd: '\n',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function expressionOf(program) {
|
|
138
|
+
const statement = program === null || program === void 0 ? void 0 : program.body[0];
|
|
139
|
+
return statement && statement.type === 'ExpressionStatement' ? statement.expression : undefined;
|
|
140
|
+
}
|
|
141
|
+
const isWhitespaceLiteral = (expression) => (expression === null || expression === void 0 ? void 0 : expression.type) === 'Literal' && whitespaceOnly.test(String(expression.value));
|
|
142
|
+
class Emitter {
|
|
143
|
+
constructor(rewriter) {
|
|
144
|
+
this.rewriter = rewriter;
|
|
145
|
+
this.info = newInfo();
|
|
146
|
+
this.usesJsxs = false;
|
|
147
|
+
this.usesFragment = false;
|
|
148
|
+
this.async = false;
|
|
149
|
+
this.schema = html;
|
|
150
|
+
this.comments = [];
|
|
151
|
+
}
|
|
152
|
+
collectComments(estree, sink = this.comments) {
|
|
153
|
+
var _a;
|
|
154
|
+
const comments = (_a = estree === null || estree === void 0 ? void 0 : estree.comments) !== null && _a !== void 0 ? _a : [];
|
|
155
|
+
sink.push(...comments);
|
|
156
|
+
if (estree)
|
|
157
|
+
attachComments(estree, estree.comments);
|
|
158
|
+
}
|
|
159
|
+
userExpression(expression, level) {
|
|
160
|
+
return printEstree(this.userEstree(expression), level);
|
|
161
|
+
}
|
|
162
|
+
userEstree(node) {
|
|
163
|
+
if (checkUserEstree(node, false))
|
|
164
|
+
this.async = true;
|
|
165
|
+
const resolved = this.rewriter.resolveImports(node);
|
|
166
|
+
this.rewriter.rewrite(resolved);
|
|
167
|
+
return this.rewriter.build(resolved);
|
|
168
|
+
}
|
|
169
|
+
call(name, props, children, level, keyArg) {
|
|
170
|
+
let count = 0;
|
|
171
|
+
let codes = '';
|
|
172
|
+
for (const child of children) {
|
|
173
|
+
if (child.kind === 'empty')
|
|
174
|
+
continue;
|
|
175
|
+
codes = count++ === 0 ? child.code : codes + ', ' + child.code;
|
|
176
|
+
}
|
|
177
|
+
if (count === 1)
|
|
178
|
+
props.push('children: ' + codes);
|
|
179
|
+
else if (count > 1) {
|
|
180
|
+
props.push('children: [' + codes + ']');
|
|
181
|
+
this.usesJsxs = true;
|
|
182
|
+
}
|
|
183
|
+
return ((count > 1 ? '_jsxs(' : '_jsx(') +
|
|
184
|
+
name +
|
|
185
|
+
', ' +
|
|
186
|
+
this.object(props, level) +
|
|
187
|
+
(keyArg === undefined ? '' : ', ' + keyArg) +
|
|
188
|
+
')');
|
|
189
|
+
}
|
|
190
|
+
children(nodes, level, skipLineBreaks) {
|
|
191
|
+
const out = [];
|
|
192
|
+
for (const node of nodes) {
|
|
193
|
+
if (skipLineBreaks && node.type === 'text' && node.value === '\n')
|
|
194
|
+
continue;
|
|
195
|
+
const child = this.node(node, level);
|
|
196
|
+
if (child)
|
|
197
|
+
out.push(child);
|
|
198
|
+
}
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
node(node, level) {
|
|
202
|
+
var _a, _b, _c;
|
|
203
|
+
var _d;
|
|
204
|
+
switch (node.type) {
|
|
205
|
+
case 'text': {
|
|
206
|
+
const value = String(node.value || '');
|
|
207
|
+
if (!value)
|
|
208
|
+
return undefined;
|
|
209
|
+
return whitespaceOnly.test(value)
|
|
210
|
+
? { kind: 'ws', code: str(value) }
|
|
211
|
+
: { kind: 'expr', code: str(value) };
|
|
212
|
+
}
|
|
213
|
+
case 'element':
|
|
214
|
+
return { kind: 'expr', code: this.element(node, level), element: true };
|
|
215
|
+
case 'mdxJsxFlowElement':
|
|
216
|
+
case 'mdxJsxTextElement':
|
|
217
|
+
return {
|
|
218
|
+
kind: 'expr',
|
|
219
|
+
code: this.mdxElement(node, level),
|
|
220
|
+
element: !!node.name,
|
|
221
|
+
};
|
|
222
|
+
case 'mdxFlowExpression':
|
|
223
|
+
case 'mdxTextExpression': {
|
|
224
|
+
const estree = (_a = node.data) === null || _a === void 0 ? void 0 : _a.estree;
|
|
225
|
+
this.collectComments(estree);
|
|
226
|
+
const expression = expressionOf(estree);
|
|
227
|
+
if (!expression)
|
|
228
|
+
return { kind: 'empty' };
|
|
229
|
+
const ws = isWhitespaceLiteral(expression);
|
|
230
|
+
const code = this.userExpression(expression, level);
|
|
231
|
+
return ws ? { kind: 'ws', code } : { kind: 'expr', code };
|
|
232
|
+
}
|
|
233
|
+
case 'mdxjsEsm':
|
|
234
|
+
this.comments.push(...((_d = (_c = (_b = node.data) === null || _b === void 0 ? void 0 : _b.estree) === null || _c === void 0 ? void 0 : _c.comments) !== null && _d !== void 0 ? _d : []));
|
|
235
|
+
return undefined;
|
|
236
|
+
default:
|
|
237
|
+
throw new Unsupported(node.type);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
name(name, explicit) {
|
|
241
|
+
const info = this.info;
|
|
242
|
+
if (name.includes('.')) {
|
|
243
|
+
const parts = name.split('.');
|
|
244
|
+
if (parts.some((part) => !isIdentifierName(part)))
|
|
245
|
+
throw new Unsupported('member-name');
|
|
246
|
+
const id = parts[0];
|
|
247
|
+
const isInScope = this.rewriter.inScope(id);
|
|
248
|
+
if (!info.references.has(name))
|
|
249
|
+
info.references.set(name, true);
|
|
250
|
+
if (!isInScope)
|
|
251
|
+
info.objects.add(id);
|
|
252
|
+
return parts.join('.');
|
|
253
|
+
}
|
|
254
|
+
if (name.includes(':'))
|
|
255
|
+
return str(name);
|
|
256
|
+
if (isIdentifierName(name) && !/^[a-z]/.test(name)) {
|
|
257
|
+
if (!this.rewriter.inScope(name)) {
|
|
258
|
+
if (name !== 'MDXLayout' && !info.references.has(name))
|
|
259
|
+
info.references.set(name, true);
|
|
260
|
+
info.components.add(name);
|
|
261
|
+
}
|
|
262
|
+
return name;
|
|
263
|
+
}
|
|
264
|
+
if (explicit)
|
|
265
|
+
return str(name);
|
|
266
|
+
info.tags.add(name);
|
|
267
|
+
if (!isIdentifierName(name)) {
|
|
268
|
+
let invalid = info.invalid.get(name);
|
|
269
|
+
if (invalid === undefined) {
|
|
270
|
+
invalid = '_component' + info.invalid.size;
|
|
271
|
+
info.invalid.set(name, invalid);
|
|
272
|
+
}
|
|
273
|
+
return invalid;
|
|
274
|
+
}
|
|
275
|
+
return '_components.' + name;
|
|
276
|
+
}
|
|
277
|
+
element(node, level) {
|
|
278
|
+
var _a;
|
|
279
|
+
const parentSchema = this.schema;
|
|
280
|
+
if (parentSchema.space === 'html' && node.tagName.toLowerCase() === 'svg')
|
|
281
|
+
this.schema = svg;
|
|
282
|
+
const explicit = !!((_a = node.data) === null || _a === void 0 ? void 0 : _a._mdxExplicitJsx);
|
|
283
|
+
const callee = this.name(node.tagName, explicit);
|
|
284
|
+
const children = this.children(node.children, level + 1, this.schema.space === 'html' && tableElements.has(node.tagName.toLowerCase()));
|
|
285
|
+
const props = [];
|
|
286
|
+
let alignValue;
|
|
287
|
+
let styleValue;
|
|
288
|
+
let styleIndex = -1;
|
|
289
|
+
const properties = node.properties;
|
|
290
|
+
for (const prop in properties) {
|
|
291
|
+
if (!Object.hasOwn(properties, prop))
|
|
292
|
+
continue;
|
|
293
|
+
let value = properties[prop];
|
|
294
|
+
const info = propertyInfo(this.schema, prop);
|
|
295
|
+
const falsy = !value;
|
|
296
|
+
if (value === null ||
|
|
297
|
+
value === undefined ||
|
|
298
|
+
value === false ||
|
|
299
|
+
(typeof value === 'number' && Number.isNaN(value)) ||
|
|
300
|
+
(falsy && info.boolean))
|
|
301
|
+
continue;
|
|
302
|
+
const name = info.space ? hastToReact[info.property] || info.property : info.attribute;
|
|
303
|
+
if (Array.isArray(value))
|
|
304
|
+
value = info.commaSeparated ? commas(value) : spaces(value);
|
|
305
|
+
if (name === 'style') {
|
|
306
|
+
styleValue = value;
|
|
307
|
+
styleIndex = props.length;
|
|
308
|
+
props.push('');
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (value === true) {
|
|
312
|
+
props.push(key(name) + ': true');
|
|
313
|
+
}
|
|
314
|
+
else if (tableCellElement.has(node.tagName) && name === 'align') {
|
|
315
|
+
alignValue = String(value);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
props.push(key(name) + ': ' + str(String(value)));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (alignValue !== undefined && styleIndex < 0) {
|
|
322
|
+
styleIndex = props.length;
|
|
323
|
+
props.push('');
|
|
324
|
+
}
|
|
325
|
+
if (styleIndex >= 0)
|
|
326
|
+
props[styleIndex] = styleField(styleValue, alignValue, level + 1);
|
|
327
|
+
this.schema = parentSchema;
|
|
328
|
+
return this.call(callee, props, children, level);
|
|
329
|
+
}
|
|
330
|
+
object(fields, level) {
|
|
331
|
+
return objectCode(fields, level);
|
|
332
|
+
}
|
|
333
|
+
mdxElement(node, level) {
|
|
334
|
+
var _a, _b, _c;
|
|
335
|
+
const parentSchema = this.schema;
|
|
336
|
+
if (node.name && parentSchema.space === 'html' && node.name.toLowerCase() === 'svg')
|
|
337
|
+
this.schema = svg;
|
|
338
|
+
const explicit = !!((_a = node.data) === null || _a === void 0 ? void 0 : _a._mdxExplicitJsx);
|
|
339
|
+
let callee;
|
|
340
|
+
if (node.name)
|
|
341
|
+
callee = this.name(node.name, explicit);
|
|
342
|
+
else {
|
|
343
|
+
this.usesFragment = true;
|
|
344
|
+
callee = '_Fragment';
|
|
345
|
+
}
|
|
346
|
+
const attributeComments = [];
|
|
347
|
+
const props = [];
|
|
348
|
+
let spread = false;
|
|
349
|
+
let keyArg;
|
|
350
|
+
for (const attribute of node.attributes) {
|
|
351
|
+
if (attribute.type === 'mdxJsxExpressionAttribute') {
|
|
352
|
+
const estree = (_b = attribute.data) === null || _b === void 0 ? void 0 : _b.estree;
|
|
353
|
+
this.collectComments(estree, attributeComments);
|
|
354
|
+
const object = expressionOf(estree);
|
|
355
|
+
const first = (object === null || object === void 0 ? void 0 : object.type) === 'ObjectExpression' ? object.properties[0] : undefined;
|
|
356
|
+
if (!first || first.type !== 'SpreadElement')
|
|
357
|
+
throw new Unsupported('spread-attribute');
|
|
358
|
+
if (first.argument.type === 'ObjectExpression') {
|
|
359
|
+
const object = this.userEstree(first.argument);
|
|
360
|
+
for (const property of object.properties)
|
|
361
|
+
props.push(printEstree(property, level + 1));
|
|
362
|
+
}
|
|
363
|
+
else
|
|
364
|
+
props.push('...' + this.userExpression(first.argument, level + 1));
|
|
365
|
+
spread = true;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
let value;
|
|
369
|
+
const raw = attribute.value;
|
|
370
|
+
if (raw === null || raw === undefined)
|
|
371
|
+
value = 'true';
|
|
372
|
+
else if (typeof raw === 'object') {
|
|
373
|
+
const estree = (_c = raw.data) === null || _c === void 0 ? void 0 : _c.estree;
|
|
374
|
+
this.collectComments(estree, attributeComments);
|
|
375
|
+
const expression = expressionOf(estree);
|
|
376
|
+
if (!expression)
|
|
377
|
+
throw new Unsupported('empty-attribute-expression');
|
|
378
|
+
value = this.userExpression(expression, level + 1);
|
|
379
|
+
}
|
|
380
|
+
else
|
|
381
|
+
value = str(String(raw));
|
|
382
|
+
if (attribute.name === 'key') {
|
|
383
|
+
if (spread)
|
|
384
|
+
throw new Unsupported('key-after-spread');
|
|
385
|
+
keyArg = value;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
props.push(key(attribute.name) + ': ' + value);
|
|
389
|
+
}
|
|
390
|
+
const children = this.children(node.children, level + 1, false);
|
|
391
|
+
this.comments.push(...attributeComments);
|
|
392
|
+
this.schema = parentSchema;
|
|
393
|
+
return this.call(callee, props, children, level, keyArg);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const HELPER = `function _missingMdxReference(id, component) {
|
|
397
|
+
throw new Error("Expected " + (component ? "component" : "object") + " \`" + id + "\` to be defined: you likely forgot to import, pass, or provide it.");
|
|
398
|
+
}`;
|
|
399
|
+
const MDX_CONTENT = `function MDXContent(props = {}) {
|
|
400
|
+
const {wrapper: MDXLayout} = {
|
|
401
|
+
..._provideComponents(),
|
|
402
|
+
...props.components
|
|
403
|
+
};
|
|
404
|
+
return MDXLayout ? _jsx(MDXLayout, {
|
|
405
|
+
...props,
|
|
406
|
+
children: _jsx(_createMdxContent, {
|
|
407
|
+
...props
|
|
408
|
+
})
|
|
409
|
+
}) : _createMdxContent(props);
|
|
410
|
+
}`;
|
|
411
|
+
const RESOLVE_HELPER = `function _resolveDynamicMdxSpecifier(d) {
|
|
412
|
+
if (typeof d !== "string") return d;
|
|
413
|
+
try {
|
|
414
|
+
new URL(d);
|
|
415
|
+
return d;
|
|
416
|
+
} catch {}
|
|
417
|
+
if (d.startsWith("/") || d.startsWith("./") || d.startsWith("../")) return new URL(d, _importMetaUrl).href;
|
|
418
|
+
return d;
|
|
419
|
+
}`;
|
|
420
|
+
const IMPORT_META_URL = `const _importMetaUrl = arguments[0].baseUrl;
|
|
421
|
+
if (!_importMetaUrl) throw new Error("Unexpected missing \`options.baseUrl\` needed to support \`export … from\`, \`import\`, or \`import.meta.url\` when generating \`function-body\`");`;
|
|
422
|
+
const LAYOUT_CONTENT = `function MDXContent(props = {}) {
|
|
423
|
+
return _jsx(MDXLayout, {
|
|
424
|
+
...props,
|
|
425
|
+
children: _jsx(_createMdxContent, {
|
|
426
|
+
...props
|
|
427
|
+
})
|
|
428
|
+
});
|
|
429
|
+
}`;
|
|
430
|
+
const identifier = (name) => ({ type: 'Identifier', name });
|
|
431
|
+
const importOf = (source) => ({
|
|
432
|
+
type: 'AwaitExpression',
|
|
433
|
+
argument: { type: 'ImportExpression', source },
|
|
434
|
+
});
|
|
435
|
+
const nameOf = (node) => node.type === 'Identifier' ? node.name : String(node.value);
|
|
436
|
+
const isDefault = (node) => node.type === 'Identifier' && node.name === 'default';
|
|
437
|
+
function specifiersToDeclarations(specifiers, init) {
|
|
438
|
+
const namespace = specifiers.find((specifier) => specifier.type === 'ImportNamespaceSpecifier');
|
|
439
|
+
const properties = specifiers
|
|
440
|
+
.filter((specifier) => specifier.type !== 'ImportNamespaceSpecifier')
|
|
441
|
+
.map((specifier) => {
|
|
442
|
+
let key = specifier.type === 'ImportSpecifier'
|
|
443
|
+
? specifier.imported
|
|
444
|
+
: specifier.type === 'ExportSpecifier'
|
|
445
|
+
? specifier.exported
|
|
446
|
+
: identifier('default');
|
|
447
|
+
let value = specifier.local;
|
|
448
|
+
if (specifier.type === 'ExportSpecifier')
|
|
449
|
+
[key, value] = [value, key];
|
|
450
|
+
if (value.type !== 'Identifier')
|
|
451
|
+
throw new Unsupported('specifier');
|
|
452
|
+
return {
|
|
453
|
+
type: 'Property',
|
|
454
|
+
kind: 'init',
|
|
455
|
+
shorthand: key.type === 'Identifier' && key.name === value.name,
|
|
456
|
+
method: false,
|
|
457
|
+
computed: false,
|
|
458
|
+
key,
|
|
459
|
+
value,
|
|
460
|
+
};
|
|
461
|
+
});
|
|
462
|
+
const declarations = [];
|
|
463
|
+
if (namespace)
|
|
464
|
+
declarations.push({ type: 'VariableDeclarator', id: namespace.local, init });
|
|
465
|
+
declarations.push({
|
|
466
|
+
type: 'VariableDeclarator',
|
|
467
|
+
id: { type: 'ObjectPattern', properties },
|
|
468
|
+
init: namespace ? identifier(namespace.local.name) : init,
|
|
469
|
+
});
|
|
470
|
+
return declarations;
|
|
471
|
+
}
|
|
472
|
+
function esmReplacement(statement, esm, exportAll) {
|
|
473
|
+
const declare = (declarations) => ({
|
|
474
|
+
type: 'VariableDeclaration',
|
|
475
|
+
kind: 'const',
|
|
476
|
+
declarations,
|
|
477
|
+
});
|
|
478
|
+
if (statement.type === 'ExportDefaultDeclaration') {
|
|
479
|
+
if (esm.layout)
|
|
480
|
+
throw new Unsupported('duplicate-layout');
|
|
481
|
+
esm.layout = true;
|
|
482
|
+
const declaration = statement.declaration;
|
|
483
|
+
const init = declaration.type === 'FunctionDeclaration'
|
|
484
|
+
? Object.assign(Object.assign({}, declaration), { type: 'FunctionExpression' }) : declaration.type === 'ClassDeclaration'
|
|
485
|
+
? Object.assign(Object.assign({}, declaration), { type: 'ClassExpression' }) : declaration;
|
|
486
|
+
return [
|
|
487
|
+
declare([
|
|
488
|
+
{ type: 'VariableDeclarator', id: identifier('MDXLayout'), init: init },
|
|
489
|
+
]),
|
|
490
|
+
];
|
|
491
|
+
}
|
|
492
|
+
if (statement.type === 'ImportDeclaration' || statement.type === 'ExportAllDeclaration') {
|
|
493
|
+
const init = importOf(statement.source);
|
|
494
|
+
if (statement.type === 'ExportAllDeclaration')
|
|
495
|
+
return [declare([{ type: 'VariableDeclarator', id: identifier(exportAll()), init }])];
|
|
496
|
+
return [
|
|
497
|
+
statement.specifiers.length === 0
|
|
498
|
+
? { type: 'ExpressionStatement', expression: init }
|
|
499
|
+
: declare(specifiersToDeclarations(statement.specifiers, init)),
|
|
500
|
+
];
|
|
501
|
+
}
|
|
502
|
+
if (statement.type !== 'ExportNamedDeclaration')
|
|
503
|
+
throw new Unsupported(statement.type);
|
|
504
|
+
if (statement.source) {
|
|
505
|
+
const layout = statement.specifiers.find((specifier) => isDefault(specifier.exported));
|
|
506
|
+
const source = statement.source;
|
|
507
|
+
const init = () => importOf(source);
|
|
508
|
+
const replacements = [];
|
|
509
|
+
if (layout) {
|
|
510
|
+
if (esm.layout)
|
|
511
|
+
throw new Unsupported('duplicate-layout');
|
|
512
|
+
esm.layout = true;
|
|
513
|
+
replacements.push(declare(specifiersToDeclarations([
|
|
514
|
+
isDefault(layout.local)
|
|
515
|
+
? { type: 'ImportDefaultSpecifier', local: identifier('MDXLayout') }
|
|
516
|
+
: {
|
|
517
|
+
type: 'ImportSpecifier',
|
|
518
|
+
imported: layout.local,
|
|
519
|
+
local: identifier('MDXLayout'),
|
|
520
|
+
},
|
|
521
|
+
], importOf({ type: 'Literal', value: source.value }))));
|
|
522
|
+
}
|
|
523
|
+
const rest = statement.specifiers.filter((specifier) => specifier !== layout);
|
|
524
|
+
if (rest.length === 0)
|
|
525
|
+
return replacements;
|
|
526
|
+
esm.exported.push(...rest.map((specifier) => nameOf(specifier.exported)));
|
|
527
|
+
return [...replacements, declare(specifiersToDeclarations(rest, init()))];
|
|
528
|
+
}
|
|
529
|
+
esm.exported.push(...statement.specifiers.map((specifier) => nameOf(specifier.exported)));
|
|
530
|
+
if (statement.declaration)
|
|
531
|
+
return [statement.declaration];
|
|
532
|
+
const declarators = statement.specifiers
|
|
533
|
+
.filter((specifier) => specifier.exported.type === 'Identifier' &&
|
|
534
|
+
specifier.local.type === 'Identifier' &&
|
|
535
|
+
specifier.local.name !== specifier.exported.name)
|
|
536
|
+
.map((specifier) => ({
|
|
537
|
+
type: 'VariableDeclarator',
|
|
538
|
+
id: specifier.exported,
|
|
539
|
+
init: specifier.local,
|
|
540
|
+
}));
|
|
541
|
+
return declarators.length > 0 ? [declare(declarators)] : [];
|
|
542
|
+
}
|
|
543
|
+
function esmNodes(parent, found = []) {
|
|
544
|
+
for (const node of parent.children) {
|
|
545
|
+
if (node.type === 'mdxjsEsm')
|
|
546
|
+
found.push(node);
|
|
547
|
+
else if (node.type === 'element' ||
|
|
548
|
+
node.type === 'mdxJsxFlowElement' ||
|
|
549
|
+
node.type === 'mdxJsxTextElement')
|
|
550
|
+
esmNodes(node, found);
|
|
551
|
+
}
|
|
552
|
+
return found;
|
|
553
|
+
}
|
|
554
|
+
function esmStatements(root, rewriter) {
|
|
555
|
+
var _a;
|
|
556
|
+
const esm = { code: [], exported: [], layout: false };
|
|
557
|
+
const spreads = [];
|
|
558
|
+
const exportAll = () => {
|
|
559
|
+
const name = '_exportAll' + (spreads.length + 1);
|
|
560
|
+
spreads.push('...' + name);
|
|
561
|
+
return name;
|
|
562
|
+
};
|
|
563
|
+
for (const node of esmNodes(root)) {
|
|
564
|
+
const estree = (_a = node.data) === null || _a === void 0 ? void 0 : _a.estree;
|
|
565
|
+
if (!estree)
|
|
566
|
+
throw new Unsupported('esm');
|
|
567
|
+
attachComments(estree, estree.comments);
|
|
568
|
+
for (const statement of estree.body) {
|
|
569
|
+
const declaration = statement.type === 'ExportNamedDeclaration' && statement.declaration;
|
|
570
|
+
if (declaration)
|
|
571
|
+
esm.exported.push(...rewriter.exportedNames(statement));
|
|
572
|
+
for (const replacement of esmReplacement(statement, esm, exportAll)) {
|
|
573
|
+
checkUserEstree(replacement, true);
|
|
574
|
+
rewriter.resolveImports(replacement);
|
|
575
|
+
rewriter.rewrite(replacement);
|
|
576
|
+
const program = {
|
|
577
|
+
type: 'Program',
|
|
578
|
+
sourceType: 'module',
|
|
579
|
+
body: [rewriter.build(replacement)],
|
|
580
|
+
};
|
|
581
|
+
esm.code.push(printEstree(program, 0).replace(/\n$/, ''));
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
esm.exported.unshift(...spreads);
|
|
586
|
+
return esm;
|
|
587
|
+
}
|
|
588
|
+
function rootKind(node) {
|
|
589
|
+
var _a;
|
|
590
|
+
switch (node.type) {
|
|
591
|
+
case 'text':
|
|
592
|
+
return node.value ? (whitespaceOnly.test(node.value) ? 'ws' : 'expr') : undefined;
|
|
593
|
+
case 'element':
|
|
594
|
+
return 'element';
|
|
595
|
+
case 'mdxJsxFlowElement':
|
|
596
|
+
case 'mdxJsxTextElement':
|
|
597
|
+
return node.name ? 'element' : 'expr';
|
|
598
|
+
case 'mdxFlowExpression':
|
|
599
|
+
case 'mdxTextExpression': {
|
|
600
|
+
const expression = expressionOf((_a = node.data) === null || _a === void 0 ? void 0 : _a.estree);
|
|
601
|
+
return !expression ? 'empty' : isWhitespaceLiteral(expression) ? 'ws' : 'expr';
|
|
602
|
+
}
|
|
603
|
+
default:
|
|
604
|
+
return undefined;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
function singleRootElement(root) {
|
|
608
|
+
const kinds = [];
|
|
609
|
+
let queue;
|
|
610
|
+
for (const node of root.children) {
|
|
611
|
+
const kind = rootKind(node);
|
|
612
|
+
if (!kind)
|
|
613
|
+
continue;
|
|
614
|
+
if (kind === 'ws') {
|
|
615
|
+
if (queue)
|
|
616
|
+
queue.push(kind);
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
if (queue)
|
|
620
|
+
kinds.push(...queue);
|
|
621
|
+
kinds.push(kind);
|
|
622
|
+
queue = [];
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return kinds.length === 1 && kinds[0] === 'element';
|
|
626
|
+
}
|
|
627
|
+
export function emitProgram(root) {
|
|
628
|
+
const rewriter = new Rewriter();
|
|
629
|
+
const esm = esmStatements(root, rewriter);
|
|
630
|
+
const emitter = new Emitter(rewriter);
|
|
631
|
+
rewriter.enterContent(emitter.info);
|
|
632
|
+
let content;
|
|
633
|
+
if (singleRootElement(root)) {
|
|
634
|
+
const single = emitter.children(root.children, 1, false).find((child) => child.kind === 'expr');
|
|
635
|
+
content = single.code;
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
const raw = emitter.children(root.children, 2, false);
|
|
639
|
+
const cleaned = [];
|
|
640
|
+
let queue;
|
|
641
|
+
for (const child of raw) {
|
|
642
|
+
if (child.kind === 'ws') {
|
|
643
|
+
if (queue)
|
|
644
|
+
queue.push(child);
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
if (queue)
|
|
648
|
+
cleaned.push(...queue);
|
|
649
|
+
cleaned.push(child);
|
|
650
|
+
queue = [];
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
emitter.usesFragment = true;
|
|
654
|
+
content = emitter.call('_Fragment', [], cleaned, 1);
|
|
655
|
+
}
|
|
656
|
+
const info = emitter.info;
|
|
657
|
+
addPartialReferences(info);
|
|
658
|
+
const lines = [];
|
|
659
|
+
if (emitter.comments.length > 0)
|
|
660
|
+
lines.push(printEstree({ type: 'Program', sourceType: 'module', body: [], comments: emitter.comments }, 0).replace(/\n$/, ''));
|
|
661
|
+
lines.push('"use strict";');
|
|
662
|
+
const runtime = [
|
|
663
|
+
emitter.usesFragment || rewriter.usesFragment ? 'Fragment: _Fragment' : undefined,
|
|
664
|
+
'jsx: _jsx',
|
|
665
|
+
emitter.usesJsxs || rewriter.usesJsxs ? 'jsxs: _jsxs' : undefined,
|
|
666
|
+
].filter(Boolean);
|
|
667
|
+
lines.push('const {' + runtime.join(', ') + '} = arguments[0];');
|
|
668
|
+
const tags = [...info.tags].sort();
|
|
669
|
+
const actual = [...info.components];
|
|
670
|
+
for (const object of info.objects)
|
|
671
|
+
if (!actual.includes(object))
|
|
672
|
+
actual.push(object);
|
|
673
|
+
actual.sort();
|
|
674
|
+
const provides = tags.length > 0 || actual.length > 0 || info.invalid.size > 0;
|
|
675
|
+
if (provides || !esm.layout || rewriter.provides)
|
|
676
|
+
lines.push('const {useMDXComponents: _provideComponents} = arguments[0];');
|
|
677
|
+
const resolve = rewriter.dynamicImport;
|
|
678
|
+
if (resolve || rewriter.importMetaUrl)
|
|
679
|
+
lines.push(IMPORT_META_URL);
|
|
680
|
+
for (const statement of esm.code)
|
|
681
|
+
lines.push(statement);
|
|
682
|
+
const body = [];
|
|
683
|
+
if (provides) {
|
|
684
|
+
const declarators = [];
|
|
685
|
+
let init;
|
|
686
|
+
if (tags.length > 0) {
|
|
687
|
+
const defaults = tags.map((tag) => key(tag) + ': ' + str(tag));
|
|
688
|
+
defaults.push('..._provideComponents()', '...props.components');
|
|
689
|
+
declarators.push('_components = ' + emitter.object(defaults, 1));
|
|
690
|
+
init = '_components';
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
init = emitter.object(['..._provideComponents()', '...props.components'], 1);
|
|
694
|
+
}
|
|
695
|
+
for (const [id, name] of [...info.invalid].sort(([a], [b]) => a.localeCompare(b)))
|
|
696
|
+
declarators.push(name + ' = _components[' + str(id) + ']');
|
|
697
|
+
if (actual.length > 0)
|
|
698
|
+
declarators.push('{' + actual.join(', ') + '} = ' + init);
|
|
699
|
+
body.push('const ' + declarators.join(', ') + ';');
|
|
700
|
+
}
|
|
701
|
+
const references = [...info.references.keys()].sort();
|
|
702
|
+
for (const id of references)
|
|
703
|
+
body.push('if (!' +
|
|
704
|
+
id +
|
|
705
|
+
') _missingMdxReference(' +
|
|
706
|
+
str(id) +
|
|
707
|
+
', ' +
|
|
708
|
+
String(info.references.get(id)) +
|
|
709
|
+
');');
|
|
710
|
+
lines.push((emitter.async ? 'async ' : '') +
|
|
711
|
+
'function _createMdxContent(props) {\n' +
|
|
712
|
+
body.map((line) => indent(1) + line + '\n').join('') +
|
|
713
|
+
indent(1) +
|
|
714
|
+
'return ' +
|
|
715
|
+
content +
|
|
716
|
+
';\n}');
|
|
717
|
+
lines.push(esm.layout ? LAYOUT_CONTENT : MDX_CONTENT);
|
|
718
|
+
const exported = [...esm.exported, 'default: MDXContent'];
|
|
719
|
+
lines.push('return {\n' + exported.map((name) => indent(1) + name).join(',\n') + '\n};');
|
|
720
|
+
if (resolve)
|
|
721
|
+
lines.push(RESOLVE_HELPER);
|
|
722
|
+
if (references.length > 0 || rewriter.missingReference)
|
|
723
|
+
lines.push(HELPER);
|
|
724
|
+
lines.push('');
|
|
725
|
+
return lines.join('\n');
|
|
726
|
+
}
|