-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathroute.ts
More file actions
48 lines (42 loc) · 1.31 KB
/
route.ts
File metadata and controls
48 lines (42 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { Node, Root } from 'fumadocs-core/page-tree';
import { rewriteCookbookUrl } from '@/lib/geistdocs/cookbook-source';
import { source } from '@/lib/geistdocs/source';
export const revalidate = false;
export const dynamic = 'force-static';
export async function GET(
_req: Request,
{ params }: RouteContext<'/[lang]/sitemap.md'>
) {
const { lang } = await params;
let mdText = '';
function traverseTree(node: Node | Root, depth = 0) {
const indent = ' '.repeat(depth);
if ('type' in node) {
if (node.type === 'page') {
mdText += `${indent}- [${node.name}](${rewriteCookbookUrl(node.url)})\n`;
} else if (node.type === 'folder') {
if (node.index) {
mdText += `${indent}- [${node.name}](${rewriteCookbookUrl(node.index.url)})\n`;
} else {
mdText += `${indent}- ${node.name}\n`;
}
if (node.children.length > 0) {
for (const child of node.children) {
traverseTree(child, depth + 1);
}
}
}
} else if (node.children.length > 0) {
for (const child of node.children) {
traverseTree(child, depth);
}
}
}
const tree = source.getPageTree(lang);
traverseTree(tree, 0);
return new Response(mdText, {
headers: {
'Content-Type': 'text/markdown',
},
});
}