| 12 | const processor = remark().use(remarkMdx).use(remarkInclude).use(remarkGfm).use(remarkStringify); |
| 13 | |
| 14 | export async function getLLMText(page: InferPageType<typeof source>): Promise<string> { |
| 15 | // Resolve file path relative to project root to handle ISR correctly |
| 16 | // During ISR on Vercel, absolutePath might not be correct, so we resolve it ourselves |
| 17 | // process.cwd() is already packages/docs during build/ISR |
| 18 | const relativePath = page.file.path; |
| 19 | const resolvedPath = path.join(process.cwd(), "content", relativePath); |
| 20 | |
| 21 | // Try resolved path first, fallback to absolutePath if it doesn't exist |
| 22 | let filePath: string; |
| 23 | let fileContent: Buffer; |
| 24 | try { |
| 25 | filePath = resolvedPath; |
| 26 | fileContent = await fs.readFile(filePath); |
| 27 | } catch { |
| 28 | // If the resolved path doesn't work, try the absolutePath as fallback |
| 29 | filePath = page.data._file.absolutePath; |
| 30 | fileContent = await fs.readFile(filePath); |
| 31 | } |
| 32 | |
| 33 | const { content } = matter(fileContent.toString()); |
| 34 | |
| 35 | const processed = await processor.process({ |
| 36 | path: filePath, |
| 37 | value: content, |
| 38 | }); |
| 39 | |
| 40 | return `# ${page.data.title} |
| 41 | |
| 42 | ${processed}`; |
| 43 | } |