Intelligence
Artifacts
Browse the repository, read documents, and manage the governance folders. Source, runtime, and infrastructure are read-only.
Repository
repositories/aaf-holdings/hq01/lib/content/reports.ts
1.9 KB
import path from "node:path";
import { cache } from "react";
import { CONTENT_ROOT } from "./config";
import { parseDoc, pick, readText, relPath, walkFiles } from "./fs";
import { excerpt } from "../utils";
import type { Report } from "./types";
function isReportFile(p: string): boolean {
const norm = p.split(path.sep).join("/");
if (/\/templates?\//.test(norm)) return false;
return /\/reports\/.+\.md$/.test(norm);
}
function reportId(filePath: string): string {
const base = path.basename(filePath, ".md");
if (base.toLowerCase() === "report") {
return path.basename(path.dirname(filePath));
}
return base;
}
function firstParagraph(body: string): string {
const withoutHeading = body.replace(/^#.*$/m, "").trim();
const para = withoutHeading.split(/\n\s*\n/).find((b) => b.trim().length > 0) ?? "";
return excerpt(para.replace(/[#>*_`-]/g, " "), 180);
}
function toReport(filePath: string): Report {
const doc = parseDoc(readText(filePath));
const id = reportId(filePath);
return {
id,
title: doc.title || id,
date: pick(doc.meta, "date", "created", "completed") ?? null,
author: pick(doc.meta, "author", "owner", "worker") ?? null,
summary: pick(doc.blocks, "summary") || firstParagraph(doc.body),
body: doc.body,
sourcePath: relPath(filePath),
};
}
export const getReports = cache((): Report[] => {
const files = walkFiles(CONTENT_ROOT, isReportFile);
const byId = new Map<string, Report>();
for (const f of files) {
const r = toReport(f);
if (!byId.has(r.id)) byId.set(r.id, r);
}
// Most recent first when dated; otherwise reverse-id (newest ids last).
return [...byId.values()].sort((a, b) => {
if (a.date && b.date) return b.date.localeCompare(a.date);
return b.id.localeCompare(a.id);
});
});
export const getReport = cache((id: string): Report | undefined => {
return getReports().find((r) => r.id === id);
});
root · /srv/aaf