Skip to content

Commit e93b670

Browse files
committed
Update route.ts
1 parent 9bb4f8d commit e93b670

File tree

1 file changed

+62
-34
lines changed

1 file changed

+62
-34
lines changed

app/api/md/[...slug]/route.ts

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { getPage } from '@/app/source';
1+
import { getPage, getPages } from '@/app/source';
22
import { NextRequest, NextResponse } from 'next/server';
3-
import fs from 'fs/promises';
4-
import path from 'path';
3+
import * as fs from 'fs/promises';
4+
import * as path from 'path';
55

66
export const dynamic = 'force-dynamic';
77
export const revalidate = 0;
@@ -79,39 +79,67 @@ export async function GET(
7979
request: NextRequest,
8080
{ params }: { params: { slug: string[] } }
8181
) {
82-
const slugParts = params.slug;
83-
console.log('MD route called with slug parts:', slugParts);
84-
85-
// Special case for the index page
86-
const isIndexRequest =
87-
(slugParts.length === 2 && slugParts[0] === 'docs' && slugParts[1] === 'index');
88-
89-
let page;
90-
let contentPath;
91-
92-
if (isIndexRequest) {
93-
// Direct path to index.mdx for the special case
94-
contentPath = path.join(process.cwd(), 'content/docs/index.mdx');
95-
} else {
96-
// Normal page lookup via getPage
97-
page = getPage(slugParts);
98-
if (!page) {
82+
try {
83+
const slugParts = params.slug;
84+
console.log('MD route called with slug parts:', slugParts);
85+
console.log('User-Agent:', request.headers.get('user-agent'));
86+
87+
// Special case for the index page
88+
const isIndexRequest =
89+
(slugParts.length === 2 && slugParts[0] === 'docs' && slugParts[1] === 'index');
90+
91+
let page;
92+
let contentPath;
93+
94+
if (isIndexRequest) {
95+
// Direct path to index.mdx for the special case
96+
contentPath = path.join(process.cwd(), 'content/docs/index.mdx');
97+
console.log('Using index path:', contentPath);
98+
} else {
99+
// Normal page lookup via getPage
100+
// Remove 'docs' prefix if present
101+
const pageSlug = slugParts[0] === 'docs' ? slugParts.slice(1) : slugParts;
102+
console.log('Looking up page with pageSlug:', pageSlug);
103+
page = getPage(pageSlug);
104+
console.log('Page found:', page ? 'yes' : 'no');
105+
106+
if (!page) {
107+
console.log('Page not found for pageSlug:', pageSlug);
108+
return NextResponse.json({
109+
error: 'Page not found',
110+
slugParts,
111+
pageSlug,
112+
availablePages: getPages().map(p => p.url)
113+
}, { status: 404 });
114+
}
115+
contentPath = path.join(process.cwd(), 'content/docs', page.file.path.replace(/^docs\//, ''));
116+
console.log('Using content path:', contentPath);
117+
}
118+
119+
// Get the content
120+
const content = await readMdxFile(contentPath);
121+
if (!content) {
122+
console.log('No content found at path:', contentPath);
99123
return NextResponse.json({
100-
error: 'Page not found',
101-
slugParts
124+
error: 'Content not found',
125+
contentPath
102126
}, { status: 404 });
103127
}
104-
contentPath = path.join(process.cwd(), 'content/docs', page.file.path.replace(/^docs\//, ''));
128+
129+
const markdownContent = processMdxToMarkdown(content);
130+
131+
return new NextResponse(markdownContent, {
132+
headers: {
133+
'Content-Type': 'text/markdown',
134+
'Content-Disposition': `inline; filename="${slugParts.join('-')}.md"`,
135+
},
136+
});
137+
} catch (error) {
138+
console.error('Error in MD route:', error);
139+
return NextResponse.json({
140+
error: 'Internal server error',
141+
message: error instanceof Error ? error.message : 'Unknown error',
142+
slugParts: params.slug
143+
}, { status: 500 });
105144
}
106-
107-
// Get the content
108-
const content = await readMdxFile(contentPath);
109-
const markdownContent = processMdxToMarkdown(content);
110-
111-
return new NextResponse(markdownContent, {
112-
headers: {
113-
'Content-Type': 'text/markdown',
114-
'Content-Disposition': `inline; filename="${slugParts.join('-')}.md"`,
115-
},
116-
});
117145
}

0 commit comments

Comments
 (0)