Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
239 lines
7.9 KiB
TypeScript
239 lines
7.9 KiB
TypeScript
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, ">")
|
|
.replace(/"/g, """);
|
|
}
|
|
|
|
function lineHtml(line: RuleExportLine): string {
|
|
if (line.lead) {
|
|
return `<p><span class="lead">${escapeHtml(line.lead)}</span> ${escapeHtml(line.body)}</p>`;
|
|
}
|
|
return `<p>${escapeHtml(line.body)}</p>`;
|
|
}
|
|
|
|
export function ruleExportDocumentToPrintHtml(doc: RuleExportDocument): string {
|
|
const photo = doc.photoUrl
|
|
? `<img class="photo" src="${escapeHtml(doc.photoUrl)}" alt="" />`
|
|
: `<div class="photo initials">${escapeHtml(doc.initials)}</div>`;
|
|
const summary =
|
|
doc.summary.length > 0
|
|
? `<p class="summary">${escapeHtml(doc.summary)}</p>`
|
|
: "";
|
|
const sections = doc.sections
|
|
.map((section) => {
|
|
const cards = section.cards
|
|
.map(
|
|
(card) =>
|
|
`<article class="card"><h3>${escapeHtml(card.title)}</h3>${card.lines
|
|
.map(lineHtml)
|
|
.join("")}</article>`,
|
|
)
|
|
.join("");
|
|
return `<section><h2>${escapeHtml(section.title)}</h2>${cards}</section>`;
|
|
})
|
|
.join("");
|
|
const terms = doc.terms
|
|
.map(
|
|
(term) =>
|
|
`<p class="term"><i>${escapeHtml(term.term)}</i> ${escapeHtml(term.meaning)}</p>`,
|
|
)
|
|
.join("");
|
|
const termsBlock =
|
|
doc.terms.length > 0
|
|
? `<section class="terms"><h2>${escapeHtml(exportCopy.sharedTermsHeading)}</h2><div class="term-columns">${terms}</div></section>`
|
|
: "";
|
|
const styles = `
|
|
@page { size: letter; margin: 12.7mm 16.9mm 12mm; }
|
|
html, body { margin: 0; padding: 0; background: #fff; color: #141414; }
|
|
body { font-family: Inter, system-ui, sans-serif; font-size: 12pt; line-height: 1.35; }
|
|
h1, h2, h3 { font-family: "Bricolage Grotesque", Inter, system-ui, sans-serif; font-weight: 700; margin: 0; }
|
|
.header { display: flex; gap: 24px; align-items: center; padding-bottom: 12px; }
|
|
.photo { width: 72px; height: 72px; border-radius: 9999px; object-fit: cover; flex-shrink: 0; }
|
|
.photo.initials { border: 2px solid #141414; display: flex; align-items: center; justify-content: center; font-size: 24px; font-weight: 500; }
|
|
.group { min-width: 0; flex: 1; }
|
|
h1 { font-size: 32px; line-height: 40px; }
|
|
.summary { font-size: 16px; line-height: 24px; margin: 8px 0 0; font-weight: 400; }
|
|
section { border-top: 1px solid #141414; padding-top: 16px; margin-top: 20px; }
|
|
h2 { font-size: 24px; line-height: 32px; margin-bottom: 12px; }
|
|
.card { margin: 0 0 12px; }
|
|
h3 { font-size: 16px; line-height: 22px; margin-bottom: 8px; }
|
|
p { margin: 0 0 8px; font-size: 14px; line-height: 20px; }
|
|
.lead { font-weight: 500; }
|
|
.terms h2 { margin-bottom: 16px; }
|
|
.term-columns { column-count: 2; column-gap: 32px; }
|
|
.term { font-size: 12px; line-height: 16px; break-inside: avoid; margin: 0 0 12px; }
|
|
.term i { font-style: italic; }
|
|
.takeaway { margin-top: 24px; font-size: 12px; }
|
|
.takeaway a { color: #141414; }
|
|
.footer { display: flex; justify-content: space-between; color: #464646; font-size: 12px; margin-top: 24px; }
|
|
@media print {
|
|
.footer { position: running(pageFooter); }
|
|
@page { @bottom-center { content: none; } }
|
|
}
|
|
`.replace(/\n/g, "");
|
|
|
|
return `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>${escapeHtml(doc.title)}</title><style>${styles}</style></head><body><header class="header">${photo}<div class="group"><h1>${escapeHtml(doc.title)}</h1>${summary}</div></header>${sections}${termsBlock}<p class="takeaway">${escapeHtml(doc.takeaway)} <a href="${escapeHtml(doc.takeawayUrl)}">${escapeHtml(doc.takeawayUrl)}</a></p><div class="footer"><span>${escapeHtml(doc.credit)}</span></div></body></html>`;
|
|
}
|
|
|
|
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;
|
|
}
|