feat: add ssr
This commit is contained in:
+336
@@ -0,0 +1,336 @@
|
||||
/**
|
||||
* Data access layer for The Science of Self-Realization (SSR).
|
||||
*
|
||||
* Reads from MongoDB via Prisma.
|
||||
* SSR is a large prose book with 6 intro segments (front-pages, dedication,
|
||||
* foreword, introduction, appreciations, about-the-author) and 8 parent
|
||||
* chapters, each containing multiple sub-chapters (e.g., "1-1", "1-2", "2-1").
|
||||
*
|
||||
* 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
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const BOOK = "ssr";
|
||||
|
||||
/** Volume sentinel for books without volumes. */
|
||||
const NO_VOLUME = 0;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type SsrMeta = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
year: number;
|
||||
description: string;
|
||||
totalChapters: number;
|
||||
chapters: SsrParentChapter[];
|
||||
};
|
||||
|
||||
export type SsrParentChapter = {
|
||||
number: number;
|
||||
title: string;
|
||||
subChapters: SsrSubChapterEntry[];
|
||||
};
|
||||
|
||||
export type SsrSubChapterEntry = {
|
||||
slug: string; // "1-1", "1-2", etc.
|
||||
title: string;
|
||||
};
|
||||
|
||||
export type SsrIntroSegment = {
|
||||
slug: string;
|
||||
title: string;
|
||||
type: string;
|
||||
book: string;
|
||||
order: number;
|
||||
paragraphs: string[];
|
||||
};
|
||||
|
||||
export type SsrChapter = {
|
||||
book: string;
|
||||
chapter: string; // "1-1", "2-1", etc.
|
||||
parentChapter: number;
|
||||
subChapter: number;
|
||||
title: string;
|
||||
parentTitle: string;
|
||||
type: string;
|
||||
paragraphs: string[];
|
||||
};
|
||||
|
||||
/** Union type used in sidebar / table-of-contents navigation. */
|
||||
export type SsrSidebarItem = {
|
||||
/** URL-safe slug: "intro-front-pages", "1-1", "parent-1", "intro-about-the-author" */
|
||||
slug: string;
|
||||
/** Human-readable label for the sidebar */
|
||||
label: string;
|
||||
/** "intro-segment" | "parent" | "sub-chapter" */
|
||||
type: string;
|
||||
/** Parent chapter number, or null for intro segments */
|
||||
parentChapter: number | null;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HTML sanitisation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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, chapter list with sub-chapters, etc.)
|
||||
*/
|
||||
export async function getSsrMeta(): Promise<SsrMeta | null> {
|
||||
const meta = await prisma.bookMeta.findUnique({ where: { book: BOOK } });
|
||||
if (!meta) return null;
|
||||
return meta.data as unknown as SsrMeta;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Intro segments
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* All intro segments, ordered by their `order` field.
|
||||
*/
|
||||
export async function getSsrIntroSegments(): Promise<SsrIntroSegment[]> {
|
||||
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-front-pages" (URL slug) and "front-pages" (raw slug).
|
||||
*/
|
||||
export async function getSsrIntroSegment(
|
||||
slug: string,
|
||||
): Promise<SsrIntroSegment | 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 (sub-chapters like "1-1", "2-3", etc.)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* A sub-chapter by its slug (e.g. "1-1", "2-3").
|
||||
*/
|
||||
export async function getSsrChapter(slug: string): Promise<SsrChapter | null> {
|
||||
const doc = await prisma.proseChapter.findUnique({
|
||||
where: {
|
||||
book_volume_slug: {
|
||||
book: BOOK,
|
||||
volume: NO_VOLUME,
|
||||
slug,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!doc) return null;
|
||||
|
||||
// Parse parentChapter and subChapter from slug "X-Y"
|
||||
const parts = slug.split("-");
|
||||
const parentChapter = parseInt(parts[0], 10);
|
||||
const subChapter = parseInt(parts[1], 10);
|
||||
|
||||
// Derive parentTitle from meta if available
|
||||
const meta = await getSsrMeta();
|
||||
const parentEntry = meta?.chapters.find((ch) => ch.number === parentChapter);
|
||||
const parentTitle = parentEntry?.title ?? "";
|
||||
|
||||
return {
|
||||
book: doc.book,
|
||||
chapter: slug,
|
||||
parentChapter,
|
||||
subChapter,
|
||||
title: doc.title,
|
||||
parentTitle,
|
||||
type: doc.type,
|
||||
paragraphs: normalizeParagraphs(doc.paragraphs),
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Unified content accessor
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get content by URL slug.
|
||||
*
|
||||
* "intro-front-pages" → intro segment
|
||||
* "intro-dedication" → intro segment
|
||||
* "intro-about-the-author" → intro segment
|
||||
* "1-1", "2-3", etc. → sub-chapter
|
||||
*/
|
||||
export async function getSsrContent(
|
||||
slug: string,
|
||||
): Promise<SsrChapter | SsrIntroSegment | null> {
|
||||
if (slug.startsWith("intro-")) {
|
||||
return getSsrIntroSegment(slug);
|
||||
}
|
||||
|
||||
// Sub-chapter slug like "1-1", "2-3"
|
||||
return getSsrChapter(slug);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sidebar navigation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Build the full sidebar navigation list with hierarchical structure.
|
||||
*
|
||||
* Order: front-pages → dedication → foreword → introduction → appreciations
|
||||
* → parent-1 (label) → 1-1, 1-2, ... → parent-2 (label) → 2-1, 2-2, ...
|
||||
* → about-the-author
|
||||
*
|
||||
* "parent" type items are non-clickable headings. Only "intro-segment" and
|
||||
* "sub-chapter" items are navigable pages.
|
||||
*/
|
||||
export async function getSsrSidebarItems(): Promise<SsrSidebarItem[]> {
|
||||
const [meta, allDocs] = await Promise.all([
|
||||
getSsrMeta(),
|
||||
prisma.proseChapter.findMany({
|
||||
where: { book: BOOK, volume: NO_VOLUME },
|
||||
orderBy: { order: "asc" },
|
||||
select: {
|
||||
slug: true,
|
||||
title: true,
|
||||
type: true,
|
||||
chapter: true,
|
||||
order: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!meta) return [];
|
||||
|
||||
const items: SsrSidebarItem[] = [];
|
||||
|
||||
// Intro segments that come BEFORE chapters (order < 99)
|
||||
for (const doc of allDocs) {
|
||||
if (doc.type === "intro-segment" && doc.order < 99) {
|
||||
items.push({
|
||||
slug: doc.slug,
|
||||
label: doc.title,
|
||||
type: "intro-segment",
|
||||
parentChapter: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Chapters — grouped by parent chapter with sub-chapters
|
||||
for (const ch of meta.chapters) {
|
||||
// Parent heading (non-clickable label)
|
||||
items.push({
|
||||
slug: `parent-${ch.number}`,
|
||||
label: `${ch.number}. ${ch.title}`,
|
||||
type: "parent",
|
||||
parentChapter: ch.number,
|
||||
});
|
||||
|
||||
// Sub-chapters under this parent
|
||||
for (const sub of ch.subChapters) {
|
||||
items.push({
|
||||
slug: sub.slug,
|
||||
label: sub.title,
|
||||
type: "sub-chapter",
|
||||
parentChapter: ch.number,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Intro segments that come AFTER chapters (order >= 99, e.g., about-the-author)
|
||||
for (const doc of allDocs) {
|
||||
if (doc.type === "intro-segment" && doc.order >= 99) {
|
||||
items.push({
|
||||
slug: doc.slug,
|
||||
label: doc.title,
|
||||
type: "intro-segment",
|
||||
parentChapter: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the flat list of navigable sidebar items (excludes "parent" headings).
|
||||
* Used for prev/next navigation.
|
||||
*/
|
||||
export function getNavigableItems(items: SsrSidebarItem[]): SsrSidebarItem[] {
|
||||
return items.filter((item) => item.type !== "parent");
|
||||
}
|
||||
Reference in New Issue
Block a user