import exportCopy from "../../messages/en/create/export.json"; import type { StoredLastPublishedRule } from "./lastPublishedRule"; import { RULE_MD_FILENAME, buildRuleExportDocument, exportPdfFilename, yamlQuote, type RuleExportDocument, type RuleExportLine, type RuleExportSource, } from "./ruleExportModel"; import { ruleExportDocumentToPdfBytes } from "./ruleExportPdf"; export { buildPublicRuleUrl, buildShareRuleUrl, formatShareUrlDisplay, shareCopyUrl, } from "../sharePaths"; export { RULE_MD_FILENAME, exportPdfFilename, type RuleExportSource, } from "./ruleExportModel"; export type RuleExportFormat = "pdf" | "markdown" | "print"; function lineMarkdown(line: RuleExportLine): string { if (line.lead) return `**${line.lead}** ${line.body}`; return line.body; } export function ruleExportDocumentToMarkdown(doc: RuleExportDocument): string { const front: string[] = ["---", `title: ${yamlQuote(doc.title)}`]; if (doc.summary.length > 0) { front.push(`summary: ${yamlQuote(doc.summary)}`); } if (doc.finishedOn) { front.push(`finished: ${doc.finishedOn}`); } front.push("---", "", `# ${doc.title}`, ""); if (doc.summary.length > 0) { front.push(doc.summary, ""); } for (const section of doc.sections) { front.push(`## ${section.title}`, ""); for (const card of section.cards) { front.push(`### ${card.title}`, ""); for (const line of card.lines) { front.push(lineMarkdown(line), ""); } } } if (doc.terms.length > 0) { front.push(`## ${exportCopy.sharedTermsHeading}`, ""); for (const term of doc.terms) { front.push(`**${term.term}**`, `: ${term.meaning}`, ""); } } front.push(`${doc.takeaway}: ${doc.takeawayUrl}`, ""); return `${front.join("\n").trimEnd()}\n`; } function escapeHtml(value: string): string { return value .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } function lineHtml(line: RuleExportLine): string { if (line.lead) { return `
${escapeHtml(line.lead)} ${escapeHtml(line.body)}
`; } return `${escapeHtml(line.body)}
`; } export function ruleExportDocumentToPrintHtml(doc: RuleExportDocument): string { const photo = doc.photoUrl ? `${escapeHtml(doc.summary)}
` : ""; const sections = doc.sections .map((section) => { const cards = section.cards .map( (card) => `${escapeHtml(term.term)} ${escapeHtml(term.meaning)}
`, ) .join(""); const termsBlock = doc.terms.length > 0 ? `${escapeHtml(doc.takeaway)} ${escapeHtml(doc.takeawayUrl)}
`; } export function exportStoredRuleAsMarkdown( rule: RuleExportSource, origin: string, ): string { return ruleExportDocumentToMarkdown(buildRuleExportDocument(rule, origin)); } export function buildPrintableRuleHtmlDocument( rule: RuleExportSource, origin: string, ): string { return ruleExportDocumentToPrintHtml(buildRuleExportDocument(rule, origin)); } export function buildStoredRulePdfBytes( rule: RuleExportSource, origin: string, ): Uint8Array { return ruleExportDocumentToPdfBytes(buildRuleExportDocument(rule, origin)); } export function buildStoredRulePdfBlob( rule: RuleExportSource, origin: string, ): Blob { const bytes = buildStoredRulePdfBytes(rule, origin); const copy = new Uint8Array(bytes.byteLength); copy.set(bytes); return new Blob([copy.buffer], { type: "application/pdf" }); } /** Safe filename fragment from rule title, with stable fallback. */ export function exportFilenameBase(rule: StoredLastPublishedRule): string { const fromTitle = rule.title .trim() .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 48); return fromTitle.length > 0 ? fromTitle : `rule-${rule.id.slice(0, 8)}`; } export function downloadBlob(filename: string, blob: Blob): void { if (typeof document === "undefined") return; const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; a.rel = "noopener"; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); } export function downloadTextFile( filename: string, contents: string, mime: string, ): void { downloadBlob(filename, new Blob([contents], { type: mime })); } export function downloadStoredRuleAsPdf( rule: RuleExportSource, origin: string, ): void { downloadBlob(exportPdfFilename(rule.title), buildStoredRulePdfBlob(rule, origin)); } export function downloadStoredRuleAsMarkdown( rule: RuleExportSource, origin: string, ): void { downloadTextFile( RULE_MD_FILENAME, exportStoredRuleAsMarkdown(rule, origin), "text/markdown;charset=utf-8", ); } export function printStoredRule( rule: RuleExportSource, origin: string, ): boolean { if (typeof window === "undefined") return false; const html = buildPrintableRuleHtmlDocument(rule, origin); const popup = window.open("", "_blank"); if (!popup) return false; popup.document.open(); popup.document.write(html); popup.document.close(); popup.focus(); popup.print(); return true; }