g1t/apps/docs/src/pages/[...slug].md.ts
| 1 | import type { APIRoute, GetStaticPaths } from 'astro'; |
| 2 | import { getCollection } from 'astro:content'; |
| 3 | |
| 4 | /** |
| 5 | * Every page as Markdown, at its path plus `.md`, for "Copy page" and for |
| 6 | * agents that read the docs: the title, what it is about, then the page. |
| 7 | */ |
| 8 | export const getStaticPaths: GetStaticPaths = async () => { |
| 9 | const pages = await getCollection('docs'); |
| 10 | return pages.map((page) => ({ |
| 11 | params: { slug: page.id === '' ? 'index' : page.id }, |
| 12 | props: { page }, |
| 13 | })); |
| 14 | }; |
| 15 | |
| 16 | export const GET: APIRoute = ({ props }) => { |
| 17 | const { page } = props as { page: Awaited<ReturnType<typeof getCollection<'docs'>>>[number] }; |
| 18 | const { title, description } = page.data; |
| 19 | // MDX pages keep their markup; strip imports and leave the rest readable. |
| 20 | const body = (page.body ?? '').replace(/^import .*$/gm, '').trim(); |
| 21 | const text = `# ${title}\n\n${description ? `> ${description}\n\n` : ''}${body}\n`; |
| 22 | return new Response(text, { headers: { 'content-type': 'text/markdown; charset=utf-8' } }); |
| 23 | }; |