/** * lib/content.js * * Filesystem-first content tree builder. * * Provides a clean API for navigating content as folders and files. * Data objects (ContentFile, ContentFolder) are plain data with * pre-computed derived fields. render() is a separate action. * * Built on import.meta.glob for md/mdx (keys for path enumeration at module * init, lazy imports for content) and direct fs reads for yaml/json data files. */ import { readFileSync } from 'node:fs'; import { join, dirname, resolve } from 'node:path'; import yaml from 'js-yaml'; import integrity from '../data/integrity.json'; /** * @typedef {Object} Breadcrumb * @property {string} label * @property {string} [href] */ /** * @typedef {Object} ContentFile * @property {string} path * @property {string} folder * @property {string} slug * @property {string} url * @property {Record} frontmatter * @property {Breadcrumb[]} breadcrumbs * @property {ContentFile} [prev] * @property {ContentFile} [next] */ /** * @typedef {Object} ContentFolder * @property {string} path * @property {string} folder * @property {string} slug * @property {string} url * @property {Record} frontmatter * @property {Breadcrumb[]} breadcrumbs * @property {ContentFile[]} files * @property {ContentFolder[]} folders * @property {ContentFile[]} allFiles */ // Case-preserving filesystem paths (strings only, no modules loaded, no circular dependency) const allPaths = Object.keys(import.meta.glob('/src/content/**/*.{md,mdx}')); // Lazy module loaders, called inside async functions, not at module init /** @type {Record Promise>} */ const lazyModules = import.meta.glob('/src/content/**/*.{md,mdx}'); // Raw authored sources, eagerly loaded. Used by fileAsMarkdown to serve the // `.md` companion endpoint for every prose page. /** @type {Record} */ const rawSources = import.meta.glob('/src/content/**/*.{md,mdx}', { query: '?raw', import: 'default', eager: true, }); // Cache for loaded modules to avoid re-importing const moduleCache = new Map(); async function loadModule(fullPath) { if (moduleCache.has(fullPath)) return moduleCache.get(fullPath); const loader = lazyModules[fullPath]; if (!loader) return null; const mod = await loader(); moduleCache.set(fullPath, mod); return mod; } /** * Convention: a file with slug `full` in a collection is the aggregate view * of that collection. It's excluded from sidebar lists and prev/next nav, * and its body is synthesised at content-load time by * aggregateCollectionMarkdown(). * @param {{ slug: string }} file */ export function isAggregate(file) { return file.slug === 'full'; } export function readRaw(fullPath) { try { return readFileSync(join(process.cwd(), fullPath.replace(/^\//, '')), 'utf-8'); } catch { return ''; } } function rawBody(fullPath) { return readRaw(fullPath) .replace(/^---[\s\S]*?---\n*/, '') .replace(/