UNPKG

@mintlify/previewing

Version:

Preview Mintlify docs locally

38 lines (37 loc) 1.91 kB
import { findAndRemoveImports, optionallyRemoveLeadingSlash, resolveAllImports, stringifyTree, SNIPPET_EXTENSIONS, } from '@mintlify/common'; import { preparseMdxTree, getFileListSync } from '@mintlify/prebuild'; import { promises as _promises } from 'fs'; import { outputFile } from 'fs-extra'; import { join } from 'path'; import { CMD_EXEC_PATH, NEXT_PROPS_PATH } from '../../constants.js'; import { getProcessedSnippets } from './getSnippets.js'; const { readFile } = _promises; export const generatePagesWithImports = async (updatedSnippets) => { const snippets = await getProcessedSnippets(); const pageFilenames = getFileListSync(CMD_EXEC_PATH).filter((file) => SNIPPET_EXTENSIONS.some((ext) => file.endsWith(ext)) && !file.startsWith('_snippets/') && !file.startsWith('snippets/')); await Promise.all(pageFilenames.map(async (pageFilename) => { const sourcePath = join(CMD_EXEC_PATH, pageFilename); const contentStr = (await readFile(sourcePath)).toString(); try { const tree = await preparseMdxTree(contentStr, CMD_EXEC_PATH, sourcePath); const importsResponse = await findAndRemoveImports(tree); if (Object.keys(importsResponse.importMap).some((importPath) => updatedSnippets.has(optionallyRemoveLeadingSlash(importPath)))) { const content = await resolveAllImports({ snippets, fileWithImports: { ...importsResponse, filename: pageFilename }, }); const targetPath = join(NEXT_PROPS_PATH, pageFilename); await outputFile(targetPath, stringifyTree(content), { flag: 'w', }); } } catch (err) { console.log('Error generating pages with imports'); console.log(pageFilename); console.log(err); } })); };