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(/][\s\S]*?<\/script>/gi, ""); clean = clean.replace(/][\s\S]*?<\/style>/gi, ""); clean = clean.replace(/][\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 { 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 { 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 (
← Essays

{essay.title}

{/* Left sidebar */} {/* Main content */}
{/* Top navigation */} {/* Content */}
{essay.paragraphs.map((paragraph, i) => (
))}
{/* Bottom navigation */}
); } // --------------------------------------------------------------------------- // Sidebar // --------------------------------------------------------------------------- function EssaySidebar({ items, currentSlug, }: { items: SidebarItem[]; currentSlug: string; }) { return ( ); } // --------------------------------------------------------------------------- // Prev / Next navigation // --------------------------------------------------------------------------- function EssayNav({ prev, next, }: { prev: SidebarItem | null; next: SidebarItem | null; }) { return (
{prev ? ( {prev.title} Previous ) : (
)} {next ? ( {next.title} Next ) : (
)}
); }