import type { CommunityRuleEntry } from "../../app/components/type/CommunityRule/CommunityRule.types"; import exportCopy from "../../messages/en/create/export.json"; import { communityInviteInitials } from "./ruleInviteCardMeta"; import { parsePublishedDocumentForCommunityRuleDisplay } from "./publishedDocumentToDisplaySections"; import { RULE_SECTION_CATEGORY } from "./ruleSectionsFromMethodSelections"; import { sharedTermsSection } from "./sharedTermsSection"; import { askOrganizerUrl } from "../sharePaths"; export type RuleExportSource = { id: string; title: string; summary?: string | null; document: Record; finishedAt?: string | Date | null; }; export type RuleExportLine = { lead?: string; body: string; }; export type RuleExportCard = { title: string; lines: RuleExportLine[]; }; export type RuleExportSection = { title: string; cards: RuleExportCard[]; }; export type RuleExportTerm = { term: string; meaning: string; }; export type RuleExportDocument = { title: string; summary: string; initials: string; photoUrl?: string; finishedOn?: string; sections: RuleExportSection[]; terms: RuleExportTerm[]; takeaway: string; takeawayUrl: string; credit: string; }; function leadIn(label: string): string { const trimmed = label.trim(); if (trimmed.length === 0) return ""; return /[.!?]$/.test(trimmed) ? trimmed : `${trimmed}.`; } function splitParagraphs(text: string): string[] { return text .trim() .split(/\n\s*\n/) .map((part) => part.trim()) .filter((part) => part.length > 0); } function valueLines(body: string): RuleExportLine[] { const parts = splitParagraphs(body); if (parts.length === 0) return []; if (parts.length === 1) { const only = parts[0]; return only ? [{ body: only }] : []; } const meaning = parts[0] ?? ""; const signals = parts.slice(1).join(" "); const lines: RuleExportLine[] = []; if (meaning.length > 0) lines.push({ body: meaning }); if (signals.length > 0) { lines.push({ lead: exportCopy.signalsLead, body: signals }); } return lines; } function entryLines( entry: CommunityRuleEntry, valuesSection: boolean, ): RuleExportLine[] { if (valuesSection) { return valueLines(entry.body ?? ""); } const lines: RuleExportLine[] = []; const intro = (entry.body ?? "").trim(); if (intro.length > 0) { for (const paragraph of splitParagraphs(intro)) { lines.push({ body: paragraph }); } } for (const block of entry.blocks ?? []) { const body = block.body.trim(); if (body.length === 0) continue; const lead = leadIn(block.label); if (lead.length > 0) { lines.push({ lead, body }); } else { lines.push({ body }); } } return lines; } function photoUrlFromDocument(document: Record): string | undefined { const raw = document.communityAvatarUrl; if (typeof raw !== "string") return undefined; const trimmed = raw.trim(); return trimmed.length > 0 ? trimmed : undefined; } function finishedOn(value: string | Date | null | undefined): string | undefined { if (value == null) return undefined; const date = typeof value === "string" ? new Date(value) : value; if (Number.isNaN(date.getTime())) return undefined; return date.toISOString().slice(0, 10); } export function buildRuleExportDocument( rule: RuleExportSource, origin: string, ): RuleExportDocument { const title = rule.title.trim(); const summary = typeof rule.summary === "string" ? rule.summary.trim() : ""; const display = parsePublishedDocumentForCommunityRuleDisplay(rule.document); const sharedTitle = sharedTermsSection().categoryName; const sections: RuleExportSection[] = []; for (const section of display) { if (section.categoryName === sharedTitle) continue; const valuesSection = section.categoryName === RULE_SECTION_CATEGORY.values; const cards: RuleExportCard[] = []; for (const entry of section.entries) { const lines = entryLines(entry, valuesSection); if (lines.length === 0 && entry.title.trim().length === 0) continue; cards.push({ title: entry.title.trim(), lines }); } if (cards.length === 0) continue; sections.push({ title: section.categoryName, cards }); } const terms: RuleExportTerm[] = sharedTermsSection().entries.map((entry) => ({ term: entry.title, meaning: (entry.body ?? "").trim(), })); return { title: title.length > 0 ? title : "Rule", summary, initials: communityInviteInitials(title.length > 0 ? title : "Rule"), photoUrl: photoUrlFromDocument(rule.document), finishedOn: finishedOn(rule.finishedAt), sections, terms, takeaway: exportCopy.takeaway, takeawayUrl: askOrganizerUrl(origin), credit: exportCopy.credit, }; } export function yamlQuote(value: string): string { if (value.length === 0) return '""'; if (/[:#\n"'\\]/.test(value) || value !== value.trim()) { return JSON.stringify(value); } return value; } export function exportPdfFilename(title: string): string { const cleaned = title .trim() .replace(/[\\/:*?"<>|]/g, "") .replace(/\s+/g, " "); const base = cleaned.length > 0 ? cleaned : "Rule"; return `${base} Rule.pdf`; } export const RULE_MD_FILENAME = exportCopy.ruleMdFilename;