import { readFileSync } from "node:fs"; import { resolve } from "node:path"; import matter from "gray-matter"; import { z } from "zod"; const CAMERON_DID = process.env.CAMERON_DID || "did:plc:gfrmhdmjvxn2sjedzboeudef"; const CONTENT_ROOT = resolve(process.env.PUBLIC_CONTENT_ROOT || resolve(process.cwd(), "content")); const ABOUT_PATH = resolve(CONTENT_ROOT, "about.md"); const aboutFrontmatterSchema = z.object({ title: z.literal("About"), slug: z.literal("about"), updatedAt: z.string().datetime({ offset: true }).optional(), atproto: z.object({ collection: z.literal("stream.cameron.about"), rkey: z.literal("self"), }), }); export type AboutFrontmatter = z.infer; export interface CanonicalAboutSource { file: string; source: string; body: string; frontmatter: AboutFrontmatter; } export interface AboutDocument { uri: string; body: string; updatedAt?: string; } let aboutCache: AboutDocument | undefined; const cacheContent = process.env.NODE_ENV === "production"; export function loadCanonicalAboutSource(): CanonicalAboutSource { const raw = readFileSync(ABOUT_PATH, "utf8"); const source = matter(raw); const parsed = aboutFrontmatterSchema.safeParse(source.data); if (!parsed.success) { throw new Error(`about.md: invalid public About frontmatter: ${parsed.error.message}`); } return { file: "about.md", source: raw, body: source.content, frontmatter: parsed.data, }; } export async function getAboutDocument(): Promise { if (cacheContent && aboutCache) return aboutCache; const source = loadCanonicalAboutSource(); const front = source.frontmatter; const document = { uri: `at://${CAMERON_DID}/${front.atproto.collection}/${front.atproto.rkey}`, body: source.body.trim(), updatedAt: front.updatedAt, }; if (cacheContent) aboutCache = document; return document; } export async function getAbout(): Promise { return (await getAboutDocument()).body; }