import fs from "fs"; import path from "path"; import Link from "next/link"; import { notFound } from "next/navigation"; import { VerseIndex } from "./verse-index"; type Synonym = { word: string; meanings: string[]; }; type Verse = { book_title: string; canto: number; chapter: number; verse: number; verse_end?: number | null; reference: string; devanagari: string; transliteration: string; synonyms: Synonym[]; translation: string; purport: string[]; }; type ChapterInfo = { number: number; title: string; totalVerses: number; }; type CantoInfo = { number: number; title: string; totalChapters: number; chapters: ChapterInfo[]; }; type Intro = { book: string; title: string; author: string; totalCantos: number; cantos: CantoInfo[]; }; function getChapterVerses(canto: string, chapter: string): Verse[] { const dir = path.join( process.cwd(), "app/lib/books/sb/cantos", canto.padStart(2, "0"), "chapters", chapter.padStart(2, "0"), "verses", ); if (!fs.existsSync(dir)) return []; return fs .readdirSync(dir) .filter((f) => f.endsWith(".json")) .sort((a, b) => { const na = parseInt(a); const nb = parseInt(b); return na - nb; }) .map((f) => JSON.parse(fs.readFileSync(path.join(dir, f), "utf-8"))); } function getCantoAndChapterInfo( canto: string, chapter: string, ): { cantoInfo: CantoInfo; chapterInfo: ChapterInfo } | null { const introPath = path.join(process.cwd(), "app/lib/books/sb/intro.json"); const intro: Intro = JSON.parse(fs.readFileSync(introPath, "utf-8")); const cantoInfo = intro.cantos.find( (c) => c.number === parseInt(canto), ); if (!cantoInfo) return null; const chapterInfo = cantoInfo.chapters.find( (ch) => ch.number === parseInt(chapter), ); if (!chapterInfo) return null; return { cantoInfo, chapterInfo }; } export default async function SbChapterPage({ params, }: { params: Promise<{ canto: string; chapter: string }>; }) { const { canto, chapter } = await params; const info = getCantoAndChapterInfo(canto, chapter); if (!info) notFound(); const { cantoInfo, chapterInfo } = info; const verses = getChapterVerses(canto, chapter); if (verses.length === 0) notFound(); const verseList = verses.map((v) => ({ verse: v.verse, verse_end: v.verse_end ?? undefined, reference: v.reference, })); return (
← Canto {cantoInfo.number}: {cantoInfo.title}

Chapter {chapter}: {chapterInfo.title}

{/* Left sidebar — verse index */} {/* Main content */}
{verses.map((verse) => (
{/* Header */}
{verse.reference}
{/* Devanagari */}

{verse.devanagari}

{/* Transliteration */}

{verse.transliteration}

{/* Synonyms */} {verse.synonyms.length > 0 && (

Synonyms

{verse.synonyms.map((s, i) => ( {s.word} —{s.meanings.join(", ")} {i < verse.synonyms.length - 1 ? "; " : "."} ))}

)} {/* Translation */}

Translation

{/* Purport */} {verse.purport.length > 0 && (

Purport

{verse.purport.map((p, i) => (

))}

)}
))}
); }