diff --git a/app/(app)/transcriptions/book/[book]/book-browser.tsx b/app/(app)/transcriptions/book/[book]/book-browser.tsx index e11ae9ff..4851bfa4 100644 --- a/app/(app)/transcriptions/book/[book]/book-browser.tsx +++ b/app/(app)/transcriptions/book/[book]/book-browser.tsx @@ -8,7 +8,7 @@ import { buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { ChevronLeft, ChevronRight, Menu, Calendar, Clock, MapPin } from "lucide-react"; import { PlayAudioButton } from "@/app/(app)/transcriptions/[slug]/play-audio-button"; -import { parseChapter } from "@/lib/book-reference"; +import { parseChapter, parseVerse } from "@/lib/book-reference"; import { Drawer, DrawerClose, @@ -105,7 +105,11 @@ function buildSections( .map(([k, v]) => ({ key: k, label: v.label, - transcriptions: v.items, + // Order lectures by verse ascending (e.g. 2.1, 2.3, 2.4…), + // irrespective of year. Stable sort keeps year order within a verse. + transcriptions: v.items + .slice() + .sort((a, b) => parseVerse(a.reference) - parseVerse(b.reference)), })), })); } diff --git a/lib/book-reference.ts b/lib/book-reference.ts index deba3fb6..579f05bc 100644 --- a/lib/book-reference.ts +++ b/lib/book-reference.ts @@ -28,6 +28,20 @@ export function bookTitle(code: string): string { return b ? b.title : ""; } +/** + * Extract the (starting) verse number from a reference, for ordering lectures + * within a chapter. The verse is the last dotted numeric component, and for a + * range we take the start: "BG 2.13" → 13, "SB 1.2.3" → 3, "SB 3.27.11–16" → + * 11, "CC Madhya-līlā 20.101" → 101, "ISO Mantra 4" → 4. References with no + * number (e.g. bare "NOD") sort last (stable → keep their incoming order). + */ +export function parseVerse(reference: string | null): number { + const m = (reference ?? "").trim().match(/(\d+(?:\.\d+)*)/); + if (!m) return Number.MAX_SAFE_INTEGER; + const parts = m[1].split("."); + return Number(parts[parts.length - 1]); +} + /** A section is a canto (SB) or līlā (CC); null for books without that level. */ export type ChapterSection = { key: string;