import { n as sanitizeHref, r as sanitizeImageSrc } from "./sanitize-O_3j1mNJ.js";

//#region src/renderers/markdown.ts
function resolveColumn(col) {
	return typeof col === "string" ? { header: col } : col;
}
function renderChild(child) {
	if (typeof child === "string") return child;
	return renderNode(child);
}
function renderChildren(children) {
	return children.map(renderChild).join("");
}
function renderInline(children) {
	return children.map(renderChild).join("");
}
function renderNode(node) {
	const p = node.props;
	switch (node.type) {
		case "document": return renderChildren(node.children);
		case "page": return renderChildren(node.children);
		case "section": return `${renderChildren(node.children)}\n`;
		case "row":
		case "column": return renderChildren(node.children);
		case "heading": {
			const level = p.level ?? 1;
			return `${"#".repeat(Math.min(Math.max(level, 1), 6))} ${renderInline(node.children)}\n\n`;
		}
		case "text": {
			let text = renderInline(node.children);
			if (p.bold) text = `**${text}**`;
			if (p.italic) text = `*${text}*`;
			if (p.strikethrough) text = `~~${text}~~`;
			return `${text}\n\n`;
		}
		case "link": return `[${renderInline(node.children)}](${sanitizeHref(p.href)})`;
		case "image": {
			let md = `![${p.alt ?? ""}](${sanitizeImageSrc(p.src)})`;
			if (p.caption) md += `\n*${p.caption}*`;
			return `${md}\n\n`;
		}
		case "table": {
			const columns = (p.columns ?? []).map(resolveColumn);
			const rows = p.rows ?? [];
			if (columns.length === 0) return "";
			let md = `${`| ${columns.map((c) => c.header).join(" | ")} |`}\n${`| ${columns.map((c) => {
				const align = c.align ?? "left";
				if (align === "center") return ":---:";
				if (align === "right") return "---:";
				return "---";
			}).join(" | ")} |`}\n${rows.map((row) => `| ${row.map((cell) => String(cell ?? "")).join(" | ")} |`).join("\n")}\n\n`;
			if (p.caption) md = `*${p.caption}*\n\n${md}`;
			return md;
		}
		case "list": {
			const ordered = p.ordered;
			return `${node.children.filter((c) => typeof c !== "string").map((item, i) => {
				return `${ordered ? `${i + 1}.` : "-"} ${renderInline(item.children)}`;
			}).join("\n")}\n\n`;
		}
		case "list-item": return renderInline(node.children);
		case "code": return `\`\`\`${p.language ?? ""}\n${renderInline(node.children)}\n\`\`\`\n\n`;
		case "divider": return "---\n\n";
		case "page-break": return "---\n\n";
		case "spacer": return "\n";
		case "button": return `[${renderInline(node.children)}](${sanitizeHref(p.href)})\n\n`;
		case "quote": return `> ${renderInline(node.children)}\n\n`;
		default: return renderChildren(node.children);
	}
}
const markdownRenderer = { async render(node, _options) {
	return `${renderNode(node).trim()}\n`;
} };

//#endregion
export { markdownRenderer };
//# sourceMappingURL=markdown-CdtlFGC0.js.map