@mintlify/common 1.0.425 → 1.0.426
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/getFileCategory.js +2 -1
- package/dist/mdx/plugins/remark/remarkMdxRemoveUnknownJsx/index.d.ts +1 -1
- package/dist/mdx/remark.d.ts +1 -1
- package/dist/mdx/remark.js +8 -1
- package/dist/mdx/snippets/constants.d.ts +1 -0
- package/dist/mdx/snippets/constants.js +1 -0
- package/dist/mdx/snippets/getExportMap.js +91 -13
- package/dist/mdx/snippets/getJsxEsmTree.d.ts +7 -0
- package/dist/mdx/snippets/getJsxEsmTree.js +28 -0
- package/dist/mdx/snippets/index.d.ts +1 -0
- package/dist/mdx/snippets/index.js +1 -0
- package/dist/mdx/snippets/isJsxFile.d.ts +1 -0
- package/dist/mdx/snippets/isJsxFile.js +4 -0
- package/dist/mdx/snippets/nodeIncludesExport.d.ts +6 -5
- package/dist/mdx/snippets/resolveImport/addExportPrefix.d.ts +6 -0
- package/dist/mdx/snippets/resolveImport/addExportPrefix.js +12 -0
- package/dist/mdx/snippets/resolveImport/containsRegularFunction.d.ts +5 -0
- package/dist/mdx/snippets/resolveImport/containsRegularFunction.js +14 -0
- package/dist/mdx/snippets/resolveImport/findExport.d.ts +1 -1
- package/dist/mdx/snippets/resolveImport/findExport.js +99 -13
- package/dist/mdx/snippets/resolveImport/index.js +13 -6
- package/dist/mdx/snippets/resolveImport/resolveComponentWithContent.js +22 -4
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +3 -2
package/dist/getFileCategory.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { parse } from 'path';
|
|
2
|
+
import { SNIPPET_EXTENSIONS } from './mdx/snippets/constants.js';
|
|
2
3
|
const excludedMdFiles = ['readme', 'license', 'contributing', 'contribute'];
|
|
3
4
|
const supportedStaticFileExtensions = [
|
|
4
5
|
'.jpeg',
|
|
@@ -29,7 +30,7 @@ export const getFileCategory = (filePath) => {
|
|
|
29
30
|
const fileName = parsed.name;
|
|
30
31
|
const extension = parsed.ext;
|
|
31
32
|
if ((filePath.startsWith('_snippets/') || filePath.startsWith('snippets/')) &&
|
|
32
|
-
(extension ===
|
|
33
|
+
(SNIPPET_EXTENSIONS.some((ext) => extension === ext) || extension === '.md')) {
|
|
33
34
|
if (filePath.startsWith('_snippets/')) {
|
|
34
35
|
return 'snippet';
|
|
35
36
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Root } from 'mdast';
|
|
2
|
-
export declare const remarkMdxRemoveUnknownJsx: (allowedComponents?: string[]) => (tree: Root) => Root | import("mdast-util-mdxjs-esm").MdxjsEsm | import("mdast
|
|
2
|
+
export declare const remarkMdxRemoveUnknownJsx: (allowedComponents?: string[]) => (tree: Root) => Root | import("mdast-util-mdxjs-esm").MdxjsEsm | import("mdast").Link | import("mdast").Delete | import("mdast").Blockquote | import("mdast").Break | import("mdast").Code | import("mdast").Definition | import("mdast").Emphasis | import("mdast").FootnoteDefinition | import("mdast").FootnoteReference | import("mdast").Heading | import("mdast").Html | import("mdast").Image | import("mdast").ImageReference | import("mdast").InlineCode | import("mdast").LinkReference | import("mdast").List | import("mdast").ListItem | import("mdast").Paragraph | import("mdast").Strong | import("mdast").Table | import("mdast").TableCell | import("mdast").TableRow | import("mdast").Text | import("mdast").ThematicBreak | import("mdast").Yaml | import("mdast-util-math").InlineMath | import("mdast-util-math").Math | import("mdast-util-mdx-expression").MdxTextExpression | import("mdast-util-mdx-expression").MdxFlowExpression | import("mdast-util-mdx").MdxJsxFlowElement | import("mdast-util-mdx").MdxJsxTextElement;
|
package/dist/mdx/remark.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ import type { Root } from 'mdast';
|
|
|
2
2
|
import type { PluggableList } from 'unified';
|
|
3
3
|
export declare const coreRemarkMdxPlugins: PluggableList;
|
|
4
4
|
export declare const coreRemark: import("unified").Processor<Root, undefined, undefined, Root, string>;
|
|
5
|
-
export declare const getAST: (str: string) => Root;
|
|
5
|
+
export declare const getAST: (str: string, filePath?: string) => Root;
|
|
6
6
|
export declare const stringifyTree: (tree: Root) => string;
|
package/dist/mdx/remark.js
CHANGED
|
@@ -4,6 +4,8 @@ import remarkGfm from 'remark-gfm';
|
|
|
4
4
|
import remarkMath from 'remark-math';
|
|
5
5
|
import remarkMdx from 'remark-mdx';
|
|
6
6
|
import remarkStringify from 'remark-stringify';
|
|
7
|
+
import { getJsxEsmTree } from './snippets/getJsxEsmTree.js';
|
|
8
|
+
import { isJsxFile } from './snippets/isJsxFile.js';
|
|
7
9
|
export const coreRemarkMdxPlugins = [
|
|
8
10
|
remarkMdx,
|
|
9
11
|
remarkGfm,
|
|
@@ -11,5 +13,10 @@ export const coreRemarkMdxPlugins = [
|
|
|
11
13
|
remarkMath,
|
|
12
14
|
];
|
|
13
15
|
export const coreRemark = remark().use(coreRemarkMdxPlugins).freeze();
|
|
14
|
-
export const getAST = (str) =>
|
|
16
|
+
export const getAST = (str, filePath) => {
|
|
17
|
+
if (isJsxFile(filePath)) {
|
|
18
|
+
return getJsxEsmTree(str);
|
|
19
|
+
}
|
|
20
|
+
return coreRemark().parse(str);
|
|
21
|
+
};
|
|
15
22
|
export const stringifyTree = (tree) => coreRemark().use(remarkStringify).stringify(tree);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const SNIPPET_EXTENSIONS: string[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SNIPPET_EXTENSIONS = ['.mdx', '.jsx'];
|
|
@@ -1,34 +1,112 @@
|
|
|
1
1
|
import { jsx, toJs } from 'estree-util-to-js';
|
|
2
2
|
import { visit } from 'unist-util-visit';
|
|
3
3
|
import { nodeIncludesExport } from './nodeIncludesExport.js';
|
|
4
|
+
import { addExportPrefix } from './resolveImport/addExportPrefix.js';
|
|
4
5
|
/**
|
|
5
6
|
*
|
|
6
7
|
* @returns map of export names and their content
|
|
7
8
|
*/
|
|
8
9
|
export const getExportMapFromTree = (tree) => {
|
|
10
|
+
var _a;
|
|
9
11
|
const exportMap = {};
|
|
10
12
|
visit(tree, (node) => {
|
|
11
13
|
if (!nodeIncludesExport(node))
|
|
12
14
|
return;
|
|
13
15
|
for (const bodyChild of node.data.estree.body) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
// Handle ExportDefaultDeclaration (export default ...)
|
|
17
|
+
if (bodyChild.type === 'ExportDefaultDeclaration') {
|
|
18
|
+
// If it's export default SomeIdentifier, find the original declaration
|
|
19
|
+
if (bodyChild.declaration.type === 'Identifier') {
|
|
20
|
+
const originalDeclaration = findDeclarationInTree(bodyChild.declaration.name, node);
|
|
21
|
+
if (originalDeclaration) {
|
|
22
|
+
const wrappedNode = structuredClone(node);
|
|
23
|
+
wrappedNode.data.estree.body = [originalDeclaration];
|
|
24
|
+
const value = toJs(wrappedNode.data.estree, { handlers: jsx }).value;
|
|
25
|
+
exportMap['default'] = value;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Otherwise, use the export statement directly (for inline exports like export default function() {})
|
|
26
30
|
const isolatedExport = structuredClone(node);
|
|
27
31
|
isolatedExport.data.estree.body = [bodyChild];
|
|
28
32
|
const value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
|
|
29
|
-
exportMap[
|
|
33
|
+
exportMap['default'] = value;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
// Handle ExportNamedDeclaration with specifiers (export { name })
|
|
37
|
+
if (bodyChild.type === 'ExportNamedDeclaration' && bodyChild.specifiers.length > 0) {
|
|
38
|
+
for (const specifier of bodyChild.specifiers) {
|
|
39
|
+
const localName = specifier.local.name;
|
|
40
|
+
const exportedName = specifier.exported.name;
|
|
41
|
+
// Find the original declaration
|
|
42
|
+
const originalDeclaration = findDeclarationInTree(localName, node);
|
|
43
|
+
if (originalDeclaration) {
|
|
44
|
+
const wrappedNode = structuredClone(node);
|
|
45
|
+
wrappedNode.data.estree.body = [originalDeclaration];
|
|
46
|
+
const value = toJs(wrappedNode.data.estree, { handlers: jsx }).value;
|
|
47
|
+
exportMap[exportedName] = value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
// Handle direct exports (export const/let/var/function/class ...)
|
|
53
|
+
if (bodyChild.type === 'ExportNamedDeclaration' && bodyChild.declaration) {
|
|
54
|
+
// Variable declarations
|
|
55
|
+
if (bodyChild.declaration.type === 'VariableDeclaration') {
|
|
56
|
+
for (const declaration of bodyChild.declaration.declarations) {
|
|
57
|
+
if (declaration.id.type !== 'Identifier')
|
|
58
|
+
continue;
|
|
59
|
+
if (declaration.init == null)
|
|
60
|
+
continue;
|
|
61
|
+
// Sometimes when more than one exports are defined next to each other, the body includes multiple exports so we isolate the export we are looking for.
|
|
62
|
+
const isolatedExport = structuredClone(node);
|
|
63
|
+
isolatedExport.data.estree.body = [bodyChild];
|
|
64
|
+
const value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
|
|
65
|
+
exportMap[declaration.id.name] = value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Function declarations
|
|
69
|
+
else if (bodyChild.declaration.type === 'FunctionDeclaration' && bodyChild.declaration.id) {
|
|
70
|
+
const isolatedExport = structuredClone(node);
|
|
71
|
+
isolatedExport.data.estree.body = [bodyChild];
|
|
72
|
+
const value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
|
|
73
|
+
exportMap[bodyChild.declaration.id.name] = value;
|
|
74
|
+
}
|
|
75
|
+
// Class declarations
|
|
76
|
+
else if (bodyChild.declaration.type === 'ClassDeclaration' && bodyChild.declaration.id) {
|
|
77
|
+
const isolatedExport = structuredClone(node);
|
|
78
|
+
isolatedExport.data.estree.body = [bodyChild];
|
|
79
|
+
const value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
|
|
80
|
+
exportMap[bodyChild.declaration.id.name] = value;
|
|
81
|
+
}
|
|
30
82
|
}
|
|
31
83
|
}
|
|
32
84
|
});
|
|
85
|
+
// Add "export " prefix to values that don't already start with "export"
|
|
86
|
+
for (const [key, value] of Object.entries(exportMap)) {
|
|
87
|
+
exportMap[key] = (_a = addExportPrefix(value)) !== null && _a !== void 0 ? _a : '';
|
|
88
|
+
}
|
|
33
89
|
return exportMap;
|
|
34
90
|
};
|
|
91
|
+
/**
|
|
92
|
+
* Find a declaration by name in the tree
|
|
93
|
+
*/
|
|
94
|
+
function findDeclarationInTree(name, node) {
|
|
95
|
+
var _a, _b;
|
|
96
|
+
for (const bodyChild of node.data.estree.body) {
|
|
97
|
+
if (bodyChild.type === 'VariableDeclaration') {
|
|
98
|
+
for (const declaration of bodyChild.declarations) {
|
|
99
|
+
if (declaration.id.type === 'Identifier' && declaration.id.name === name) {
|
|
100
|
+
return bodyChild;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (bodyChild.type === 'FunctionDeclaration' && ((_a = bodyChild.id) === null || _a === void 0 ? void 0 : _a.name) === name) {
|
|
105
|
+
return bodyChild;
|
|
106
|
+
}
|
|
107
|
+
else if (bodyChild.type === 'ClassDeclaration' && ((_b = bodyChild.id) === null || _b === void 0 ? void 0 : _b.name) === name) {
|
|
108
|
+
return bodyChild;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Parser } from 'acorn';
|
|
2
|
+
import jsx from 'acorn-jsx';
|
|
3
|
+
const JSXParser = Parser.extend(jsx());
|
|
4
|
+
/**
|
|
5
|
+
* Get the AST for a JSX snippet
|
|
6
|
+
* @param jsxContent the content of the JSX snippet
|
|
7
|
+
* @returns the AST for the JSX snippet
|
|
8
|
+
*/
|
|
9
|
+
export const getJsxEsmTree = (jsxContent) => {
|
|
10
|
+
const trimmedContent = jsxContent.trim();
|
|
11
|
+
const estree = JSXParser.parse(trimmedContent, {
|
|
12
|
+
ecmaVersion: 'latest',
|
|
13
|
+
sourceType: 'module',
|
|
14
|
+
allowReturnOutsideFunction: true,
|
|
15
|
+
});
|
|
16
|
+
const finalContent = trimmedContent;
|
|
17
|
+
const mdxJsEsmNode = {
|
|
18
|
+
type: 'mdxjsEsm',
|
|
19
|
+
value: finalContent,
|
|
20
|
+
data: {
|
|
21
|
+
estree,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
type: 'root',
|
|
26
|
+
children: [mdxJsEsmNode],
|
|
27
|
+
};
|
|
28
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isJsxFile: (filePath?: string) => boolean;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { Program } from 'estree-jsx';
|
|
2
2
|
import { MdxjsEsm } from 'mdast-util-mdx';
|
|
3
3
|
import { Node } from 'unist';
|
|
4
|
+
export type NodeWithExport = MdxjsEsm & {
|
|
5
|
+
data: {
|
|
6
|
+
estree: Program;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
4
9
|
/**
|
|
5
10
|
* An export looks like this in AST form:
|
|
6
11
|
* {
|
|
@@ -19,8 +24,4 @@ import { Node } from 'unist';
|
|
|
19
24
|
* @param content mdx ast node
|
|
20
25
|
* @returns whether the node includes an export or not
|
|
21
26
|
*/
|
|
22
|
-
export declare const nodeIncludesExport: (node: Node) => node is
|
|
23
|
-
data: {
|
|
24
|
-
estree: Program;
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
+
export declare const nodeIncludesExport: (node: Node) => node is NodeWithExport;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add "export " prefix to values that don't already start with "export"
|
|
3
|
+
* @param value the snippet content
|
|
4
|
+
* @returns the snippet content with the "export " prefix
|
|
5
|
+
*/
|
|
6
|
+
export const addExportPrefix = (value) => {
|
|
7
|
+
if (!value)
|
|
8
|
+
return undefined;
|
|
9
|
+
if (value.startsWith('export'))
|
|
10
|
+
return value;
|
|
11
|
+
return `export ${value}`;
|
|
12
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if an export statement contains a regular function declaration
|
|
3
|
+
* MDX parser only works properly with arrow functions, so we skip regular functions
|
|
4
|
+
*/
|
|
5
|
+
export const containsRegularFunction = (exportStatement) => {
|
|
6
|
+
// remove comments and clean up the statement
|
|
7
|
+
const cleanStatement = exportStatement.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '');
|
|
8
|
+
// check for function declarations like:
|
|
9
|
+
// 1. export default function ComponentName() { ... }
|
|
10
|
+
// 2. export function ComponentName() { ... }
|
|
11
|
+
// 3. function ComponentName() { ... }; export default ComponentName;
|
|
12
|
+
const functionDeclarationPattern = /\bfunction\s+\w+\s*\(/;
|
|
13
|
+
return functionDeclarationPattern.test(cleanStatement);
|
|
14
|
+
};
|
|
@@ -1,29 +1,80 @@
|
|
|
1
1
|
import { jsx, toJs } from 'estree-util-to-js';
|
|
2
2
|
import { visit, EXIT } from 'unist-util-visit';
|
|
3
3
|
import { nodeIncludesExport } from '../nodeIncludesExport.js';
|
|
4
|
+
import { addExportPrefix } from './addExportPrefix.js';
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
+
* Finds the export in the tree and returns the content of the export
|
|
6
7
|
* @param exportName the name of the export we want to find
|
|
7
8
|
* @param tree the content we are looking for the export in
|
|
8
9
|
* @returns the export
|
|
9
10
|
*/
|
|
10
11
|
export const findExport = (exportName, tree, renamedExportName) => {
|
|
11
|
-
let value
|
|
12
|
+
let value;
|
|
12
13
|
visit(tree, nodeIncludesExport, (node) => {
|
|
13
14
|
for (const bodyChild of node.data.estree.body) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
// handle `export default ...`
|
|
16
|
+
if (bodyChild.type === 'ExportDefaultDeclaration') {
|
|
17
|
+
if (exportName === 'default') {
|
|
18
|
+
const isolatedExport = structuredClone(node);
|
|
19
|
+
isolatedExport.data.estree.body = [bodyChild];
|
|
20
|
+
// handle renaming for default exports
|
|
21
|
+
if (renamedExportName && bodyChild.declaration.type === 'Identifier') {
|
|
22
|
+
bodyChild.declaration.name = renamedExportName;
|
|
23
|
+
}
|
|
24
|
+
value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
|
|
25
|
+
return EXIT;
|
|
26
|
+
}
|
|
17
27
|
continue;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
}
|
|
29
|
+
// handle `export { name }`
|
|
30
|
+
if (bodyChild.type === 'ExportNamedDeclaration' && bodyChild.specifiers.length > 0) {
|
|
31
|
+
for (const specifier of bodyChild.specifiers) {
|
|
32
|
+
if (specifier.exported.name === exportName) {
|
|
33
|
+
const localName = specifier.local.name;
|
|
34
|
+
const originalDeclaration = findDeclarationInTree(localName, node);
|
|
35
|
+
if (originalDeclaration) {
|
|
36
|
+
const isolatedExport = structuredClone(originalDeclaration);
|
|
37
|
+
if (renamedExportName) {
|
|
38
|
+
renameInDeclaration(isolatedExport, localName, renamedExportName);
|
|
39
|
+
}
|
|
40
|
+
const wrappedNode = structuredClone(node);
|
|
41
|
+
wrappedNode.data.estree.body = [isolatedExport];
|
|
42
|
+
value = toJs(wrappedNode.data.estree, { handlers: jsx }).value;
|
|
43
|
+
return EXIT;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
// handle `export const/let/var/function/class ...`
|
|
50
|
+
if (bodyChild.type === 'ExportNamedDeclaration' &&
|
|
51
|
+
bodyChild.declaration &&
|
|
52
|
+
bodyChild.declaration.type === 'VariableDeclaration') {
|
|
53
|
+
for (const declaration of bodyChild.declaration.declarations) {
|
|
54
|
+
if (declaration.id.type !== 'Identifier')
|
|
55
|
+
continue;
|
|
56
|
+
if (declaration.id.name === exportName) {
|
|
57
|
+
// renaming for: import { Name as RenamedName } from './source';
|
|
58
|
+
if (renamedExportName) {
|
|
59
|
+
declaration.id.name = renamedExportName;
|
|
60
|
+
}
|
|
61
|
+
// sometimes when more than one exports are defined next to each other, the body includes multiple exports so we isolate the export we are looking for.
|
|
62
|
+
const isolatedExport = structuredClone(node);
|
|
63
|
+
isolatedExport.data.estree.body = [bodyChild];
|
|
64
|
+
value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
|
|
65
|
+
return EXIT;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// handle `export function/class ...`
|
|
70
|
+
if (bodyChild.type === 'ExportNamedDeclaration' && bodyChild.declaration) {
|
|
71
|
+
if ((bodyChild.declaration.type === 'FunctionDeclaration' ||
|
|
72
|
+
bodyChild.declaration.type === 'ClassDeclaration') &&
|
|
73
|
+
bodyChild.declaration.id &&
|
|
74
|
+
bodyChild.declaration.id.name === exportName) {
|
|
23
75
|
if (renamedExportName) {
|
|
24
|
-
declaration.id.name = renamedExportName;
|
|
76
|
+
bodyChild.declaration.id.name = renamedExportName;
|
|
25
77
|
}
|
|
26
|
-
// Sometimes when more than one exports are defined next to each other, the body includes multiple exports so we isolate the export we are looking for.
|
|
27
78
|
const isolatedExport = structuredClone(node);
|
|
28
79
|
isolatedExport.data.estree.body = [bodyChild];
|
|
29
80
|
value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
|
|
@@ -32,5 +83,40 @@ export const findExport = (exportName, tree, renamedExportName) => {
|
|
|
32
83
|
}
|
|
33
84
|
}
|
|
34
85
|
});
|
|
35
|
-
return value;
|
|
86
|
+
return addExportPrefix(value);
|
|
36
87
|
};
|
|
88
|
+
function findDeclarationInTree(name, node) {
|
|
89
|
+
var _a, _b;
|
|
90
|
+
for (const bodyChild of node.data.estree.body) {
|
|
91
|
+
if (bodyChild.type === 'VariableDeclaration') {
|
|
92
|
+
for (const declaration of bodyChild.declarations) {
|
|
93
|
+
if (declaration.id.type === 'Identifier' && declaration.id.name === name) {
|
|
94
|
+
return bodyChild;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (bodyChild.type === 'FunctionDeclaration' && ((_a = bodyChild.id) === null || _a === void 0 ? void 0 : _a.name) === name) {
|
|
99
|
+
return bodyChild;
|
|
100
|
+
}
|
|
101
|
+
else if (bodyChild.type === 'ClassDeclaration' && ((_b = bodyChild.id) === null || _b === void 0 ? void 0 : _b.name) === name) {
|
|
102
|
+
return bodyChild;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
function renameInDeclaration(declaration, oldName, newName) {
|
|
108
|
+
var _a, _b;
|
|
109
|
+
if (declaration.type === 'VariableDeclaration') {
|
|
110
|
+
for (const variableDeclarator of declaration.declarations) {
|
|
111
|
+
if (variableDeclarator.id.type === 'Identifier' && variableDeclarator.id.name === oldName) {
|
|
112
|
+
variableDeclarator.id.name = newName;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else if (declaration.type === 'FunctionDeclaration' && ((_a = declaration.id) === null || _a === void 0 ? void 0 : _a.name) === oldName) {
|
|
117
|
+
declaration.id.name = newName;
|
|
118
|
+
}
|
|
119
|
+
else if (declaration.type === 'ClassDeclaration' && ((_b = declaration.id) === null || _b === void 0 ? void 0 : _b.name) === oldName) {
|
|
120
|
+
declaration.id.name = newName;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { MdxImportSpecifier } from '../../../types/mdx/snippets/import.js';
|
|
11
11
|
import { getAST } from '../../remark.js';
|
|
12
|
+
import { containsRegularFunction } from './containsRegularFunction.js';
|
|
12
13
|
import { findExport } from './findExport.js';
|
|
13
14
|
import { injectToTopOfFileOfTree } from './injectToTopOfFile.js';
|
|
14
15
|
import { resolveComponentWithContent } from './resolveComponentWithContent.js';
|
|
@@ -16,6 +17,10 @@ const VALID_SPECIFIERS = [
|
|
|
16
17
|
MdxImportSpecifier.ImportSpecifier,
|
|
17
18
|
MdxImportSpecifier.RenamedImportSpecifier,
|
|
18
19
|
];
|
|
20
|
+
const OTHER_SPECIFIERS = [
|
|
21
|
+
MdxImportSpecifier.ImportDefaultSpecifier,
|
|
22
|
+
MdxImportSpecifier.ImportNamespaceSpecifier,
|
|
23
|
+
];
|
|
19
24
|
/**
|
|
20
25
|
*
|
|
21
26
|
* @param importSpecifier The name of the imported component we want to replace
|
|
@@ -25,23 +30,25 @@ const VALID_SPECIFIERS = [
|
|
|
25
30
|
*/
|
|
26
31
|
export const resolveImport = (importSpecifier, destinationPageContent, importedFileContent, exportMap) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
32
|
if (VALID_SPECIFIERS.includes(importSpecifier.type)) {
|
|
28
|
-
// normal import: import { Component } from '/snippets/Component.mdx'
|
|
33
|
+
// normal import: import { Component } from '/snippets/Component.mdx' or jsx file
|
|
29
34
|
if ((importSpecifier.renamedName && exportMap[importSpecifier.renamedName]) ||
|
|
30
35
|
exportMap[importSpecifier.name]) {
|
|
31
36
|
// TODO: Handle collisions
|
|
32
37
|
return destinationPageContent;
|
|
33
38
|
}
|
|
34
|
-
|
|
39
|
+
// find "Component" in "/snippets/Component.mdx" or jsx files
|
|
40
|
+
const exportContent = findExport(importSpecifier.name, importedFileContent, importSpecifier.renamedName);
|
|
35
41
|
if (exportContent == undefined)
|
|
36
42
|
throw new Error(`Could not find export ${importSpecifier.name} in snippet`);
|
|
43
|
+
if (containsRegularFunction(exportContent)) {
|
|
44
|
+
console.warn(`Skipping snippet with regular function declaration. MDX parser only supports arrow functions.`);
|
|
45
|
+
return destinationPageContent;
|
|
46
|
+
}
|
|
37
47
|
// Inject "export const Component = `...`" into the top of the destination page
|
|
38
48
|
injectToTopOfFileOfTree(destinationPageContent, getAST(exportContent).children);
|
|
39
49
|
return destinationPageContent;
|
|
40
50
|
}
|
|
41
|
-
else if (
|
|
42
|
-
MdxImportSpecifier.ImportDefaultSpecifier,
|
|
43
|
-
MdxImportSpecifier.ImportNamespaceSpecifier,
|
|
44
|
-
].includes(importSpecifier.type)) {
|
|
51
|
+
else if (OTHER_SPECIFIERS.includes(importSpecifier.type)) {
|
|
45
52
|
// default export: import DefaultComponent from '/snippets/DefaultComponent.mdx'
|
|
46
53
|
// replace every instance of DefaultComponent with the content of the imported file
|
|
47
54
|
return yield resolveComponentWithContent(destinationPageContent, importSpecifier.name, importedFileContent, exportMap);
|
|
@@ -13,6 +13,7 @@ import { removeFrontmatterFromAST } from '../../astUtils.js';
|
|
|
13
13
|
import { getAST } from '../../remark.js';
|
|
14
14
|
import { createUniqueVariableName, isMdxJsxFlowElement } from '../../utils.js';
|
|
15
15
|
import { findAndRemoveExports } from '../findAndRemoveExports.js';
|
|
16
|
+
import { containsRegularFunction } from './containsRegularFunction.js';
|
|
16
17
|
import { injectToTopOfFileOfTree } from './injectToTopOfFile.js';
|
|
17
18
|
/**
|
|
18
19
|
*
|
|
@@ -31,10 +32,27 @@ export const resolveComponentWithContent = (tree, componentName, snippet, export
|
|
|
31
32
|
if (node.name === componentName && parent && i != null) {
|
|
32
33
|
// Creating clone to restore treeToInject with default values at the end of operations
|
|
33
34
|
const treeToInjectClone = structuredClone(treeToInject);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
const isDefaultExport = snippetExportMap['default'] &&
|
|
36
|
+
(snippetExportMap[componentName] === undefined ||
|
|
37
|
+
Object.keys(snippetExportMap).length === 1);
|
|
38
|
+
// Check if the default export uses a regular function - if so, skip processing
|
|
39
|
+
if (isDefaultExport &&
|
|
40
|
+
snippetExportMap['default'] &&
|
|
41
|
+
containsRegularFunction(snippetExportMap['default'])) {
|
|
42
|
+
console.warn(`Skipping snippet with regular function declaration. MDX parser only supports arrow functions.`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
let snippetNodesToInject;
|
|
46
|
+
let variableDeclarationNodes = [];
|
|
47
|
+
// for named exports, replace the variables with the props
|
|
48
|
+
if (!isDefaultExport) {
|
|
49
|
+
node.name = null;
|
|
50
|
+
const result = replaceVariablesWithProps(node, treeToInject, exportMap, snippetExportMap);
|
|
51
|
+
variableDeclarationNodes = result.variableDeclarationNodes;
|
|
52
|
+
snippetNodesToInject = result.snippetNodesToInject;
|
|
53
|
+
parent.children.splice(i, 1, ...snippetNodesToInject);
|
|
54
|
+
tree.children.splice(((_a = tree.children[0]) === null || _a === void 0 ? void 0 : _a.type) === 'yaml' ? 1 : 0, 0, ...variableDeclarationNodes);
|
|
55
|
+
}
|
|
38
56
|
treeToInject = treeToInjectClone;
|
|
39
57
|
}
|
|
40
58
|
});
|