增强报告详情页 Markdown 渲染(Task 12)

- 使用 markdown-it 实现专业的 Markdown 渲染
- 添加复制和下载报告功能
- 完善 Markdown 样式(标题、列表、代码块、表格等)
- 添加刷新按钮支持查看报告生成状态

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Opus 4.5 committed 2026-02-05 15:48:30 +08:00
1 parent 8edf7cbc9e
commit 457d69799d
3 files changed
+285 -21

No files matched your search

+89
View File
@@ -9,8 +9,10 @@
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"@element-plus/icons-vue": "^2.3.2", "@element-plus/icons-vue": "^2.3.2",
"@types/markdown-it": "^14.1.2",
"axios": "^1.13.4", "axios": "^1.13.4",
"element-plus": "^2.13.2", "element-plus": "^2.13.2",
"markdown-it": "^14.1.0",
"pinia": "^3.0.4", "pinia": "^3.0.4",
"vue": "^3.5.24", "vue": "^3.5.24",
"vue-router": "^4.6.4" "vue-router": "^4.6.4"
@@ -936,6 +938,12 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/linkify-it": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-5.0.0.tgz",
"integrity": "sha512-sVDA58zAw4eWAffKOaQH5/5j3XeayukzDk+ewSsnv3p4yJEZHCCzMDiZM8e0OUrRvmpGZ85jf4yDHkHsgBNr9Q==",
"license": "MIT"
},
"node_modules/@types/lodash": { "node_modules/@types/lodash": {
"version": "4.17.23", "version": "4.17.23",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.23.tgz", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.23.tgz",
@@ -952,6 +960,22 @@
"@types/lodash": "*" "@types/lodash": "*"
} }
}, },
"node_modules/@types/markdown-it": {
"version": "14.1.2",
"resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-14.1.2.tgz",
"integrity": "sha512-promo4eFwuiW+TfGxhi+0x3czqTYJkG8qB17ZUJiVF10Xm7NLVRSLUsfRTU/6h1e24VvRnXCx+hG7li58lkzog==",
"license": "MIT",
"dependencies": {
"@types/linkify-it": "^5",
"@types/mdurl": "^2"
}
},
"node_modules/@types/mdurl": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@types/mdurl/-/mdurl-2.0.0.tgz",
"integrity": "sha512-RGdgjQUZba5p6QEFAVx2OGb8rQDL/cPRG7GiedRzMcJ1tYnUANBncjbSB1NRGwbvjcPeikRABz2nshyPk1bhWg==",
"license": "MIT"
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "24.10.9", "version": "24.10.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.9.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.9.tgz",
@@ -1278,6 +1302,12 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
"license": "Python-2.0"
},
"node_modules/async-validator": { "node_modules/async-validator": {
"version": "4.2.5", "version": "4.2.5",
"resolved": "https://registry.npmjs.org/async-validator/-/async-validator-4.2.5.tgz", "resolved": "https://registry.npmjs.org/async-validator/-/async-validator-4.2.5.tgz",
@@ -1699,6 +1729,15 @@
"url": "https://github.com/sponsors/mesqueeb" "url": "https://github.com/sponsors/mesqueeb"
} }
}, },
"node_modules/linkify-it": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-5.0.0.tgz",
"integrity": "sha512-5aHCbzQRADcdP+ATqnDuhhJ/MRIqDkZX5pyjFHRRysS8vZ5AbqGEoFIb6pYHPZ+L/OC2Lc+xT8uHVVR5CAK/wQ==",
"license": "MIT",
"dependencies": {
"uc.micro": "^2.0.0"
}
},
"node_modules/lodash": { "node_modules/lodash": {
"version": "4.17.23", "version": "4.17.23",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz",
@@ -1733,6 +1772,35 @@
"@jridgewell/sourcemap-codec": "^1.5.5" "@jridgewell/sourcemap-codec": "^1.5.5"
} }
}, },
"node_modules/markdown-it": {
"version": "14.1.0",
"resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-14.1.0.tgz",
"integrity": "sha512-a54IwgWPaeBCAAsv13YgmALOF1elABB08FxO9i+r4VFk5Vl4pKokRPeX8u5TCgSsPi6ec1otfLjdOpVcgbpshg==",
"license": "MIT",
"dependencies": {
"argparse": "^2.0.1",
"entities": "^4.4.0",
"linkify-it": "^5.0.0",
"mdurl": "^2.0.0",
"punycode.js": "^2.3.1",
"uc.micro": "^2.1.0"
},
"bin": {
"markdown-it": "bin/markdown-it.mjs"
}
},
"node_modules/markdown-it/node_modules/entities": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz",
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=0.12"
},
"funding": {
"url": "https://github.com/fb55/entities?sponsor=1"
}
},
"node_modules/math-intrinsics": { "node_modules/math-intrinsics": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
@@ -1742,6 +1810,12 @@
"node": ">= 0.4" "node": ">= 0.4"
} }
}, },
"node_modules/mdurl": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/mdurl/-/mdurl-2.0.0.tgz",
"integrity": "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w==",
"license": "MIT"
},
"node_modules/memoize-one": { "node_modules/memoize-one": {
"version": "6.0.0", "version": "6.0.0",
"resolved": "https://registry.npmjs.org/memoize-one/-/memoize-one-6.0.0.tgz", "resolved": "https://registry.npmjs.org/memoize-one/-/memoize-one-6.0.0.tgz",
@@ -1894,6 +1968,15 @@
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/punycode.js": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/punycode.js/-/punycode.js-2.3.1.tgz",
"integrity": "sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA==",
"license": "MIT",
"engines": {
"node": ">=6"
}
},
"node_modules/rfdc": { "node_modules/rfdc": {
"version": "1.4.1", "version": "1.4.1",
"resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz", "resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz",
@@ -2007,6 +2090,12 @@
"node": ">=14.17" "node": ">=14.17"
} }
}, },
"node_modules/uc.micro": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-2.1.0.tgz",
"integrity": "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A==",
"license": "MIT"
},
"node_modules/undici-types": { "node_modules/undici-types": {
"version": "7.16.0", "version": "7.16.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz",
+2
View File
@@ -10,8 +10,10 @@
}, },
"dependencies": { "dependencies": {
"@element-plus/icons-vue": "^2.3.2", "@element-plus/icons-vue": "^2.3.2",
"@types/markdown-it": "^14.1.2",
"axios": "^1.13.4", "axios": "^1.13.4",
"element-plus": "^2.13.2", "element-plus": "^2.13.2",
"markdown-it": "^14.1.0",
"pinia": "^3.0.4", "pinia": "^3.0.4",
"vue": "^3.5.24", "vue": "^3.5.24",
"vue-router": "^4.6.4" "vue-router": "^4.6.4"
+194 -21
View File
@@ -6,7 +6,10 @@
返回 返回
</el-button> </el-button>
<h2>{{ report?.title || '报告详情' }}</h2> <h2>{{ report?.title || '报告详情' }}</h2>
<div></div> <el-button type="primary" @click="handleRefresh" :loading="loading">
<el-icon><Refresh /></el-icon>
刷新
</el-button>
</div> </div>
<el-card v-if="report"> <el-card v-if="report">
@@ -26,14 +29,28 @@
<el-divider /> <el-divider />
<div class="report-content"> <div class="report-content">
<h3>报告内容</h3> <div class="content-header">
<h3>报告内容</h3>
<el-button-group v-if="report.status === 'completed'">
<el-button size="small" @click="copyContent">
<el-icon><CopyDocument /></el-icon>
复制
</el-button>
<el-button size="small" @click="downloadContent">
<el-icon><Download /></el-icon>
下载
</el-button>
</el-button-group>
</div>
<div v-if="report.status === 'failed'" class="error-message"> <div v-if="report.status === 'failed'" class="error-message">
<el-alert :title="report.errorMessage || '生成失败'" type="error" show-icon :closable="false" /> <el-alert :title="report.errorMessage || '生成失败'" type="error" show-icon :closable="false" />
</div> </div>
<div v-else-if="report.status === 'pending' || report.status === 'generating'" class="pending-message"> <div v-else-if="report.status === 'pending' || report.status === 'generating'" class="pending-message">
<el-empty description="报告正在生成中,请稍后刷新查看" /> <el-empty description="报告正在生成中,请稍后刷新查看">
<el-button type="primary" @click="handleRefresh">刷新状态</el-button>
</el-empty>
</div> </div>
<div v-else class="content-body" v-html="renderedContent"></div> <div v-else class="content-body markdown-body" v-html="renderedContent"></div>
</div> </div>
</el-card> </el-card>
@@ -44,24 +61,27 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted } from 'vue' import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
import { ArrowLeft } from '@element-plus/icons-vue' import { ElMessage } from 'element-plus'
import { ArrowLeft, Refresh, CopyDocument, Download } from '@element-plus/icons-vue'
import MarkdownIt from 'markdown-it'
import request from '@/api/request' import request from '@/api/request'
import type { Report } from '@/api/types' import type { Report } from '@/api/types'
// 初始化 markdown-it
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true,
breaks: true,
})
const route = useRoute() const route = useRoute()
const loading = ref(false) const loading = ref(false)
const report = ref<Report | null>(null) const report = ref<Report | null>(null)
const renderedContent = computed(() => { const renderedContent = computed(() => {
if (!report.value?.content) return '' if (!report.value?.content) return ''
// 简单的 Markdown 渲染(实际项目中可以使用 markdown-it) return md.render(report.value.content)
return report.value.content
.replace(/^### (.*$)/gim, '<h3>$1</h3>')
.replace(/^## (.*$)/gim, '<h2>$1</h2>')
.replace(/^# (.*$)/gim, '<h1>$1</h1>')
.replace(/\*\*(.*)\*\*/gim, '<strong>$1</strong>')
.replace(/\*(.*)\*/gim, '<em>$1</em>')
.replace(/\n/gim, '<br>')
}) })
const loadReport = async () => { const loadReport = async () => {
@@ -79,6 +99,34 @@ const loadReport = async () => {
} }
} }
const handleRefresh = () => {
loadReport()
}
const copyContent = async () => {
if (!report.value?.content) return
try {
await navigator.clipboard.writeText(report.value.content)
ElMessage.success('已复制到剪贴板')
} catch (error) {
ElMessage.error('复制失败')
}
}
const downloadContent = () => {
if (!report.value?.content) return
const blob = new Blob([report.value.content], { type: 'text/markdown' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `${report.value.title || '报告'}.md`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
ElMessage.success('下载成功')
}
const getStatusType = (status: string) => { const getStatusType = (status: string) => {
const map: Record<string, 'info' | 'warning' | 'success' | 'danger'> = { const map: Record<string, 'info' | 'warning' | 'success' | 'danger'> = {
pending: 'info', pending: 'info',
@@ -136,24 +184,149 @@ onMounted(() => {
margin-bottom: 20px; margin-bottom: 20px;
} }
.report-content h3 { .report-content .content-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px; margin-bottom: 15px;
}
.report-content .content-header h3 {
margin: 0;
color: #303133; color: #303133;
} }
.content-body { .content-body {
padding: 20px; padding: 24px;
background-color: #fafafa; background-color: #fff;
border: 1px solid #ebeef5;
border-radius: 4px; border-radius: 4px;
line-height: 1.8; line-height: 1.8;
color: #303133;
}
/* Markdown 样式 */
.markdown-body :deep(h1) {
font-size: 24px;
font-weight: 600;
margin: 24px 0 16px;
padding-bottom: 8px;
border-bottom: 1px solid #ebeef5;
}
.markdown-body :deep(h2) {
font-size: 20px;
font-weight: 600;
margin: 20px 0 12px;
padding-bottom: 6px;
border-bottom: 1px solid #ebeef5;
}
.markdown-body :deep(h3) {
font-size: 18px;
font-weight: 600;
margin: 16px 0 10px;
}
.markdown-body :deep(h4) {
font-size: 16px;
font-weight: 600;
margin: 14px 0 8px;
}
.markdown-body :deep(p) {
margin: 12px 0;
}
.markdown-body :deep(ul),
.markdown-body :deep(ol) {
padding-left: 24px;
margin: 12px 0;
}
.markdown-body :deep(li) {
margin: 6px 0;
}
.markdown-body :deep(blockquote) {
margin: 16px 0;
padding: 12px 20px;
background-color: #f5f7fa;
border-left: 4px solid #409eff;
color: #606266; color: #606266;
} }
.content-body :deep(h1), .markdown-body :deep(code) {
.content-body :deep(h2), padding: 2px 6px;
.content-body :deep(h3) { background-color: #f5f7fa;
margin: 20px 0 10px; border-radius: 3px;
color: #303133; font-family: 'Consolas', 'Monaco', monospace;
font-size: 14px;
color: #e6a23c;
}
.markdown-body :deep(pre) {
margin: 16px 0;
padding: 16px;
background-color: #282c34;
border-radius: 4px;
overflow-x: auto;
}
.markdown-body :deep(pre code) {
padding: 0;
background-color: transparent;
color: #abb2bf;
}
.markdown-body :deep(table) {
width: 100%;
margin: 16px 0;
border-collapse: collapse;
}
.markdown-body :deep(th),
.markdown-body :deep(td) {
padding: 10px 12px;
border: 1px solid #ebeef5;
text-align: left;
}
.markdown-body :deep(th) {
background-color: #f5f7fa;
font-weight: 600;
}
.markdown-body :deep(tr:nth-child(even)) {
background-color: #fafafa;
}
.markdown-body :deep(a) {
color: #409eff;
text-decoration: none;
}
.markdown-body :deep(a:hover) {
text-decoration: underline;
}
.markdown-body :deep(hr) {
margin: 24px 0;
border: none;
border-top: 1px solid #ebeef5;
}
.markdown-body :deep(img) {
max-width: 100%;
border-radius: 4px;
}
.markdown-body :deep(strong) {
font-weight: 600;
}
.markdown-body :deep(em) {
font-style: italic;
} }
.error-message, .error-message,