forked from speechmatics/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
23 lines (17 loc) · 856 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
23 lines (17 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export default function middleware(request: Request): Response | undefined {
const accept = request.headers.get("accept") ?? "";
if (!accept.includes("text/markdown")) return;
const url = new URL(request.url);
const { pathname } = url;
// Skip paths that already have a file extension
if (/\.[a-zA-Z0-9]+$/.test(pathname)) return;
// Root maps to /docs/index.md; everything else maps to /docs{pathname}.md.
// Section index pages (e.g. /speech-to-text → /docs/speech-to-text/index.md)
// are handled by the create-md-aliases build script, which copies each
// index.md to a flat sibling file so /docs/speech-to-text.md also exists.
url.pathname = pathname === "/" ? "/llms.txt" : `${pathname}.md`;
return Response.redirect(url.toString(), 302);
}
export const config = {
matcher: ["/((?!assets/|img/|js/|search/).*)"],
};