UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

64 lines (63 loc) 2.69 kB
import { jsx, toJs } from 'estree-util-to-js'; import { visit } from 'unist-util-visit'; import { getAST } from '../remark.js'; import { nodeIncludesExport } from './nodeIncludesExport.js'; /** * * @returns map of export names and their content */ export const getExportMap = (content) => { const exportMap = {}; const ast = getAST(content); visit(ast, (node) => { if (!nodeIncludesExport(node)) return; for (const bodyChild of node.data.estree.body) { if (bodyChild.type != 'ExportNamedDeclaration') continue; if (bodyChild.declaration == null) continue; if (bodyChild.declaration.type != 'VariableDeclaration') continue; for (const declaration of bodyChild.declaration.declarations) { if (declaration.id.type !== 'Identifier') continue; if (declaration.init == null) continue; // 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]; const value = toJs(isolatedExport.data.estree, { handlers: jsx }).value; exportMap[declaration.id.name] = value; } } }); return exportMap; }; export const getExportMapFromTree = (tree) => { const exportMap = {}; visit(tree, (node) => { if (!nodeIncludesExport(node)) return; for (const bodyChild of node.data.estree.body) { if (bodyChild.type != 'ExportNamedDeclaration') continue; if (bodyChild.declaration == null) continue; if (bodyChild.declaration.type != 'VariableDeclaration') continue; for (const declaration of bodyChild.declaration.declarations) { if (declaration.id.type !== 'Identifier') continue; if (declaration.init == null) continue; // 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]; const value = toJs(isolatedExport.data.estree, { handlers: jsx }).value; exportMap[declaration.id.name] = value; } } }); return exportMap; };