feat: export report as Markdown

This commit is contained in:
AnotiaWang
2025-02-14 17:25:04 +08:00
parent 8c36e5728a
commit 36ebbf3fbe
3 changed files with 58 additions and 13 deletions

View File

@ -16,13 +16,18 @@
const error = ref('')
const loading = ref(false)
const loadingExportPdf = ref(false)
const loadingExportMarkdown = ref(false)
const reasoningContent = ref('')
const reportContent = ref('')
const reportHtml = computed(() =>
marked(reportContent.value, { silent: true, gfm: true, breaks: true }),
)
const isExportButtonDisabled = computed(
() => !reportContent.value || loading.value || loadingExportPdf.value,
() =>
!reportContent.value ||
loading.value ||
loadingExportPdf.value ||
loadingExportMarkdown.value,
)
let pdf: jsPDF | undefined
@ -81,6 +86,8 @@
duration: 5000,
color: 'info',
})
// Wait for 100ms to avoid toast being blocked by PDF generation
await new Promise((resolve) => setTimeout(resolve, 100))
const fontUrl = '/fonts/SourceHanSansCN-VF.ttf'
pdf.addFont(fontUrl, 'SourceHanSans', 'normal')
pdf.setFont('SourceHanSans')
@ -146,27 +153,63 @@
}
}
async function exportToMarkdown() {
if (!reportContent.value) return
loadingExportMarkdown.value = true
try {
const blob = new Blob([reportContent.value], { type: 'text/markdown' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'research-report.md'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
} catch (error) {
console.error('Export to Markdown failed:', error)
} finally {
loadingExportMarkdown.value = false
}
}
defineExpose({
generateReport,
exportToPdf,
exportToMarkdown,
})
</script>
<template>
<UCard>
<template #header>
<div class="flex items-center justify-between">
<div
class="flex flex-col sm:flex-row sm:items-center justify-between gap-2"
>
<h2 class="font-bold">{{ $t('researchReport.title') }}</h2>
<UButton
color="info"
variant="ghost"
icon="i-lucide-download"
:disabled="isExportButtonDisabled"
:loading="loadingExportPdf"
@click="exportToPdf"
>
{{ $t('researchReport.exportPdf') }}
</UButton>
<div class="flex gap-2">
<UButton
color="info"
variant="ghost"
icon="i-lucide-download"
:disabled="isExportButtonDisabled"
:loading="loadingExportMarkdown"
@click="exportToMarkdown"
>
{{ $t('researchReport.exportMarkdown') }}
</UButton>
<UButton
color="info"
variant="ghost"
icon="i-lucide-download"
:disabled="isExportButtonDisabled"
:loading="loadingExportPdf"
@click="exportToPdf"
>
{{ $t('researchReport.exportPdf') }}
</UButton>
</div>
</div>
</template>