|
| 1 | +import { getCollection } from 'astro:content'; |
| 2 | +import type { APIRoute, GetStaticPaths } from 'astro'; |
| 3 | + |
| 4 | +export const prerender = true; |
| 5 | + |
| 6 | +export const getStaticPaths: GetStaticPaths = async () => { |
| 7 | + const docs = await getCollection('docs'); |
| 8 | + return docs.map((doc) => ({ |
| 9 | + params: { slug: doc.id === 'index' ? undefined : doc.id }, |
| 10 | + props: { doc }, |
| 11 | + })); |
| 12 | +}; |
| 13 | + |
| 14 | +function cleanMdxContent(content: string): string { |
| 15 | + // Remove MDX import statements at the start of the file |
| 16 | + content = content.replace( |
| 17 | + /^(\s*import\s+.*?(?:from\s+['"].*?['"])?;?\s*\n)+/m, |
| 18 | + '', |
| 19 | + ); |
| 20 | + |
| 21 | + // Process Tabs components - extract TabItem contents |
| 22 | + content = content.replace(/<Tabs>[\s\S]*?<\/Tabs>/g, (match) => { |
| 23 | + const results: string[] = []; |
| 24 | + const tabItemRegex = |
| 25 | + /<TabItem[^>]*label="([^"]*)"[^>]*>([\s\S]*?)(?=<TabItem|<\/Tabs>)/g; |
| 26 | + |
| 27 | + for (const [, label, tabContent] of match.matchAll(tabItemRegex)) { |
| 28 | + const cleanContent = tabContent.replace(/<\/TabItem>\s*$/, '').trim(); |
| 29 | + if (cleanContent) { |
| 30 | + results.push(`**${label}:**\n${cleanContent}`); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return results.length > 0 ? results.join('\n\n') : ''; |
| 35 | + }); |
| 36 | + |
| 37 | + // Remove self-closing JSX/MDX components |
| 38 | + content = content.replace(/<[A-Z][a-zA-Z]*\s+[^>]*\/>/g, ''); |
| 39 | + |
| 40 | + // Handle Callout components - keep content |
| 41 | + content = content.replace( |
| 42 | + /<Callout[^>]*>([\s\S]*?)<\/Callout>/g, |
| 43 | + (_, inner) => inner.trim(), |
| 44 | + ); |
| 45 | + |
| 46 | + // Remove remaining JSX component tags |
| 47 | + content = content.replace(/<[A-Z][a-zA-Z]*[^>]*>/g, ''); |
| 48 | + content = content.replace(/<\/[A-Z][a-zA-Z]*>/g, ''); |
| 49 | + |
| 50 | + // Convert relative links to fully qualified URLs |
| 51 | + content = content.replace( |
| 52 | + /\[([^\]]+)\]\(\/([^)]*)\)/g, |
| 53 | + (_, text, path) => `[${text}](https://docs.sprites.dev/${path})`, |
| 54 | + ); |
| 55 | + |
| 56 | + // Clean up excessive blank lines |
| 57 | + content = content.replace(/\n{4,}/g, '\n\n\n'); |
| 58 | + |
| 59 | + return content.trim(); |
| 60 | +} |
| 61 | + |
| 62 | +export const GET: APIRoute = async ({ props }) => { |
| 63 | + const { doc } = props as { |
| 64 | + doc: { |
| 65 | + data: { title: string; description?: string }; |
| 66 | + body: string; |
| 67 | + id: string; |
| 68 | + }; |
| 69 | + }; |
| 70 | + |
| 71 | + const url = `https://docs.sprites.dev/${doc.id === 'index' ? '' : `${doc.id}/`}`; |
| 72 | + const cleanedContent = cleanMdxContent(doc.body); |
| 73 | + |
| 74 | + const markdown = `# ${doc.data.title} |
| 75 | +
|
| 76 | +Source: ${url} |
| 77 | +${doc.data.description ? `\n${doc.data.description}\n` : ''} |
| 78 | +${cleanedContent} |
| 79 | +`; |
| 80 | + |
| 81 | + return new Response(markdown, { |
| 82 | + headers: { |
| 83 | + 'Content-Type': 'text/plain; charset=utf-8', |
| 84 | + }, |
| 85 | + }); |
| 86 | +}; |
0 commit comments