forked from CaliCastle/cali.so
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67a9e4f
commit 61537e7
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { eq } from 'drizzle-orm' | ||
import { type Metadata } from 'next' | ||
import { notFound } from 'next/navigation' | ||
import { ReactMarkdown } from 'react-markdown/lib/react-markdown' | ||
|
||
import { Container } from '~/components/ui/Container' | ||
import { db } from '~/db' | ||
import { newsletters } from '~/db/schema' | ||
|
||
async function getNewsletter(id: string) { | ||
const [newsletter] = await db | ||
.select() | ||
.from(newsletters) | ||
.where(eq(newsletters.id, parseInt(id))) | ||
|
||
if (!newsletter || !newsletter.body) { | ||
notFound() | ||
} | ||
|
||
return newsletter | ||
} | ||
|
||
export async function generateMetadata({ params }: { params: { id: string } }) { | ||
const newsletter = await getNewsletter(params.id) | ||
|
||
const imageUrlRegex = /!\[[^\]]*\]\((.*?)\)/ | ||
const match = newsletter.body?.match(imageUrlRegex) | ||
let imageUrl: string | undefined = undefined | ||
|
||
if (match) { | ||
imageUrl = match[1] | ||
} | ||
|
||
return { | ||
title: newsletter.subject, | ||
openGraph: { | ||
images: imageUrl, | ||
title: newsletter.subject ?? '', | ||
type: 'article', | ||
}, | ||
twitter: { | ||
card: 'summary_large_image', | ||
title: newsletter.subject ?? '', | ||
images: imageUrl, | ||
}, | ||
} satisfies Metadata | ||
} | ||
|
||
export default async function NewsletterRenderPage({ | ||
params, | ||
}: { | ||
params: { id: string } | ||
}) { | ||
const newsletter = await getNewsletter(params.id) | ||
|
||
if (!newsletter.body) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Container className="mt-16"> | ||
<article className="prose mx-auto max-w-[500px] dark:prose-invert"> | ||
<ReactMarkdown>{newsletter.body}</ReactMarkdown> | ||
</article> | ||
</Container> | ||
) | ||
} | ||
|
||
export const revalidate = 3600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters