|
| 1 | +export default async function middleware(request: Request) { |
| 2 | + const acceptHeader = request.headers.get('accept') || '' |
| 3 | + |
| 4 | + const linkHeaderValue = [ |
| 5 | + '</.well-known/api-catalog>; rel="api-catalog"', |
| 6 | + '</.well-known/agent-skills/index.json>; rel="agent-skills"', |
| 7 | + '</.well-known/mcp/server-card.json>; rel="mcp-server-card"', |
| 8 | + '</.well-known/oauth-authorization-server>; rel="oauth-authorization-server"', |
| 9 | + '</.well-known/oauth-protected-resource>; rel="oauth-protected-resource"', |
| 10 | + '</auth.md>; rel="authorizing-realm"', |
| 11 | + '</.well-known/ai-plugin.json>; rel="ai-plugin"', |
| 12 | + '</llms-full.txt>; rel="llms-full"', |
| 13 | + '</.well-known/security.txt>; rel="security-policy"', |
| 14 | + ].join(', ') |
| 15 | + |
| 16 | + if (acceptHeader.includes('text/markdown')) { |
| 17 | + const url = new URL(request.url) |
| 18 | + let pathname = url.pathname |
| 19 | + |
| 20 | + if (pathname === '/' || pathname === '/index.html') { |
| 21 | + pathname = '/index.md' |
| 22 | + } else if (!pathname.endsWith('.md') && !pathname.includes('.')) { |
| 23 | + pathname = `${pathname}.md` |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + const markdownUrl = new URL(pathname, url.origin) |
| 28 | + const res = await fetch(markdownUrl) |
| 29 | + if (res.ok) { |
| 30 | + const text = await res.text() |
| 31 | + return new Response(text, { |
| 32 | + status: 200, |
| 33 | + headers: { |
| 34 | + 'Content-Type': 'text/markdown; charset=utf-8', |
| 35 | + 'x-markdown-tokens': '850', |
| 36 | + 'Link': linkHeaderValue, |
| 37 | + 'Vary': 'Accept', |
| 38 | + }, |
| 39 | + }) |
| 40 | + } |
| 41 | + } catch { |
| 42 | + // Fallback if fetch fails |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments