63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { notFound } from "next/navigation";
|
|
import { MarkdownViewer } from "@/components/MarkdownViewer";
|
|
import { SharePanel } from "@/components/SharePanel";
|
|
import { getDb } from "@/lib/mongo";
|
|
import { serializePost } from "@/lib/posts";
|
|
import { normalizeImageUrl } from "@/lib/normalize";
|
|
import { getSiteUrl } from "@/lib/site";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
type Props = {
|
|
params: { slug: string };
|
|
};
|
|
|
|
async function fetchPost(slug: string) {
|
|
const db = await getDb();
|
|
const doc = await db.collection("posts").findOne({ slug });
|
|
if (!doc) {
|
|
return null;
|
|
}
|
|
|
|
db.collection("posts").updateOne({ _id: doc._id }, { $inc: { views: 1 } }).catch(() => {});
|
|
return serializePost({ ...doc, views: (doc.views ?? 0) + 1 });
|
|
}
|
|
|
|
export default async function PostPage({ params }: Props) {
|
|
const post = await fetchPost(params.slug);
|
|
if (!post) {
|
|
notFound();
|
|
}
|
|
|
|
const coverUrl = normalizeImageUrl(post.cover);
|
|
const shareUrl = `${getSiteUrl()}/p/${post.slug}`;
|
|
|
|
return (
|
|
<article className="rounded-2xl bg-white/80 p-6 shadow-sm ring-1 ring-slate-100">
|
|
<div className="mb-4">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<h1 className="text-2xl font-semibold text-slate-900">{post.title}</h1>
|
|
<SharePanel url={shareUrl} />
|
|
</div>
|
|
<p className="mt-2 text-sm text-slate-500">
|
|
{post.author || "匿名"} |{" "}
|
|
{new Date(post.createdAt).toLocaleString("zh-CN", {
|
|
hour12: false,
|
|
timeZone: "Asia/Shanghai"
|
|
})}
|
|
</p>
|
|
{coverUrl ? (
|
|
<img
|
|
src={coverUrl}
|
|
alt=""
|
|
className="mt-3 w-full rounded-2xl object-cover"
|
|
loading="lazy"
|
|
referrerPolicy="no-referrer"
|
|
/>
|
|
) : null}
|
|
</div>
|
|
<MarkdownViewer markdown={post.markdown} />
|
|
</article>
|
|
);
|
|
}
|