|
1 | 1 | import { getCollection } from 'astro:content'; |
2 | 2 | import type { APIRoute, GetStaticPaths } from 'astro'; |
3 | 3 |
|
| 4 | +import { cleanMdxContent } from '@/lib/utils'; |
| 5 | + |
4 | 6 | export const prerender = true; |
5 | 7 |
|
6 | 8 | export const getStaticPaths: GetStaticPaths = async () => { |
7 | | - const docs = await getCollection('docs'); |
| 9 | + const docs = await getCollection('docs', ({ data }) => { |
| 10 | + return data.draft !== true; |
| 11 | + }); |
8 | 12 | return docs.map((doc) => ({ |
9 | 13 | params: { slug: doc.id }, |
10 | 14 | props: { doc }, |
11 | 15 | })); |
12 | 16 | }; |
13 | 17 |
|
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 | | - // Process CardGrid/LinkCard components - convert to markdown links |
38 | | - content = content.replace(/<CardGrid[^>]*>[\s\S]*?<\/CardGrid>/g, (match) => { |
39 | | - const links: string[] = []; |
40 | | - const linkCardRegex = |
41 | | - /<LinkCard\s+[^>]*href="([^"]*)"[^>]*title="([^"]*)"[^>]*description="([^"]*)"[^>]*\/>/g; |
42 | | - |
43 | | - for (const [, href, title, description] of match.matchAll(linkCardRegex)) { |
44 | | - const fullUrl = href.startsWith('/') |
45 | | - ? `https://docs.sprites.dev${href}` |
46 | | - : href; |
47 | | - links.push(`- [${title}](${fullUrl}) - ${description}`); |
48 | | - } |
49 | | - |
50 | | - return links.length > 0 ? links.join('\n') : ''; |
51 | | - }); |
52 | | - |
53 | | - // Remove self-closing JSX/MDX components |
54 | | - content = content.replace(/<[A-Z][a-zA-Z]*\s+[^>]*\/>/g, ''); |
55 | | - |
56 | | - // Handle Callout components - keep content |
57 | | - content = content.replace( |
58 | | - /<Callout[^>]*>([\s\S]*?)<\/Callout>/g, |
59 | | - (_, inner) => inner.trim(), |
60 | | - ); |
61 | | - |
62 | | - // Remove remaining JSX component tags |
63 | | - content = content.replace(/<[A-Z][a-zA-Z]*[^>]*>/g, ''); |
64 | | - content = content.replace(/<\/[A-Z][a-zA-Z]*>/g, ''); |
65 | | - |
66 | | - // Convert relative links to fully qualified URLs |
67 | | - content = content.replace( |
68 | | - /\[([^\]]+)\]\(\/([^)]*)\)/g, |
69 | | - (_, text, path) => `[${text}](https://docs.sprites.dev/${path})`, |
70 | | - ); |
71 | | - |
72 | | - // Clean up excessive blank lines |
73 | | - content = content.replace(/\n{4,}/g, '\n\n\n'); |
74 | | - |
75 | | - return content.trim(); |
76 | | -} |
77 | | - |
78 | 18 | export const GET: APIRoute = async ({ props }) => { |
79 | 19 | const { doc } = props as { |
80 | 20 | doc: { |
|
0 commit comments