Nico Albanese fb0b95a0d3
feat: add markdown export API for shared pages (#767)
* feat: add markdown export API for shared pages

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

* feat: add data-snippet rendering in markdown export

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

---------

Co-authored-by: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>
2026-04-15 15:45:04 +02:00

36 lines
910 B
TypeScript

import { NextResponse, type NextRequest } from "next/server";
function wantsSharedMarkdown(acceptHeader: string | null): boolean {
if (!acceptHeader) {
return false;
}
const accept = acceptHeader.toLowerCase();
return accept.includes("text/markdown") || accept.includes("text/plain");
}
export function proxy(request: NextRequest) {
if (request.method !== "GET") {
return NextResponse.next();
}
const pathname = request.nextUrl.pathname;
const segments = pathname.split("/").filter(Boolean);
if (
segments.length === 2 &&
segments[0] === "shared" &&
wantsSharedMarkdown(request.headers.get("accept"))
) {
const rewrittenUrl = request.nextUrl.clone();
rewrittenUrl.pathname = `/api/shared/${segments[1]}/markdown`;
return NextResponse.rewrite(rewrittenUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/shared/:path*"],
};