@mintlify/common 1.0.786 → 1.0.788

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.
@@ -1,4 +1,3 @@
1
- import { walk } from 'estree-walker';
2
1
  import { remove } from 'unist-util-remove';
3
2
  import { CONTINUE, visit } from 'unist-util-visit';
4
3
  import { isMdxJsEsm } from '../../utils.js';
@@ -231,35 +230,92 @@ export function rebuildStyleValue(properties) {
231
230
  });
232
231
  return `{ ${parts.join(', ')} }`;
233
232
  }
234
- function isFunction(estree) {
235
- if (!estree.body.length)
236
- return false;
237
- let hasFunctionDeclaration = false;
238
- walk(estree, {
239
- enter(node) {
240
- if (node.type === 'FunctionDeclaration' ||
241
- node.type === 'ArrowFunctionExpression' ||
242
- node.type === 'FunctionExpression') {
243
- hasFunctionDeclaration = true;
244
- return this.skip();
245
- }
246
- if (node.type === 'ExportDefaultDeclaration' &&
247
- (node.declaration.type === 'FunctionDeclaration' ||
248
- node.declaration.type === 'FunctionExpression' ||
249
- node.declaration.type === 'ArrowFunctionExpression')) {
250
- hasFunctionDeclaration = true;
251
- return this.skip();
252
- }
253
- if (node.type === 'VariableDeclaration' &&
254
- node.declarations.some((decl) => decl.init &&
255
- (decl.init.type === 'ArrowFunctionExpression' ||
256
- decl.init.type === 'FunctionExpression'))) {
257
- hasFunctionDeclaration = true;
258
- return this.skip();
233
+ function collectExportedNames(estree) {
234
+ var _a, _b;
235
+ const names = [];
236
+ for (const node of estree.body) {
237
+ if (node.type !== 'ExportNamedDeclaration')
238
+ continue;
239
+ const decl = node.declaration;
240
+ if (!decl)
241
+ continue;
242
+ if (decl.type === 'FunctionDeclaration' && decl.id) {
243
+ names.push({ name: decl.id.name, isFunction: true });
244
+ }
245
+ else if (decl.type === 'VariableDeclaration') {
246
+ for (const declarator of decl.declarations) {
247
+ if (declarator.id.type !== 'Identifier')
248
+ continue;
249
+ const isFn = ((_a = declarator.init) === null || _a === void 0 ? void 0 : _a.type) === 'ArrowFunctionExpression' ||
250
+ ((_b = declarator.init) === null || _b === void 0 ? void 0 : _b.type) === 'FunctionExpression';
251
+ names.push({ name: declarator.id.name, isFunction: isFn });
259
252
  }
253
+ }
254
+ }
255
+ return names;
256
+ }
257
+ function buildStubEsmNode(names) {
258
+ const sourceLines = [];
259
+ const body = [];
260
+ for (const { name, isFunction } of names) {
261
+ if (isFunction) {
262
+ sourceLines.push(`export const ${name} = () => null;`);
263
+ body.push({
264
+ type: 'ExportNamedDeclaration',
265
+ declaration: {
266
+ type: 'VariableDeclaration',
267
+ declarations: [
268
+ {
269
+ type: 'VariableDeclarator',
270
+ id: { type: 'Identifier', name },
271
+ init: {
272
+ type: 'ArrowFunctionExpression',
273
+ params: [],
274
+ body: { type: 'Literal', value: null, raw: 'null' },
275
+ expression: true,
276
+ async: false,
277
+ generator: false,
278
+ },
279
+ },
280
+ ],
281
+ kind: 'const',
282
+ },
283
+ specifiers: [],
284
+ source: null,
285
+ });
286
+ }
287
+ else {
288
+ sourceLines.push(`export const ${name} = undefined;`);
289
+ body.push({
290
+ type: 'ExportNamedDeclaration',
291
+ declaration: {
292
+ type: 'VariableDeclaration',
293
+ declarations: [
294
+ {
295
+ type: 'VariableDeclarator',
296
+ id: { type: 'Identifier', name },
297
+ init: { type: 'Identifier', name: 'undefined' },
298
+ },
299
+ ],
300
+ kind: 'const',
301
+ },
302
+ specifiers: [],
303
+ source: null,
304
+ });
305
+ }
306
+ }
307
+ return {
308
+ type: 'mdxjsEsm',
309
+ value: sourceLines.join('\n'),
310
+ data: {
311
+ estree: {
312
+ type: 'Program',
313
+ body,
314
+ sourceType: 'module',
315
+ comments: [],
316
+ },
260
317
  },
261
- });
262
- return hasFunctionDeclaration;
318
+ };
263
319
  }
264
320
  function isArrayOfStringLiterals(estree) {
265
321
  const stmt = estree.body[0];
@@ -272,16 +328,19 @@ function isArrayOfStringLiterals(estree) {
272
328
  export function remarkMdxRemoveJs() {
273
329
  return (tree) => {
274
330
  remove(tree, ['mdxTextExpression', 'mdxFlowExpression']);
275
- remove(tree, (node) => {
331
+ // Collect exported names before stripping ESM so we can inject stubs
332
+ const exportedNames = [];
333
+ visit(tree, (node) => {
276
334
  var _a;
277
- if (!isMdxJsEsm(node))
278
- return false;
279
- if (((_a = node.data) === null || _a === void 0 ? void 0 : _a.estree) && !isFunction(node.data.estree))
280
- return false;
281
- const value = node.value;
282
- const containsReact = REACT_HOOKS.some((hook) => value.includes(hook) || value.includes('React.' + hook.toLowerCase()));
283
- return containsReact;
335
+ if (isMdxJsEsm(node) && ((_a = node.data) === null || _a === void 0 ? void 0 : _a.estree)) {
336
+ exportedNames.push(...collectExportedNames(node.data.estree));
337
+ }
284
338
  });
339
+ remove(tree, (node) => isMdxJsEsm(node));
340
+ // Inject stub declarations for stripped exports to prevent ReferenceErrors
341
+ if (exportedNames.length > 0) {
342
+ tree.children.unshift(buildStubEsmNode(exportedNames));
343
+ }
285
344
  visit(tree, (node) => {
286
345
  if (!('attributes' in node))
287
346
  return CONTINUE;