|
| 1 | +import { getCleanMd } from "docs-utils" |
| 2 | +import { existsSync } from "fs" |
| 3 | +import { unstable_cache } from "next/cache" |
| 4 | +import { notFound } from "next/navigation" |
| 5 | +import { NextRequest, NextResponse } from "next/server" |
| 6 | +import path from "path" |
| 7 | +import { |
| 8 | + addUrlToRelativeLink, |
| 9 | + crossProjectLinksPlugin, |
| 10 | + localLinksRehypePlugin, |
| 11 | +} from "remark-rehype-plugins" |
| 12 | +import type { Plugin } from "unified" |
| 13 | +import { filesMap } from "../../../generated/files-map.mjs" |
| 14 | +import { slugChanges } from "../../../generated/slug-changes.mjs" |
| 15 | + |
| 16 | +type Params = { |
| 17 | + params: Promise<{ slug: string[] }> |
| 18 | +} |
| 19 | + |
| 20 | +export async function GET(req: NextRequest, { params }: Params) { |
| 21 | + const { slug = ["/"] } = await params |
| 22 | + |
| 23 | + // keep this so that Vercel keeps the files in deployment |
| 24 | + path.join(process.cwd(), "app") |
| 25 | + path.join(process.cwd(), "references") |
| 26 | + |
| 27 | + const filePathFromMap = await getFileFromMaps(`/${slug.join("/")}`) |
| 28 | + if (!filePathFromMap) { |
| 29 | + return notFound() |
| 30 | + } |
| 31 | + |
| 32 | + const filePath = path.join(path.resolve("..", "..", ".."), filePathFromMap) |
| 33 | + |
| 34 | + if (!existsSync(filePath)) { |
| 35 | + return notFound() |
| 36 | + } |
| 37 | + |
| 38 | + const cleanMdContent = await getCleanMd_(filePath, { |
| 39 | + before: [ |
| 40 | + [ |
| 41 | + crossProjectLinksPlugin, |
| 42 | + { |
| 43 | + baseUrl: process.env.NEXT_PUBLIC_BASE_URL, |
| 44 | + projectUrls: { |
| 45 | + docs: { |
| 46 | + url: process.env.NEXT_PUBLIC_DOCS_URL, |
| 47 | + path: "", |
| 48 | + }, |
| 49 | + "user-guide": { |
| 50 | + url: process.env.NEXT_PUBLIC_USER_GUIDE_URL, |
| 51 | + }, |
| 52 | + ui: { |
| 53 | + url: process.env.NEXT_PUBLIC_UI_URL, |
| 54 | + }, |
| 55 | + api: { |
| 56 | + url: process.env.NEXT_PUBLIC_API_URL, |
| 57 | + }, |
| 58 | + }, |
| 59 | + useBaseUrl: |
| 60 | + process.env.NODE_ENV === "production" || |
| 61 | + process.env.VERCEL_ENV === "production", |
| 62 | + }, |
| 63 | + ], |
| 64 | + [localLinksRehypePlugin], |
| 65 | + ] as unknown as Plugin[], |
| 66 | + after: [ |
| 67 | + [addUrlToRelativeLink, { url: process.env.NEXT_PUBLIC_BASE_URL }], |
| 68 | + ] as unknown as Plugin[], |
| 69 | + }) |
| 70 | + |
| 71 | + return new NextResponse(cleanMdContent, { |
| 72 | + headers: { |
| 73 | + "Content-Type": "text/markdown", |
| 74 | + }, |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +const getCleanMd_ = unstable_cache( |
| 79 | + async (filePath: string, plugins?: { before?: Plugin[]; after?: Plugin[] }) => |
| 80 | + getCleanMd({ filePath, plugins }), |
| 81 | + ["clean-md"], |
| 82 | + { |
| 83 | + revalidate: 3600, |
| 84 | + } |
| 85 | +) |
| 86 | + |
| 87 | +const getFileFromMaps = unstable_cache( |
| 88 | + async (path: string) => { |
| 89 | + return ( |
| 90 | + slugChanges.find((slugChange) => slugChange.newSlug === path)?.filePath || |
| 91 | + filesMap.find((file) => file.pathname === path)?.filePath |
| 92 | + ) |
| 93 | + }, |
| 94 | + ["file-map"], |
| 95 | + { |
| 96 | + revalidate: 3600, |
| 97 | + } |
| 98 | +) |
0 commit comments