@mintlify/common
Version:
Commonly shared code within Mintlify
39 lines (38 loc) • 1.74 kB
JavaScript
import { jsx, toJs } from 'estree-util-to-js';
import { visit, EXIT } from 'unist-util-visit';
import { getAST } from '../../remark.js';
import { nodeIncludesExport } from '../nodeIncludesExport.js';
/**
*
* @param exportName the name of the export we want to find
* @param content the content we are looking for the export in
* @returns the export
*/
export const findExport = (exportName, content, renamedExportName) => {
const ast = getAST(content);
let value = undefined;
visit(ast, nodeIncludesExport, (node) => {
for (const bodyChild of node.data.estree.body) {
if (bodyChild.type !== 'ExportNamedDeclaration' ||
bodyChild.declaration == null ||
bodyChild.declaration.type !== 'VariableDeclaration')
continue;
for (const declaration of bodyChild.declaration.declarations) {
if (declaration.id.type !== 'Identifier')
continue;
if (declaration.id.name === exportName) {
// Renaming for: import { Name as RenamedName } from './source';
if (renamedExportName) {
declaration.id.name = renamedExportName;
}
// 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.
const isolatedExport = structuredClone(node);
isolatedExport.data.estree.body = [bodyChild];
value = toJs(isolatedExport.data.estree, { handlers: jsx }).value;
return EXIT;
}
}
}
});
return value;
};