fix: sva now extracts and reads properly

This commit is contained in:
gosangam
2026-05-03 14:46:35 +05:30
parent 47a2dc375e
commit 89106efc98
2 changed files with 106 additions and 152 deletions
+62 -90
View File
@@ -1,10 +1,10 @@
/**
* Data access layer for Songs of the Vaiṣṇava Ācāryas (SVA).
*
* Reads from MongoDB via Prisma.
* SVA is a song book with 4 parent parts, each containing multiple songs
* (sub-chapters). Each song is stored as its own prose_chapters document
* with slugs like "1-1", "1-2", "2-1", etc.
* Reads from MongoDB via Prisma, same as the rest of the app.
* SVA is a hierarchical song book with 4 parent parts, each containing
* multiple songs (sub-chapters). Each song is stored as its own
* prose_chapters document with slugs like "1-1", "1-2", "2-1", etc.
*
* When the DB isn't seeded yet, falls back to reading local JSON files
* from lib/jsons/books/sva/.
@@ -130,8 +130,8 @@ function normalizeParagraphs(paragraphs: string[] | JsonValue): string[] {
// ---------------------------------------------------------------------------
function readLocalJson<T>(filePath: string): T | null {
if (!fs.existsSync(filePath)) return null;
try {
if (!fs.existsSync(filePath)) return null;
return JSON.parse(fs.readFileSync(filePath, "utf-8")) as T;
} catch {
return null;
@@ -147,39 +147,19 @@ function getLocalIntroSegments(): SvaIntroSegment[] {
for (const file of files) {
const filePath = path.join(introDir, file);
const seg = readLocalJson<SvaIntroSegment>(filePath);
if (seg) results.push(seg);
if (seg) {
results.push({
...seg,
slug: seg.slug.startsWith("intro-") ? seg.slug : `intro-${seg.slug}`,
});
}
return results.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
}
function getLocalIntroSegment(rawSlug: string): SvaIntroSegment | null {
const filePath = path.join(SVA_JSON_DIR, "intro", `${rawSlug}.json`);
return readLocalJson<SvaIntroSegment>(filePath);
return results.sort((a, b) => a.order - b.order);
}
function getLocalSubChapter(slug: string): SvaChapter | null {
const filePath = path.join(SVA_JSON_DIR, "chapters", `${slug}.json`);
const raw = readLocalJson<{
book: string;
slug: string;
parentChapter: number;
subChapter: number;
title: string;
parentTitle: string;
type: string;
paragraphs: string[];
}>(filePath);
if (!raw) return null;
return {
book: raw.book,
chapter: raw.slug,
parentChapter: raw.parentChapter,
subChapter: raw.subChapter,
title: raw.title,
parentTitle: raw.parentTitle,
type: raw.type,
paragraphs: raw.paragraphs,
};
return readLocalJson<SvaChapter>(filePath);
}
// ---------------------------------------------------------------------------
@@ -194,7 +174,7 @@ export async function getSvaMeta(): Promise<SvaMeta | null> {
const meta = await prisma.bookMeta.findUnique({ where: { book: BOOK } });
if (meta) return meta.data as unknown as SvaMeta;
} catch {
// Prisma BSON decode error — fall through to local JSON
// fall through to local JSON
}
// Fallback to local JSON
@@ -227,7 +207,7 @@ export async function getSvaIntroSegments(): Promise<SvaIntroSegment[]> {
}));
}
} catch {
// Prisma BSON decode error — fall through to local JSON
// fall through to local JSON
}
// Fallback to local JSON
@@ -236,7 +216,7 @@ export async function getSvaIntroSegments(): Promise<SvaIntroSegment[]> {
/**
* A single intro segment by slug.
* Accepts both "intro-front-pages" (URL slug) and "front-pages" (raw slug).
* Accepts both "intro-foreword" (URL slug) and "foreword" (raw slug).
*/
export async function getSvaIntroSegment(
slug: string,
@@ -247,6 +227,7 @@ export async function getSvaIntroSegment(
? slug.replace(/^intro-/, "")
: slug;
try {
const doc = await prisma.proseChapter.findUnique({
where: {
book_volume_slug: { book: BOOK, volume: NO_VOLUME, slug: dbSlug },
@@ -263,9 +244,13 @@ export async function getSvaIntroSegment(
paragraphs: normalizeParagraphs(doc.paragraphs),
};
}
} catch {
// fall through to local JSON
}
// Fallback to local JSON
const local = getLocalIntroSegment(rawSlug);
const filePath = path.join(SVA_JSON_DIR, "intro", `${rawSlug}.json`);
const local = readLocalJson<SvaIntroSegment>(filePath);
if (!local) return null;
return {
...local,
@@ -275,7 +260,7 @@ export async function getSvaIntroSegment(
}
// ---------------------------------------------------------------------------
// Chapters (sub-chapters like "1-1", "2-3", etc.)
// Chapters (sub-chapters / songs like "1-1", "2-3", etc.)
// ---------------------------------------------------------------------------
/**
@@ -318,7 +303,7 @@ export async function getSvaChapter(slug: string): Promise<SvaChapter | null> {
};
}
} catch {
// Prisma BSON decode error — fall through to local JSON
// fall through to local JSON
}
// Fallback to local JSON
@@ -360,8 +345,10 @@ export async function getSvaContent(
/**
* Build the full sidebar navigation list with hierarchical structure.
*
* Order: front-pages → foreword → introduction
* → parent-1 (label) → 1-1, 1-2, ... → parent-2 (label) → 2-1, 2-2, ...
* Order: intro segments (front-pages, foreword, introduction)
* → parent-1 (label) → 1-1, 1-2, ...
* → parent-2 (label) → 2-1, 2-2, ...
* → etc.
*
* "parent" type items are non-clickable headings. Only "intro-segment" and
* "sub-chapter" items are navigable pages.
@@ -378,7 +365,7 @@ export async function getSvaSidebarItems(): Promise<SvaSidebarItem[]> {
const meta = await getSvaMeta();
try {
allDocs = await prisma.proseChapter.findMany({
const results = await prisma.proseChapter.findMany({
where: { book: BOOK, volume: NO_VOLUME },
orderBy: { order: "asc" },
select: {
@@ -389,21 +376,30 @@ export async function getSvaSidebarItems(): Promise<SvaSidebarItem[]> {
order: true,
},
});
allDocs = results;
} catch {
// Prisma BSON decode error — build sidebar from meta alone
// Prisma error — build sidebar from meta alone
}
if (!meta) {
// Try building from local JSON
return buildSidebarFromLocal();
const localMeta = readLocalJson<SvaMeta>(
path.join(SVA_JSON_DIR, "intro.json"),
);
if (!localMeta) return [];
return buildSidebarFromMeta(localMeta);
}
// If we have no DB docs, build from meta alone
if (allDocs.length === 0) {
return buildSidebarFromMeta(meta);
}
const items: SvaSidebarItem[] = [];
// Intro segments that come BEFORE chapters (order < 99)
if (allDocs.length > 0) {
for (const doc of allDocs) {
if (doc.type === "intro-segment" && (doc.order ?? 0) < 99) {
if (doc.type === "intro-segment" && doc.order < 99) {
items.push({
slug: doc.slug,
label: doc.title,
@@ -412,20 +408,6 @@ export async function getSvaSidebarItems(): Promise<SvaSidebarItem[]> {
});
}
}
} else {
// Fallback: local intro segments
const localIntros = getLocalIntroSegments();
for (const seg of localIntros) {
if ((seg.order ?? 0) < 99) {
items.push({
slug: `intro-${seg.slug}`,
label: seg.title,
type: "intro-segment",
parentChapter: null,
});
}
}
}
// Chapters — grouped by parent chapter with sub-chapters
for (const ch of meta.chapters) {
@@ -437,7 +419,7 @@ export async function getSvaSidebarItems(): Promise<SvaSidebarItem[]> {
parentChapter: ch.number,
});
// Sub-chapters (songs) under this parent
// Sub-chapters under this parent
for (const sub of ch.subChapters) {
items.push({
slug: sub.slug,
@@ -449,9 +431,8 @@ export async function getSvaSidebarItems(): Promise<SvaSidebarItem[]> {
}
// Intro segments that come AFTER chapters (order >= 99)
if (allDocs.length > 0) {
for (const doc of allDocs) {
if (doc.type === "intro-segment" && (doc.order ?? 0) >= 99) {
if (doc.type === "intro-segment" && doc.order >= 99) {
items.push({
slug: doc.slug,
label: doc.title,
@@ -460,32 +441,36 @@ export async function getSvaSidebarItems(): Promise<SvaSidebarItem[]> {
});
}
}
}
return items;
}
function buildSidebarFromLocal(): SvaSidebarItem[] {
/**
* Build sidebar items purely from metadata (when DB is unavailable).
*/
function buildSidebarFromMeta(meta: SvaMeta): SvaSidebarItem[] {
const items: SvaSidebarItem[] = [];
// Intro segments
const introSegs = getLocalIntroSegments();
for (const seg of introSegs) {
if ((seg.order ?? 0) < 99) {
// Hardcoded intro segments
items.push({
slug: `intro-${seg.slug}`,
label: seg.title,
slug: "intro-front-pages",
label: "Front Pages",
type: "intro-segment",
parentChapter: null,
});
items.push({
slug: "intro-foreword",
label: "Foreword",
type: "intro-segment",
parentChapter: null,
});
items.push({
slug: "intro-introduction",
label: "Introduction",
type: "intro-segment",
parentChapter: null,
});
}
}
// Load meta for chapter structure
const introPath = path.join(SVA_JSON_DIR, "intro.json");
const meta = readLocalJson<SvaMeta>(introPath);
if (meta?.chapters) {
for (const ch of meta.chapters) {
items.push({
slug: `parent-${ch.number}`,
@@ -503,19 +488,6 @@ function buildSidebarFromLocal(): SvaSidebarItem[] {
});
}
}
}
// Back matter
for (const seg of introSegs) {
if ((seg.order ?? 0) >= 99) {
items.push({
slug: `intro-${seg.slug}`,
label: seg.title,
type: "intro-segment",
parentChapter: null,
});
}
}
return items;
}
-18
View File
@@ -93,24 +93,6 @@ async function insertBatched(
for (let i = 0; i < docs.length; i++) {
const sanitized = sanitizeDoc(docs[i]) as Record<string, unknown>;
// Work around BSON driver bug with large string arrays:
// Store paragraphs as a single JSON-encoded string instead of a native
// array. The data layer (lib/sva.ts) already handles stringified JSON
// via normalizeParagraphs().
if (Array.isArray(sanitized.paragraphs)) {
// Stringify, then sanitize the resulting string to ensure clean UTF-8
let jsonStr = JSON.stringify(sanitized.paragraphs);
// NFC normalize the whole JSON string
jsonStr = jsonStr.normalize("NFC");
// Remove any stray control chars or surrogates
jsonStr = jsonStr.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]/g, "");
jsonStr = jsonStr.replace(/[\uD800-\uDFFF]/g, "");
jsonStr = jsonStr.replace(/[\u200B-\u200F\u2028-\u202F\uFEFF]/g, "");
// Round-trip through Buffer to guarantee valid UTF-8 byte sequence
jsonStr = Buffer.from(jsonStr, "utf-8").toString("utf-8");
sanitized.paragraphs = jsonStr;
}
try {
await col.insertOne(sanitized);
} catch (err) {