333 lines
8.6 KiB
TypeScript
333 lines
8.6 KiB
TypeScript
/**
|
||
* Data access layer for The Nectar of Devotion (NOD).
|
||
*
|
||
* Reads from MongoDB via Prisma, same as the rest of the app.
|
||
* NoD is a prose book (like KB) but has no volume structure —
|
||
* we use volume=0 as the sentinel for "no volumes".
|
||
*
|
||
* Collections used:
|
||
* - book_meta (via Prisma BookMeta model)
|
||
* - prose_chapters (via Prisma ProseChapter model)
|
||
*/
|
||
|
||
import { prisma } from "@/lib/db";
|
||
import type { JsonValue } from "@prisma/client/runtime/library";
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Constants
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/** Volume sentinel for books without volumes. */
|
||
const NO_VOLUME = 0;
|
||
|
||
const BOOK = "nod";
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Types
|
||
// ---------------------------------------------------------------------------
|
||
|
||
export type NodMeta = {
|
||
book: string;
|
||
title: string;
|
||
subtitle: string;
|
||
author: string;
|
||
year: number;
|
||
description: string;
|
||
totalChapters: number;
|
||
parts: NodPart[];
|
||
chapters: NodChapterEntry[];
|
||
};
|
||
|
||
export type NodPart = {
|
||
number: number;
|
||
title: string;
|
||
chapters: number[];
|
||
};
|
||
|
||
export type NodChapterEntry = {
|
||
number: number;
|
||
title: string;
|
||
};
|
||
|
||
export type NodIntroSegment = {
|
||
slug: string;
|
||
title: string;
|
||
type: string;
|
||
book: string;
|
||
order: number;
|
||
paragraphs: string[];
|
||
};
|
||
|
||
export type NodChapter = {
|
||
book: string;
|
||
chapter: number | null;
|
||
title: string;
|
||
type: string;
|
||
slug?: string;
|
||
paragraphs: string[];
|
||
};
|
||
|
||
/** Union type used in sidebar / table-of-contents navigation. */
|
||
export type NodSidebarItem = {
|
||
/** URL-safe slug: "intro-front-flap", "1", "51", "concluding-words" */
|
||
slug: string;
|
||
/** Human-readable label for the sidebar */
|
||
label: string;
|
||
/** "intro-segment" | "chapter" | "conclusion" */
|
||
type: string;
|
||
/** Chapter number, or null for non-chapter items */
|
||
chapter: number | null;
|
||
};
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// HTML sanitisation (mirrors lib/api.ts)
|
||
// ---------------------------------------------------------------------------
|
||
|
||
function sanitizeHtml(html: string): string {
|
||
let clean = html.replace(/<script[\s>][\s\S]*?<\/script>/gi, "");
|
||
clean = clean.replace(/<style[\s>][\s\S]*?<\/style>/gi, "");
|
||
clean = clean.replace(/<iframe[\s>][\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;
|
||
}
|
||
|
||
/** Normalize paragraphs — handles both native arrays and stringified JSON. */
|
||
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);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Book meta
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Book-level metadata (title, author, parts, chapter list, etc.)
|
||
*/
|
||
export async function getNodMeta(): Promise<NodMeta | null> {
|
||
const meta = await prisma.bookMeta.findUnique({ where: { book: BOOK } });
|
||
if (!meta) return null;
|
||
return meta.data as unknown as NodMeta;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Intro segments
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* All intro segments, ordered by their `order` field.
|
||
*/
|
||
export async function getNodIntroSegments(): Promise<NodIntroSegment[]> {
|
||
const results = await prisma.proseChapter.findMany({
|
||
where: { book: BOOK, volume: NO_VOLUME, type: "intro-segment" },
|
||
orderBy: { order: "asc" },
|
||
});
|
||
|
||
return results.map((doc) => ({
|
||
slug: doc.slug,
|
||
title: doc.title,
|
||
type: doc.type,
|
||
book: doc.book,
|
||
order: doc.order,
|
||
paragraphs: normalizeParagraphs(doc.paragraphs),
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* A single intro segment by slug.
|
||
* Accepts both "intro-preface" (URL slug) and "preface" (raw slug).
|
||
*/
|
||
export async function getNodIntroSegment(
|
||
slug: string,
|
||
): Promise<NodIntroSegment | null> {
|
||
// DB stores with "intro-" prefix; accept either form
|
||
const dbSlug = slug.startsWith("intro-") ? slug : `intro-${slug}`;
|
||
|
||
const doc = await prisma.proseChapter.findUnique({
|
||
where: {
|
||
book_volume_slug: { book: BOOK, volume: NO_VOLUME, slug: dbSlug },
|
||
},
|
||
});
|
||
if (!doc) return null;
|
||
|
||
return {
|
||
slug: doc.slug,
|
||
title: doc.title,
|
||
type: doc.type,
|
||
book: doc.book,
|
||
order: doc.order,
|
||
paragraphs: normalizeParagraphs(doc.paragraphs),
|
||
};
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Chapters
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* A numbered chapter (1–51).
|
||
*/
|
||
export async function getNodChapter(
|
||
chapterNum: number,
|
||
): Promise<NodChapter | null> {
|
||
if (chapterNum < 1 || chapterNum > 51 || !Number.isInteger(chapterNum)) {
|
||
return null;
|
||
}
|
||
|
||
const doc = await prisma.proseChapter.findUnique({
|
||
where: {
|
||
book_volume_slug: {
|
||
book: BOOK,
|
||
volume: NO_VOLUME,
|
||
slug: String(chapterNum),
|
||
},
|
||
},
|
||
});
|
||
if (!doc) return null;
|
||
|
||
return {
|
||
book: doc.book,
|
||
chapter: doc.chapter,
|
||
title: doc.title,
|
||
type: doc.type,
|
||
slug: doc.slug,
|
||
paragraphs: normalizeParagraphs(doc.paragraphs),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* The concluding words section.
|
||
*/
|
||
export async function getNodConcludingWords(): Promise<NodChapter | null> {
|
||
const doc = await prisma.proseChapter.findUnique({
|
||
where: {
|
||
book_volume_slug: {
|
||
book: BOOK,
|
||
volume: NO_VOLUME,
|
||
slug: "concluding-words",
|
||
},
|
||
},
|
||
});
|
||
if (!doc) return null;
|
||
|
||
return {
|
||
book: doc.book,
|
||
chapter: doc.chapter,
|
||
title: doc.title,
|
||
type: doc.type,
|
||
slug: doc.slug,
|
||
paragraphs: normalizeParagraphs(doc.paragraphs),
|
||
};
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Unified content accessor
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Get content by URL slug.
|
||
*
|
||
* "intro-front-flap" → intro segment
|
||
* "intro-preface" → intro segment
|
||
* "intro-introduction" → intro segment
|
||
* "1" … "51" → chapter
|
||
* "concluding-words" → concluding section
|
||
*/
|
||
export async function getNodContent(
|
||
slug: string,
|
||
): Promise<NodChapter | NodIntroSegment | null> {
|
||
if (slug.startsWith("intro-")) {
|
||
return getNodIntroSegment(slug);
|
||
}
|
||
|
||
if (slug === "concluding-words") {
|
||
return getNodConcludingWords();
|
||
}
|
||
|
||
const num = Number(slug);
|
||
if (!Number.isNaN(num) && Number.isInteger(num) && num >= 1 && num <= 51) {
|
||
return getNodChapter(num);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Sidebar navigation
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Build the full sidebar navigation list.
|
||
*
|
||
* Order: intro segments → chapters 1–51 → concluding words
|
||
*/
|
||
export async function getNodSidebarItems(): Promise<NodSidebarItem[]> {
|
||
const [meta, allDocs] = await Promise.all([
|
||
getNodMeta(),
|
||
prisma.proseChapter.findMany({
|
||
where: { book: BOOK, volume: NO_VOLUME },
|
||
orderBy: { order: "asc" },
|
||
select: { slug: true, title: true, type: true, chapter: true },
|
||
}),
|
||
]);
|
||
|
||
if (!meta) return [];
|
||
|
||
const items: NodSidebarItem[] = [];
|
||
|
||
// Intro segments (from DB, in order)
|
||
for (const doc of allDocs) {
|
||
if (doc.type === "intro-segment") {
|
||
items.push({
|
||
slug: doc.slug,
|
||
label: doc.title,
|
||
type: "intro-segment",
|
||
chapter: null,
|
||
});
|
||
}
|
||
}
|
||
|
||
// Chapters 1–51 (use meta.chapters for consistent title/ordering)
|
||
for (const ch of meta.chapters) {
|
||
items.push({
|
||
slug: String(ch.number),
|
||
label: `Chapter ${ch.number}: ${ch.title}`,
|
||
type: "chapter",
|
||
chapter: ch.number,
|
||
});
|
||
}
|
||
|
||
// Concluding words
|
||
const cw = allDocs.find((d) => d.type === "conclusion");
|
||
if (cw) {
|
||
items.push({
|
||
slug: cw.slug,
|
||
label: cw.title,
|
||
type: "conclusion",
|
||
chapter: null,
|
||
});
|
||
}
|
||
|
||
return items;
|
||
}
|