export const dynamic = "force-dynamic"; import type { Metadata } from "next"; import { redirect } from "next/navigation"; import { notFound } from "next/navigation"; import { getChapterVerseIndex } from "@/lib/api"; import { buildOgMeta } from "@/lib/og"; import { BOOK_NAMES } from "@/lib/constants"; import { Card, CardContent } from "@/components/ui/card"; import { PageHeader } from "@/components/layout/page-header"; import { FindInPage } from "@/components/layout/find-in-page"; import Link from "next/link"; import fs from "fs"; import path from "path"; const NBS_JSON_DIR = path.join(process.cwd(), "lib", "jsons", "books", "nbs"); type IntroSegment = { slug: string; title: string; paragraphs: string[]; }; export async function generateMetadata({ params, }: { params: Promise<{ chapter: string }>; }): Promise { const { chapter } = await params; return buildOgMeta({ title: BOOK_NAMES["nbs"], detail: `Chapter ${chapter}`, }); } export default async function NbsChapterPage({ params, }: { params: Promise<{ chapter: string }>; }) { const { chapter } = await params; // Handle intro segments (intro-introduction) if (chapter.startsWith("intro-")) { const rawSlug = chapter.replace(/^intro-/, ""); const filePath = path.join(NBS_JSON_DIR, "intro", `${rawSlug}.json`); if (!fs.existsSync(filePath)) notFound(); let segment: IntroSegment; try { segment = JSON.parse(fs.readFileSync(filePath, "utf-8")); } catch { notFound(); } return (
← Nārada-bhakti-sūtra

{segment.title}

{segment.paragraphs.map((paragraph: string, i: number) => (
))}
); } // Handle numeric chapters — redirect to first verse const chapterNum = parseInt(chapter); if (isNaN(chapterNum)) notFound(); // Try DB first const verseIndex = await getChapterVerseIndex("nbs", null, chapterNum); // Fallback to local JSON if (verseIndex.length === 0) { const versesDir = path.join( NBS_JSON_DIR, "chapters", String(chapterNum).padStart(2, "0"), "verses", ); if (fs.existsSync(versesDir)) { const files = fs .readdirSync(versesDir) .filter((f: string) => f.endsWith(".json")) .sort(); if (files.length > 0) { const first = JSON.parse( fs.readFileSync(path.join(versesDir, files[0]), "utf-8"), ); redirect(`/books/nbs/${chapter}/${first.verse}`); } } notFound(); } redirect(`/books/nbs/${chapter}/${verseIndex[0].verse}`); }