|
| 1 | +import { getCollection } from 'astro:content'; |
1 | 2 | import { readFile } from 'node:fs/promises'; |
2 | 3 | import path from 'node:path'; |
3 | 4 | import type { APIRoute } from 'astro'; |
4 | 5 | import { allPages } from '~/content'; |
5 | 6 |
|
| 7 | +type MarkdownSourceProps = { |
| 8 | + id: string; |
| 9 | + collection: 'docs' | 'changelog'; |
| 10 | +}; |
| 11 | + |
6 | 12 | /** |
7 | 13 | * Provides raw markdown (MDX source) versions of each documentation page at the |
8 | 14 | * same route with `.md` appended, e.g. `/getting-started.md`. |
9 | 15 | * This enables llms.txt consumers to fetch LLM-friendly source content. |
10 | 16 | */ |
11 | 17 | export async function getStaticPaths() { |
12 | | - return allPages.map((page) => ({ |
| 18 | + const docPaths = allPages.map((page) => ({ |
13 | 19 | // For catch-all `[...slug]` we must provide the full id as a string (not array), |
14 | 20 | // mirroring the pattern used in `[...slug].astro` so that `index` maps to /index.md |
15 | 21 | params: { slug: page.id }, |
16 | | - props: { id: page.id }, |
| 22 | + props: { id: page.id, collection: 'docs' } satisfies MarkdownSourceProps, |
17 | 23 | })); |
| 24 | + |
| 25 | + const changelogEntries = await getCollection('changelog'); |
| 26 | + const changelogPaths = changelogEntries.map((entry) => ({ |
| 27 | + params: { slug: `changelog/${entry.slug}` }, |
| 28 | + props: { id: entry.slug, collection: 'changelog' } satisfies MarkdownSourceProps, |
| 29 | + })); |
| 30 | + |
| 31 | + return [...docPaths, ...changelogPaths]; |
18 | 32 | } |
19 | 33 |
|
20 | 34 | export const GET: APIRoute = async ({ props }) => { |
21 | | - const { id } = props as { id: string }; |
22 | | - const fsPath = path.join(process.cwd(), 'src', 'content', 'docs', id + '.mdx'); |
| 35 | + const { id, collection } = props as MarkdownSourceProps; |
| 36 | + const fsPath = path.join(process.cwd(), 'src', 'content', collection, id + '.mdx'); |
23 | 37 | try { |
24 | 38 | let source = await readFile(fsPath, 'utf-8'); |
25 | 39 | if (!source.endsWith('\n')) source += '\n'; |
|
0 commit comments