@mintlify/common 1.0.1324 → 1.0.1326

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.
@@ -0,0 +1,28 @@
1
+ import type { Expression, Node as EstreeNode, Statement } from 'estree-jsx';
2
+ export type FunctionInfo = {
3
+ tags: Set<string>;
4
+ components: Set<string>;
5
+ objects: Set<string>;
6
+ references: Map<string, boolean>;
7
+ invalid: Map<string, string>;
8
+ };
9
+ export declare const newInfo: () => FunctionInfo;
10
+ export declare function addPartialReferences(info: FunctionInfo): void;
11
+ export declare class Rewriter {
12
+ visitors: import("estree-util-scope").Visitors;
13
+ missingReference: boolean;
14
+ provides: boolean;
15
+ usesFragment: boolean;
16
+ usesJsxs: boolean;
17
+ dynamicImport: boolean;
18
+ importMetaUrl: boolean;
19
+ private stack;
20
+ inScope(id: string): boolean;
21
+ enterContent(info: FunctionInfo): void;
22
+ exportedNames(statement: EstreeNode): string[];
23
+ resolveImports<T extends EstreeNode>(root: T): T;
24
+ rewrite(root: EstreeNode): void;
25
+ private finishFunction;
26
+ build(node: Statement): Statement;
27
+ build(node: Expression): Expression;
28
+ }
@@ -0,0 +1,312 @@
1
+ import { buildJsx } from 'estree-util-build-jsx';
2
+ import { name as isIdentifierName } from 'estree-util-is-identifier-name';
3
+ import { createVisitors } from 'estree-util-scope';
4
+ import { walk } from 'estree-walker';
5
+ export const newInfo = () => ({
6
+ tags: new Set(),
7
+ components: new Set(),
8
+ objects: new Set(),
9
+ references: new Map(),
10
+ invalid: new Map(),
11
+ });
12
+ const isFunction = (node) => node.type === 'ArrowFunctionExpression' ||
13
+ node.type === 'FunctionDeclaration' ||
14
+ node.type === 'FunctionExpression';
15
+ const CONTENT_FUNCTION = { type: 'FunctionDeclaration', id: { name: '_createMdxContent' } };
16
+ function idOrMember(ids) {
17
+ let object;
18
+ for (const name of ids) {
19
+ const id = isIdentifierName(name)
20
+ ? { type: 'Identifier', name }
21
+ : { type: 'Literal', value: name };
22
+ object = object
23
+ ? {
24
+ type: 'MemberExpression',
25
+ object,
26
+ property: id,
27
+ computed: id.type === 'Literal',
28
+ optional: false,
29
+ }
30
+ : id;
31
+ }
32
+ return object;
33
+ }
34
+ function jsxIdOrMember(ids) {
35
+ let object;
36
+ for (const name of ids) {
37
+ const id = { type: 'JSXIdentifier', name };
38
+ object = object ? { type: 'JSXMemberExpression', object, property: id } : id;
39
+ }
40
+ return object;
41
+ }
42
+ export function addPartialReferences(info) {
43
+ for (const name of [...info.references.keys()]) {
44
+ const parts = name.split('.');
45
+ for (let index = 1; index < parts.length; index++) {
46
+ const partial = parts.slice(0, index).join('.');
47
+ if (!info.references.has(partial))
48
+ info.references.set(partial, false);
49
+ }
50
+ }
51
+ }
52
+ export class Rewriter {
53
+ constructor() {
54
+ this.visitors = createVisitors();
55
+ this.missingReference = false;
56
+ this.provides = false;
57
+ this.usesFragment = false;
58
+ this.usesJsxs = false;
59
+ this.dynamicImport = false;
60
+ this.importMetaUrl = false;
61
+ this.stack = [];
62
+ }
63
+ inScope(id) {
64
+ const scopes = this.visitors.scopes;
65
+ for (let index = scopes.length - 1; index >= 0; index--) {
66
+ if (scopes[index].defined.includes(id))
67
+ return true;
68
+ }
69
+ return false;
70
+ }
71
+ enterContent(info) {
72
+ const scopes = this.visitors.scopes;
73
+ scopes[0].defined.push('_createMdxContent');
74
+ scopes.push({ block: false, defined: ['props'] }, { block: true, defined: [] });
75
+ this.stack = [{ node: CONTENT_FUNCTION, info }];
76
+ }
77
+ exportedNames(statement) {
78
+ const visitors = createVisitors();
79
+ walk(statement, {
80
+ enter(node) {
81
+ visitors.enter(node);
82
+ if (isFunction(node)) {
83
+ this.skip();
84
+ visitors.exit(node);
85
+ }
86
+ },
87
+ leave: (node) => visitors.exit(node),
88
+ });
89
+ return visitors.scopes[0].defined;
90
+ }
91
+ resolveImports(root) {
92
+ const flag = (name) => (this[name] = true);
93
+ return walk(root, {
94
+ enter(raw) {
95
+ const node = raw;
96
+ if (node.type === 'ImportExpression') {
97
+ flag('dynamicImport');
98
+ node.source = {
99
+ type: 'CallExpression',
100
+ callee: { type: 'Identifier', name: '_resolveDynamicMdxSpecifier' },
101
+ arguments: [node.source],
102
+ optional: false,
103
+ };
104
+ return;
105
+ }
106
+ if (node.type === 'MemberExpression' &&
107
+ node.object.type === 'MetaProperty' &&
108
+ node.property.type === 'Identifier' &&
109
+ node.object.meta.name === 'import' &&
110
+ node.object.property.name === 'meta' &&
111
+ node.property.name === 'url') {
112
+ flag('importMetaUrl');
113
+ this.replace({ type: 'Identifier', name: '_importMetaUrl' });
114
+ }
115
+ },
116
+ });
117
+ }
118
+ rewrite(root) {
119
+ const visitors = this.visitors;
120
+ const stack = this.stack;
121
+ const inScope = (id) => this.inScope(id);
122
+ const finish = (frame, node) => this.finishFunction(frame, node);
123
+ walk(root, {
124
+ enter(raw) {
125
+ var _a, _b, _c;
126
+ const node = raw;
127
+ visitors.enter(node);
128
+ if (isFunction(node))
129
+ stack.push({ node, info: newInfo() });
130
+ const frame = stack[0];
131
+ if (!frame || node.type !== 'JSXElement')
132
+ return;
133
+ const info = frame.info;
134
+ let name = node.openingElement.name;
135
+ if (name.type === 'JSXMemberExpression') {
136
+ const ids = [];
137
+ while (name.type === 'JSXMemberExpression') {
138
+ ids.unshift(name.property.name);
139
+ name = name.object;
140
+ }
141
+ ids.unshift(name.name);
142
+ const fullId = ids.join('.');
143
+ const id = name.name;
144
+ const isInScope = inScope(id);
145
+ if (!info.references.has(fullId) &&
146
+ (!isInScope ||
147
+ (stack.length === 1 &&
148
+ ((_a = stack[0]) === null || _a === void 0 ? void 0 : _a.node.type) === 'FunctionDeclaration' &&
149
+ ((_b = stack[0].node.id) === null || _b === void 0 ? void 0 : _b.name) === '_createMdxContent')))
150
+ info.references.set(fullId, true);
151
+ if (!isInScope)
152
+ info.objects.add(id);
153
+ }
154
+ else if (name.type === 'JSXNamespacedName') {
155
+ return;
156
+ }
157
+ else if (isIdentifierName(name.name) && !/^[a-z]/.test(name.name)) {
158
+ const id = name.name;
159
+ if (!inScope(id)) {
160
+ if (id !== 'MDXLayout' && !info.references.has(id))
161
+ info.references.set(id, true);
162
+ info.components.add(id);
163
+ }
164
+ }
165
+ else if ((_c = node.data) === null || _c === void 0 ? void 0 : _c._mdxExplicitJsx) {
166
+ return;
167
+ }
168
+ else {
169
+ const id = name.name;
170
+ info.tags.add(id);
171
+ let ids = ['_components', id];
172
+ if (!isIdentifierName(id)) {
173
+ let invalid = info.invalid.get(id);
174
+ if (invalid === undefined) {
175
+ invalid = `_component${info.invalid.size}`;
176
+ info.invalid.set(id, invalid);
177
+ }
178
+ ids = [invalid];
179
+ }
180
+ node.openingElement.name = jsxIdOrMember(ids);
181
+ if (node.closingElement)
182
+ node.closingElement.name = jsxIdOrMember(ids);
183
+ }
184
+ },
185
+ leave(raw) {
186
+ const node = raw;
187
+ visitors.exit(node);
188
+ if (!isFunction(node))
189
+ return;
190
+ const frame = stack[stack.length - 1];
191
+ finish(frame, node);
192
+ stack.pop();
193
+ },
194
+ });
195
+ }
196
+ finishFunction(frame, node) {
197
+ const { info } = frame;
198
+ const statements = [];
199
+ const defaults = [...info.tags].sort().map((name) => ({
200
+ type: 'Property',
201
+ kind: 'init',
202
+ key: isIdentifierName(name) ? { type: 'Identifier', name } : { type: 'Literal', value: name },
203
+ value: { type: 'Literal', value: name },
204
+ method: false,
205
+ shorthand: false,
206
+ computed: false,
207
+ }));
208
+ const actual = [...info.components];
209
+ for (const name of info.objects)
210
+ if (!actual.includes(name))
211
+ actual.push(name);
212
+ actual.sort();
213
+ if (defaults.length > 0 || actual.length > 0 || info.invalid.size > 0) {
214
+ this.provides = true;
215
+ const provider = {
216
+ type: 'CallExpression',
217
+ callee: { type: 'Identifier', name: '_provideComponents' },
218
+ arguments: [],
219
+ optional: false,
220
+ };
221
+ if (defaults.length > 0)
222
+ defaults.push({ type: 'SpreadElement', argument: provider });
223
+ let init = defaults.length > 0
224
+ ? { type: 'ObjectExpression', properties: defaults }
225
+ : {
226
+ type: 'LogicalExpression',
227
+ operator: '||',
228
+ left: provider,
229
+ right: { type: 'ObjectExpression', properties: [] },
230
+ };
231
+ const declarations = [];
232
+ if (info.tags.size > 0) {
233
+ declarations.push({
234
+ type: 'VariableDeclarator',
235
+ id: { type: 'Identifier', name: '_components' },
236
+ init,
237
+ });
238
+ init = { type: 'Identifier', name: '_components' };
239
+ }
240
+ if (actual.length > 0)
241
+ declarations.push({
242
+ type: 'VariableDeclarator',
243
+ id: {
244
+ type: 'ObjectPattern',
245
+ properties: actual.map((name) => ({
246
+ type: 'Property',
247
+ kind: 'init',
248
+ key: { type: 'Identifier', name },
249
+ value: { type: 'Identifier', name },
250
+ method: false,
251
+ shorthand: true,
252
+ computed: false,
253
+ })),
254
+ },
255
+ init,
256
+ });
257
+ if (declarations.length > 0)
258
+ statements.push({ type: 'VariableDeclaration', kind: 'const', declarations });
259
+ }
260
+ addPartialReferences(info);
261
+ for (const id of [...info.references.keys()].sort()) {
262
+ this.missingReference = true;
263
+ statements.push({
264
+ type: 'IfStatement',
265
+ test: {
266
+ type: 'UnaryExpression',
267
+ operator: '!',
268
+ prefix: true,
269
+ argument: idOrMember(id.split('.')),
270
+ },
271
+ consequent: {
272
+ type: 'ExpressionStatement',
273
+ expression: {
274
+ type: 'CallExpression',
275
+ callee: { type: 'Identifier', name: '_missingMdxReference' },
276
+ arguments: [
277
+ { type: 'Literal', value: id },
278
+ { type: 'Literal', value: info.references.get(id) },
279
+ ],
280
+ optional: false,
281
+ },
282
+ },
283
+ alternate: undefined,
284
+ });
285
+ }
286
+ if (statements.length === 0)
287
+ return;
288
+ if (node.body.type !== 'BlockStatement')
289
+ node.body = {
290
+ type: 'BlockStatement',
291
+ body: [{ type: 'ReturnStatement', argument: node.body }],
292
+ };
293
+ node.body.body.unshift(...statements);
294
+ }
295
+ build(node) {
296
+ const expression = !/(Statement|Declaration)$/.test(node.type);
297
+ const statement = (expression ? { type: 'ExpressionStatement', expression: node } : node);
298
+ const program = { type: 'Program', sourceType: 'module', body: [statement] };
299
+ buildJsx(program, { runtime: 'automatic', importSource: 'react' });
300
+ const first = program.body[0];
301
+ if ((first === null || first === void 0 ? void 0 : first.type) === 'ImportDeclaration') {
302
+ for (const specifier of first.specifiers) {
303
+ if (specifier.local.name === '_Fragment')
304
+ this.usesFragment = true;
305
+ else if (specifier.local.name === '_jsxs')
306
+ this.usesJsxs = true;
307
+ }
308
+ }
309
+ const last = program.body[program.body.length - 1];
310
+ return expression ? last.expression : last;
311
+ }
312
+ }
@@ -0,0 +1,7 @@
1
+ import type { Root as HastRoot } from 'hast';
2
+ import type { Processor } from 'unified';
3
+ export declare function remarkMdasticParser(this: Processor): void;
4
+ export type EmittedProgram = {
5
+ code?: string;
6
+ };
7
+ export declare function rehypeEmitProgram(output: EmittedProgram): (tree: HastRoot) => HastRoot;
@@ -0,0 +1,24 @@
1
+ import { parse } from 'mdastic';
2
+ import { gfm } from 'mdastic/gfm';
3
+ import { math } from 'mdastic/math';
4
+ import { mdx } from 'mdastic/mdx';
5
+ import { emitProgram } from '../emit/emit.js';
6
+ const syntax = [
7
+ gfm({ singleTilde: true }),
8
+ mdx({ estree: true }),
9
+ math({ singleDollarTextMath: true }),
10
+ ];
11
+ export function remarkMdasticParser() {
12
+ this.parser = (document) => {
13
+ const tree = parse(document, { syntax });
14
+ if (tree instanceof Error)
15
+ throw tree;
16
+ return tree;
17
+ };
18
+ }
19
+ export function rehypeEmitProgram(output) {
20
+ return (tree) => {
21
+ output.code = emitProgram(tree);
22
+ return { type: 'root', children: [] };
23
+ };
24
+ }
@@ -17,6 +17,7 @@ import { PackageIslandsError, parsePackageIslands, preprocessPageContent, } from
17
17
  import { preprocessCustomHeadingIds } from '../preprocessCustomHeadingIds.js';
18
18
  import { preprocessMdxFragmentCode } from '../preprocessMdxFragmentCode.js';
19
19
  import { replaceVariables } from '../replaceVariables.js';
20
+ import { rehypeEmitProgram, remarkMdasticParser, } from './getMdx/fastCompile.js';
20
21
  import { createSnippetTreeMap } from './getMdx/snippets.js';
21
22
  import { getSerializeExtras } from './getSerializeExtras.js';
22
23
  const getPackageIslandOptions = (packageBuild, pagePath, processedContent) => {
@@ -231,6 +232,17 @@ function compileMdx(_a) {
231
232
  };
232
233
  });
233
234
  }
235
+ function serializeFast(args) {
236
+ return __awaiter(this, void 0, void 0, function* () {
237
+ var _a, _b;
238
+ var _c, _d;
239
+ const emitted = {};
240
+ const result = yield serialize(Object.assign(Object.assign({}, args), { mdxOptions: Object.assign(Object.assign({}, args.mdxOptions), { remarkPlugins: [remarkMdasticParser, ...((_c = (_a = args.mdxOptions) === null || _a === void 0 ? void 0 : _a.remarkPlugins) !== null && _c !== void 0 ? _c : [])], rehypePlugins: [...((_d = (_b = args.mdxOptions) === null || _b === void 0 ? void 0 : _b.rehypePlugins) !== null && _d !== void 0 ? _d : []), [rehypeEmitProgram, emitted]] }) })).catch(() => undefined);
241
+ if (!result || 'error' in result || emitted.code === undefined)
242
+ return serialize(args);
243
+ return Object.assign(Object.assign({}, result), { compiledSource: emitted.code });
244
+ });
245
+ }
234
246
  function getCompiledMdxWithFallback(_a) {
235
247
  return __awaiter(this, arguments, void 0, function* ({ source, failure, scope, mdxOptions, fallbackMdxOptions = mdxOptions, codeStyling, path, traceFn, customLanguages, }) {
236
248
  const themeOrThemes = codeStylingToThemeOrThemes(codeStyling);
@@ -238,7 +250,7 @@ function getCompiledMdxWithFallback(_a) {
238
250
  if (failure)
239
251
  throw failure;
240
252
  const result = yield traceFn('getCompiledMdxWithFallback.serialize', () => __awaiter(this, void 0, void 0, function* () {
241
- return serialize({
253
+ return serializeFast({
242
254
  source,
243
255
  path,
244
256
  scope,