|
| 1 | +import { OPENPANEL_BASE_URL } from '@/lib/openpanel-brand'; |
| 2 | +import { articleSource, guideSource, pageSource, source } from '@/lib/source'; |
| 3 | +import { NextResponse } from 'next/server'; |
| 4 | + |
| 5 | +const ALLOWED_PAGE_PATHS = new Set([ |
| 6 | + 'privacy', |
| 7 | + 'terms', |
| 8 | + 'about', |
| 9 | + 'contact', |
| 10 | + 'cookies', |
| 11 | +]); |
| 12 | + |
| 13 | +export const runtime = 'nodejs'; |
| 14 | + |
| 15 | +function stubMarkdown(canonicalUrl: string, path: string): string { |
| 16 | + return `# ${path}\n\nThis page is available at: [${canonicalUrl}](${canonicalUrl})\n`; |
| 17 | +} |
| 18 | + |
| 19 | +async function getProcessedText(page: { |
| 20 | + data: { getText?: (type: 'processed' | 'raw') => Promise<string> }; |
| 21 | +}): Promise<string | null> { |
| 22 | + try { |
| 23 | + const getText = page.data.getText; |
| 24 | + if (typeof getText === 'function') { |
| 25 | + return await getText('processed'); |
| 26 | + } |
| 27 | + } catch { |
| 28 | + // ignore |
| 29 | + } |
| 30 | + return null; |
| 31 | +} |
| 32 | + |
| 33 | +export async function GET(request: Request) { |
| 34 | + const url = new URL(request.url); |
| 35 | + const pathname = url.pathname; |
| 36 | + |
| 37 | + // Rewrites preserve the original request URL, so pathname is e.g. /docs/foo.md |
| 38 | + // Derive path from pathname when present; otherwise use query (e.g. /md?path=...) |
| 39 | + const pathParam = pathname.endsWith('.md') |
| 40 | + ? pathname.slice(0, -3) |
| 41 | + : url.searchParams.get('path'); |
| 42 | + |
| 43 | + if (!pathParam || pathParam.includes('..')) { |
| 44 | + return NextResponse.json({ error: 'Invalid path' }, { status: 400 }); |
| 45 | + } |
| 46 | + |
| 47 | + const path = pathParam.startsWith('/') ? pathParam : `/${pathParam}`; |
| 48 | + |
| 49 | + if (path.startsWith('/docs')) { |
| 50 | + const slug = path |
| 51 | + .replace(/^\/docs\/?/, '') |
| 52 | + .split('/') |
| 53 | + .filter(Boolean); |
| 54 | + const page = source.getPage(slug); |
| 55 | + if (!page) { |
| 56 | + return new NextResponse('Not found', { status: 404 }); |
| 57 | + } |
| 58 | + const processed = await page.data.getText('processed'); |
| 59 | + const canonical = `${OPENPANEL_BASE_URL}${page.url}`; |
| 60 | + const body = `# ${page.data.title}\n\nURL: ${canonical}\n\n${processed}`; |
| 61 | + return new Response(body, { |
| 62 | + headers: { 'Content-Type': 'text/markdown; charset=utf-8' }, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + if (path.startsWith('/articles')) { |
| 67 | + const slug = path |
| 68 | + .replace(/^\/articles\/?/, '') |
| 69 | + .split('/') |
| 70 | + .filter(Boolean); |
| 71 | + if (slug.length === 0) |
| 72 | + return new NextResponse('Not found', { status: 404 }); |
| 73 | + const page = articleSource.getPage(slug); |
| 74 | + if (!page) { |
| 75 | + return new NextResponse('Not found', { status: 404 }); |
| 76 | + } |
| 77 | + const text = await getProcessedText(page); |
| 78 | + const canonical = `${OPENPANEL_BASE_URL}${page.url}`; |
| 79 | + const body = text |
| 80 | + ? `# ${page.data.title}\n\nURL: ${canonical}\n\n${text}` |
| 81 | + : stubMarkdown(canonical, path); |
| 82 | + return new Response(body, { |
| 83 | + headers: { 'Content-Type': 'text/markdown; charset=utf-8' }, |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + if (path.startsWith('/guides')) { |
| 88 | + const slug = path |
| 89 | + .replace(/^\/guides\/?/, '') |
| 90 | + .split('/') |
| 91 | + .filter(Boolean); |
| 92 | + if (slug.length === 0) |
| 93 | + return new NextResponse('Not found', { status: 404 }); |
| 94 | + const page = guideSource.getPage(slug); |
| 95 | + if (!page) { |
| 96 | + return new NextResponse('Not found', { status: 404 }); |
| 97 | + } |
| 98 | + const text = await getProcessedText(page); |
| 99 | + const canonical = `${OPENPANEL_BASE_URL}${page.url}`; |
| 100 | + const body = text |
| 101 | + ? `# ${page.data.title}\n\nURL: ${canonical}\n\n${text}` |
| 102 | + : stubMarkdown(canonical, path); |
| 103 | + return new Response(body, { |
| 104 | + headers: { 'Content-Type': 'text/markdown; charset=utf-8' }, |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + if ( |
| 109 | + path === '/' || |
| 110 | + (path.startsWith('/') && path.split('/').filter(Boolean).length === 1) |
| 111 | + ) { |
| 112 | + const segment = path.replace(/^\//, ''); |
| 113 | + const slug = segment ? [segment] : []; |
| 114 | + const page = slug.length ? pageSource.getPage(slug) : null; |
| 115 | + if (page) { |
| 116 | + try { |
| 117 | + const getText = ( |
| 118 | + page.data as { getText?: (mode: string) => Promise<string> } |
| 119 | + ).getText; |
| 120 | + if (typeof getText === 'function') { |
| 121 | + const processed = await getText('processed'); |
| 122 | + const canonical = `${OPENPANEL_BASE_URL}${page.url}`; |
| 123 | + const body = `# ${page.data.title}\n\nURL: ${canonical}\n\n${processed}`; |
| 124 | + return new Response(body, { |
| 125 | + headers: { 'Content-Type': 'text/markdown; charset=utf-8' }, |
| 126 | + }); |
| 127 | + } |
| 128 | + } catch { |
| 129 | + // fall through to stub if getText not available |
| 130 | + } |
| 131 | + if (ALLOWED_PAGE_PATHS.has(segment)) { |
| 132 | + return new Response( |
| 133 | + stubMarkdown(`${OPENPANEL_BASE_URL}/${segment}`, path), |
| 134 | + { |
| 135 | + headers: { 'Content-Type': 'text/markdown; charset=utf-8' }, |
| 136 | + }, |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + if (ALLOWED_PAGE_PATHS.has(segment)) { |
| 141 | + return new Response( |
| 142 | + stubMarkdown(`${OPENPANEL_BASE_URL}/${segment}`, path), |
| 143 | + { |
| 144 | + headers: { 'Content-Type': 'text/markdown; charset=utf-8' }, |
| 145 | + }, |
| 146 | + ); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return new NextResponse('Not found', { status: 404 }); |
| 151 | +} |
0 commit comments