-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
40 lines (32 loc) · 1.41 KB
/
Copy pathproxy.ts
File metadata and controls
40 lines (32 loc) · 1.41 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
import { NextRequest, NextResponse } from 'next/server';
import { isMarkdownPreferred } from 'fumadocs-core/negotiation';
import { docsContentRoute } from '@/lib/shared';
/** Maps a page path (`/reference/configuration`) to its Markdown route. */
function contentPath(pathname: string): string {
return `${docsContentRoute}${pathname.replace(/\/(index)?$/, '')}/content.md`;
}
/**
* Redirects legacy `.html` URLs and serves Markdown for `.md` URLs and `Accept: text/markdown`.
*/
export default function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// The legacy VuePress site served `/page.html`, and the app, CLI and WordPress plugin link to
// those URLs. Browsers keep the `#fragment` across the redirect, and the headings keep their ids.
if (pathname.endsWith('.html')) {
const url = request.nextUrl.clone();
url.pathname = pathname.replace(/(\/index)?\.html$/, '') || '/';
return NextResponse.redirect(url, 301);
}
if (pathname.endsWith('.md')) {
return NextResponse.rewrite(new URL(contentPath(pathname.slice(0, -'.md'.length)), request.nextUrl));
}
if (isMarkdownPreferred(request)) {
return NextResponse.rewrite(new URL(contentPath(pathname), request.nextUrl), {
headers: { Vary: 'Accept' },
});
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api/|_next/|llms|images/|.*\\.(?:png|ico|svg|webmanifest|txt|xml|woff2?)$).*)'],
};