import { markdownToHtml } from "./markdown"; import { escapeHtml, sanitizeTemplateData } from "./security"; import { templates } from "./templates"; import type { GenerateOptions } from "./types"; export function generateHtml(options: GenerateOptions): string { let bodyContent = ""; if (options.template && options.data) { const templateFn = templates[options.template]; if (templateFn) { const sanitizedData = sanitizeTemplateData(options.data); return templateFn(sanitizedData); } } if (options.content) { switch (options.contentType) { case "markdown": bodyContent = markdownToHtml(options.content); break; case "html": bodyContent = options.content; break; case "text": bodyContent = `
${escapeHtml(options.content)}
`; break; } } return ` ${escapeHtml(options.title) || "Document"} ${bodyContent} `; } export function htmlToPlainText(html: string): string { return html .replace(//gi, "") .replace(//gi, "") .replace(/<\/(h1|h2|h3|p|div|li|tr)>/gi, "\n") .replace(//gi, "\n") .replace(/<[^>]+>/g, "") .replace(/ /g, " ") .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, '"') .replace(/'/g, "'") .split("\n") .map((line) => line.trim()) .filter(Boolean) .join("\n"); }