-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
35 lines (27 loc) · 1.12 KB
/
middleware.ts
File metadata and controls
35 lines (27 loc) · 1.12 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
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname.endsWith('.md')) {
const pathWithoutMd = pathname.slice(0, -3);
if (pathWithoutMd.match(/^\/apis\/[^/]+\/reference\//)) {
// Rewrite to our openapi-markdown API route
const apiPath = `/api/openapi-markdown${pathWithoutMd}`;
return NextResponse.rewrite(new URL(apiPath, request.url));
}
if (pathWithoutMd === '/index') {
return NextResponse.rewrite(new URL('/raw/index.mdx', request.url));
}
if (pathWithoutMd.startsWith('/docs/') || pathWithoutMd === '/docs') {
const docPath = pathWithoutMd.startsWith('/docs/') ? pathWithoutMd.slice(6) : '';
const rawPath = `/raw/${docPath || 'index'}.mdx`;
return NextResponse.rewrite(new URL(rawPath, request.url));
}
const rawPath = `/raw${pathWithoutMd}.mdx`;
return NextResponse.rewrite(new URL(rawPath, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/(.*).md', '/docs/(.*).md'],
};