/** * 导出文本为 Markdown / PDF */ export function downloadMarkdown(content: string, title = '导出内容') { const now = new Date().toISOString().slice(0, 19).replace(/[:T]/g, '-') const markdown = `# ${title}\n\n> 导出时间: ${new Date().toLocaleString('zh-CN')}\n\n${content}\n` const blob = new Blob([markdown], { type: 'text/markdown; charset=utf-8' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `${title.replace(/\s+/g, '_')}_${now}.md` document.body.appendChild(a) a.click() document.body.removeChild(a) URL.revokeObjectURL(url) } export function printAsPdf(content: string, title = '导出内容') { const w = window.open('', '_blank', 'width=800,height=600') if (!w) return w.document.write(`
导出时间: ${new Date().toLocaleString('zh-CN')}
${escapeHtml(content)}
`)
w.document.close()
setTimeout(() => w.print(), 300)
}
function escapeHtml(text: string) {
return text.replace(/&/g, '&').replace(//g, '>')
}