UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

44 lines (43 loc) 2.73 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { MdxImportSpecifier } from '../../../types/mdx/snippets/import.js'; import { findExport } from './findExport.js'; import { injectToTopOfFile } from './injectToTopOfFile.js'; import { resolveComponentWithContent } from './resolveComponentWithContent.js'; /** * * @param importSpecifier The name of the imported component we want to replace * @param destinationPageContent The content of the page we want to inject the import into * @param importedFileContent The content of the file we are importing from * @returns */ export const resolveImport = (importSpecifier, destinationPageContent, importedFileContent, exportMap) => __awaiter(void 0, void 0, void 0, function* () { if ([MdxImportSpecifier.ImportSpecifier, MdxImportSpecifier.RenamedImportSpecifier].includes(importSpecifier.type)) { // normal import: import { Component } from '/snippets/Component.mdx' if ((importSpecifier.renamedName && exportMap[importSpecifier.renamedName]) || exportMap[importSpecifier.name]) { // TODO: Handle collisions return destinationPageContent; } const exportContent = findExport(importSpecifier.name, importedFileContent, importSpecifier.renamedName); // find "Component" in "/snippets/Component.mdx" if (exportContent == undefined) throw new Error(`Could not find export ${importSpecifier.name} in snippet`); // Inject "export const Component = `...`" into the top of the destination page return injectToTopOfFile(destinationPageContent, exportContent); } else if ([ MdxImportSpecifier.ImportDefaultSpecifier, MdxImportSpecifier.ImportNamespaceSpecifier, ].includes(importSpecifier.type)) { // default export: import DefaultComponent from '/snippets/DefaultComponent.mdx' // replace every instance of DefaultComponent with the content of the imported file return yield resolveComponentWithContent(destinationPageContent, importSpecifier.name, importedFileContent, exportMap); } });