|
| 1 | +import type { SerializedEditorState } from "lexical"; |
| 2 | +import { getPayload, type Payload, type Where } from "payload"; |
| 3 | +import { |
| 4 | + convertLexicalToMarkdown, |
| 5 | + editorConfigFactory, |
| 6 | +} from "@payloadcms/richtext-lexical"; |
| 7 | +import config from "@payload-config"; |
| 8 | +import { |
| 9 | + markdownResponse, |
| 10 | + renderBlogIndexMarkdown, |
| 11 | + renderBlogPostMarkdown, |
| 12 | + renderHomeMarkdown, |
| 13 | +} from "@/lib/markdown-pages"; |
| 14 | +import { SITE_URL } from "@/lib/site-content"; |
| 15 | + |
| 16 | +export const dynamic = "force-dynamic"; |
| 17 | + |
| 18 | +const POSTS_WITH_SLUG = { |
| 19 | + and: [ |
| 20 | + { slug: { exists: true } }, |
| 21 | + { slug: { not_equals: "" } }, |
| 22 | + ], |
| 23 | +} satisfies Where; |
| 24 | + |
| 25 | +type RouteContext = { |
| 26 | + params: Promise<{ path?: string[] }>; |
| 27 | +}; |
| 28 | + |
| 29 | +function isSerializedEditorState(value: unknown): value is SerializedEditorState { |
| 30 | + if (!value || typeof value !== "object" || !("root" in value)) return false; |
| 31 | + |
| 32 | + const root = value.root; |
| 33 | + return Boolean( |
| 34 | + root && |
| 35 | + typeof root === "object" && |
| 36 | + "type" in root && |
| 37 | + root.type === "root" && |
| 38 | + "children" in root && |
| 39 | + Array.isArray(root.children), |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function getPostEditorConfig(payload: Payload) { |
| 44 | + const postsCollection = payload.config.collections.find( |
| 45 | + (collection) => collection.slug === "posts", |
| 46 | + ); |
| 47 | + const contentField = postsCollection?.fields.find( |
| 48 | + (field) => "name" in field && field.name === "content", |
| 49 | + ); |
| 50 | + |
| 51 | + if (!contentField || contentField.type !== "richText") { |
| 52 | + throw new Error("The posts.content rich text field is not configured."); |
| 53 | + } |
| 54 | + |
| 55 | + return editorConfigFactory.fromField({ field: contentField }); |
| 56 | +} |
| 57 | + |
| 58 | +function notFoundResponse() { |
| 59 | + return markdownResponse( |
| 60 | + "# 404\n\nThe requested page does not exist.", |
| 61 | + "404.md", |
| 62 | + 404, |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +export async function GET(_request: Request, { params }: RouteContext) { |
| 67 | + const { path = [] } = await params; |
| 68 | + const origin = SITE_URL; |
| 69 | + const isHome = path.length === 0 || (path.length === 1 && path[0] === "index"); |
| 70 | + const isBlogIndex = path.length === 1 && path[0] === "blog"; |
| 71 | + const isBlogPost = path.length === 2 && path[0] === "blog"; |
| 72 | + |
| 73 | + if (!isHome && !isBlogIndex && !isBlogPost) return notFoundResponse(); |
| 74 | + |
| 75 | + try { |
| 76 | + const payload = await getPayload({ config }); |
| 77 | + |
| 78 | + if (isHome) { |
| 79 | + const { docs: posts } = await payload.find({ |
| 80 | + collection: "posts", |
| 81 | + where: POSTS_WITH_SLUG, |
| 82 | + sort: "-publishedAt", |
| 83 | + limit: 5, |
| 84 | + }); |
| 85 | + |
| 86 | + return markdownResponse( |
| 87 | + renderHomeMarkdown(posts, origin), |
| 88 | + "index.md", |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + if (isBlogIndex) { |
| 93 | + const { docs: posts } = await payload.find({ |
| 94 | + collection: "posts", |
| 95 | + where: POSTS_WITH_SLUG, |
| 96 | + sort: "-publishedAt", |
| 97 | + pagination: false, |
| 98 | + depth: 1, |
| 99 | + }); |
| 100 | + |
| 101 | + return markdownResponse( |
| 102 | + renderBlogIndexMarkdown(posts, origin), |
| 103 | + "blog.md", |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + const slug = path[1]; |
| 108 | + const { |
| 109 | + docs: [post], |
| 110 | + } = await payload.find({ |
| 111 | + collection: "posts", |
| 112 | + where: { slug: { equals: slug } }, |
| 113 | + limit: 1, |
| 114 | + depth: 2, |
| 115 | + }); |
| 116 | + |
| 117 | + if (!post) return notFoundResponse(); |
| 118 | + |
| 119 | + if (!isSerializedEditorState(post.content)) { |
| 120 | + throw new Error(`Post "${post.slug}" has invalid rich text content.`); |
| 121 | + } |
| 122 | + |
| 123 | + const body = convertLexicalToMarkdown({ |
| 124 | + data: post.content, |
| 125 | + editorConfig: getPostEditorConfig(payload), |
| 126 | + }); |
| 127 | + |
| 128 | + return markdownResponse( |
| 129 | + renderBlogPostMarkdown(post, body, origin), |
| 130 | + `${post.slug}.md`, |
| 131 | + ); |
| 132 | + } catch (error) { |
| 133 | + console.error("Failed to render Markdown page", error); |
| 134 | + return markdownResponse( |
| 135 | + "# 500\n\nThe Markdown version of this page is temporarily unavailable.", |
| 136 | + "500.md", |
| 137 | + 500, |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments