243 lines
6.9 KiB
TypeScript
243 lines
6.9 KiB
TypeScript
export const dynamic = "force-dynamic";
|
|
|
|
import Link from "next/link";
|
|
import { notFound } from "next/navigation";
|
|
import { PageHeader } from "@/components/page-header";
|
|
import { Card, CardContent } from "@/components/ui/card";
|
|
import { buttonVariants } from "@/components/ui/button";
|
|
import { ChevronLeft, ChevronRight } from "lucide-react";
|
|
import { cn } from "@/lib/utils";
|
|
import { prisma } from "@/lib/db";
|
|
import type { JsonValue } from "@prisma/client/runtime/library";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// HTML sanitisation (same pattern used across all readers)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function sanitizeHtml(html: string): string {
|
|
let clean = html.replace(/<script[\s>][\s\S]*?<\/script>/gi, "");
|
|
clean = clean.replace(/<style[\s>][\s\S]*?<\/style>/gi, "");
|
|
clean = clean.replace(/<iframe[\s>][\s\S]*?<\/iframe>/gi, "");
|
|
clean = clean.replace(
|
|
/<\/?(?:script|style|iframe|object|embed|form|input|textarea|select|button)[^>]*>/gi,
|
|
"",
|
|
);
|
|
clean = clean.replace(/\s+on\w+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi, "");
|
|
clean = clean.replace(/href\s*=\s*"javascript:[^"]*"/gi, 'href=""');
|
|
clean = clean.replace(/href\s*=\s*'javascript:[^']*'/gi, "href=''");
|
|
return clean;
|
|
}
|
|
|
|
function normalizeParagraphs(paragraphs: string[] | JsonValue): string[] {
|
|
let result: string[];
|
|
|
|
if (typeof paragraphs === "string") {
|
|
try {
|
|
const parsed = JSON.parse(paragraphs);
|
|
result = Array.isArray(parsed) ? parsed : [paragraphs];
|
|
} catch {
|
|
result = [paragraphs];
|
|
}
|
|
} else if (Array.isArray(paragraphs)) {
|
|
result = paragraphs as string[];
|
|
} else {
|
|
result = [];
|
|
}
|
|
|
|
return result.map(sanitizeHtml);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Data fetching
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type EssayData = {
|
|
slug: string;
|
|
title: string;
|
|
author: string;
|
|
year: number | null;
|
|
group: string | null;
|
|
paragraphs: string[];
|
|
};
|
|
|
|
type SidebarItem = {
|
|
slug: string;
|
|
title: string;
|
|
};
|
|
|
|
async function getEssay(slug: string): Promise<EssayData | null> {
|
|
const doc = await prisma.essay.findUnique({ where: { slug } });
|
|
if (!doc) return null;
|
|
return {
|
|
slug: doc.slug,
|
|
title: doc.title,
|
|
author: doc.author,
|
|
year: doc.year,
|
|
group: doc.group,
|
|
paragraphs: normalizeParagraphs(doc.paragraphs),
|
|
};
|
|
}
|
|
|
|
async function getEssaySidebar(): Promise<SidebarItem[]> {
|
|
return prisma.essay.findMany({
|
|
orderBy: [{ group: "asc" }, { order: "asc" }],
|
|
select: { slug: true, title: true },
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Page
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export default async function EssayPage({
|
|
params,
|
|
}: {
|
|
params: Promise<{ slug: string }>;
|
|
}) {
|
|
const { slug } = await params;
|
|
|
|
const essay = await getEssay(slug);
|
|
if (!essay) notFound();
|
|
|
|
const allEssays = await getEssaySidebar();
|
|
const currentIdx = allEssays.findIndex((item) => item.slug === slug);
|
|
|
|
const prev = currentIdx > 0 ? allEssays[currentIdx - 1] : null;
|
|
const next =
|
|
currentIdx < allEssays.length - 1 ? allEssays[currentIdx + 1] : null;
|
|
|
|
return (
|
|
<div className="min-h-screen bg-background">
|
|
<PageHeader sticky wide>
|
|
<Link
|
|
href="/essays"
|
|
className="text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
← Essays
|
|
</Link>
|
|
<h1 className="mt-3 text-2xl font-bold text-foreground">
|
|
{essay.title}
|
|
</h1>
|
|
</PageHeader>
|
|
|
|
<div className="w-full md:max-w-6xl mx-auto flex">
|
|
{/* Left sidebar */}
|
|
<EssaySidebar items={allEssays} currentSlug={slug} />
|
|
|
|
{/* Main content */}
|
|
<main className="flex-1 min-w-0 px-6 py-8 space-y-8">
|
|
{/* Top navigation */}
|
|
<EssayNav prev={prev} next={next} />
|
|
|
|
{/* Content */}
|
|
<Card>
|
|
<CardContent className="py-8 px-6 sm:px-8">
|
|
<div className="space-y-4">
|
|
{essay.paragraphs.map((paragraph, i) => (
|
|
<div
|
|
key={i}
|
|
className="text-foreground leading-relaxed"
|
|
dangerouslySetInnerHTML={{ __html: paragraph }}
|
|
/>
|
|
))}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Bottom navigation */}
|
|
<EssayNav prev={prev} next={next} />
|
|
</main>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sidebar
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function EssaySidebar({
|
|
items,
|
|
currentSlug,
|
|
}: {
|
|
items: SidebarItem[];
|
|
currentSlug: string;
|
|
}) {
|
|
return (
|
|
<aside className="hidden lg:block w-56 shrink-0 sticky top-[89px] self-start max-h-[calc(100vh-89px)] overflow-y-auto py-8 pr-4">
|
|
<h2 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-3 px-2">
|
|
Essays
|
|
</h2>
|
|
<nav className="flex flex-col gap-0.5">
|
|
{items.map((item) => (
|
|
<Link
|
|
key={item.slug}
|
|
href={`/essays/${item.slug}`}
|
|
className={cn(
|
|
"px-2 py-1.5 rounded-md text-sm transition-colors truncate",
|
|
item.slug === currentSlug
|
|
? "bg-primary/10 text-primary font-medium"
|
|
: "text-muted-foreground hover:bg-muted hover:text-foreground",
|
|
)}
|
|
title={item.title}
|
|
>
|
|
{item.title}
|
|
</Link>
|
|
))}
|
|
</nav>
|
|
</aside>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Prev / Next navigation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function EssayNav({
|
|
prev,
|
|
next,
|
|
}: {
|
|
prev: SidebarItem | null;
|
|
next: SidebarItem | null;
|
|
}) {
|
|
return (
|
|
<div className="flex items-center justify-between gap-3">
|
|
{prev ? (
|
|
<Link
|
|
href={`/essays/${prev.slug}`}
|
|
className={cn(
|
|
buttonVariants({ variant: "outline", size: "sm" }),
|
|
"gap-1.5",
|
|
)}
|
|
>
|
|
<ChevronLeft className="size-4" />
|
|
<span className="hidden sm:inline truncate max-w-[180px]">
|
|
{prev.title}
|
|
</span>
|
|
<span className="sm:hidden">Previous</span>
|
|
</Link>
|
|
) : (
|
|
<div />
|
|
)}
|
|
|
|
{next ? (
|
|
<Link
|
|
href={`/essays/${next.slug}`}
|
|
className={cn(
|
|
buttonVariants({ variant: "outline", size: "sm" }),
|
|
"gap-1.5",
|
|
)}
|
|
>
|
|
<span className="hidden sm:inline truncate max-w-[180px]">
|
|
{next.title}
|
|
</span>
|
|
<span className="sm:hidden">Next</span>
|
|
<ChevronRight className="size-4" />
|
|
</Link>
|
|
) : (
|
|
<div />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|