-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathmiddleware.ts
More file actions
47 lines (40 loc) · 1.21 KB
/
middleware.ts
File metadata and controls
47 lines (40 loc) · 1.21 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Check if the request accepts markdown
const acceptHeader = request.headers.get("accept") || "";
const wantsMarkdown =
acceptHeader.includes("text/markdown") ||
acceptHeader.includes("text/x-markdown");
// If the client accepts markdown and this is a docs page, rewrite to markdown API
if (wantsMarkdown && isDocsPage(pathname)) {
const url = request.nextUrl.clone();
url.pathname = `/api/markdown${pathname}`;
return NextResponse.rewrite(url);
}
return NextResponse.next();
}
/**
* Check if a path is a documentation page that can be served as markdown
*/
function isDocsPage(pathname: string): boolean {
// Match documentation paths
const docPatterns = [
/^\/primitives\/docs\//,
/^\/themes\/docs\//,
/^\/colors\/docs\//,
/^\/blog\//,
];
return docPatterns.some((pattern) => pattern.test(pathname));
}
// Configure which paths the middleware runs on
export const config = {
matcher: [
// Match all docs paths
"/primitives/docs/:path*",
"/themes/docs/:path*",
"/colors/docs/:path*",
"/blog/:path*",
],
};