aegon-lv82 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -0
- package/src/App.vue +44 -0
- package/src/api/index.ts +19 -0
- package/src/api/modules/build-report/reportEditor.ts +526 -0
- package/src/api/modules/build-report/reportPermission.ts +64 -0
- package/src/api/modules/index.ts +69 -0
- package/src/api/modules/industry/green_mortgage.ts +42 -0
- package/src/api/modules/user/index.ts +4 -0
- package/src/api/request.ts +124 -0
- package/src/assets/sample-access-icon.png +0 -0
- package/src/assets/sample-pie-chart.png +0 -0
- package/src/assets/vue.svg +1 -0
- package/src/components/CapsuleScrollbar.vue +7 -0
- package/src/components/Export/ExcelExport.vue +592 -0
- package/src/components/Export/ExcelExport2.vue +494 -0
- package/src/components/Export/ExcelExport3.vue +342 -0
- package/src/components/Export/ExcelExport4.vue +665 -0
- package/src/components/Export/excelExport.js +547 -0
- package/src/components/Export/excelExport.ts +551 -0
- package/src/components/GEN-AI/index.vue +140 -0
- package/src/components/GEN-AI/index1.vue +422 -0
- package/src/components/GEN-AI/index10.vue +5 -0
- package/src/components/GEN-AI/index2.vue +548 -0
- package/src/components/GEN-AI/index3.vue +605 -0
- package/src/components/GEN-AI/index4.vue +609 -0
- package/src/components/GEN-AI/index5.vue +558 -0
- package/src/components/GEN-AI/index6.vue +634 -0
- package/src/components/GEN-AI/index7.vue +688 -0
- package/src/components/GEN-AI/index8.vue +458 -0
- package/src/components/GEN-AI/index9.vue +1217 -0
- package/src/components/GEN-AI/types.ts +12 -0
- package/src/components/GEN-AI/utils.ts +42 -0
- package/src/components/HelloWorld.vue +41 -0
- package/src/components/PageCard.vue +124 -0
- package/src/components/PageHeader.vue +32 -0
- package/src/components/backup/index5 copy.vue +556 -0
- package/src/components/backup/index5.vue +620 -0
- package/src/components/backup/index9 copy.vue +1029 -0
- package/src/components/backup/index9-pro.vue +1065 -0
- package/src/components/backup/index9.vue +1057 -0
- package/src/directives/btnLoading.ts +455 -0
- package/src/directives/debounce copy.ts +670 -0
- package/src/directives/debounce.ts +98 -0
- package/src/directives/index.ts +25 -0
- package/src/layouts/MainLayout.vue +129 -0
- package/src/main.ts +76 -0
- package/src/router/index.ts +142 -0
- package/src/router/menus.ts +28 -0
- package/src/store/modules/gen-ai/index.ts +1 -0
- package/src/store/modules/gen-ai/reportBuild.ts +37 -0
- package/src/style.css +79 -0
- package/src/styles/report-config-popover.css +39 -0
- package/src/styles/report-icon-btn.css +19 -0
- package/src/test/mock.ts +101 -0
- package/src/test/test1.vue +402 -0
- package/src/test/test2.vue +1689 -0
- package/src/types/gen-ai/build-report/reportEditor.ts +684 -0
- package/src/utils/function.ts +46 -0
- package/src/utils/gen-ai-navigation.ts +1 -0
- package/src/utils/gen-ai-scroll.ts +1 -0
- package/src/utils/openDataLoaderWordExport.ts +33 -0
- package/src/utils/pageScrollbar.ts +115 -0
- package/src/utils/randomTranscode.ts +87 -0
- package/src/utils/reportPdfExport.ts +44 -0
- package/src/views/Blank.vue +68 -0
- package/src/views/Home.vue +29 -0
- package/src/views/TemplateCenter/Knowledge.ts +83 -0
- package/src/views/TemplateCenter/data.d.ts +10 -0
- package/src/views/TemplateCenter/index.vue +1128 -0
- package/src/views/TemplateCenter/service.ts +69 -0
- package/src/views/gen-ai/build-report/chartStreamRefs.ts +174 -0
- package/src/views/gen-ai/build-report/coherence-check/index.vue +559 -0
- package/src/views/gen-ai/build-report/components/ReportChapterWorkspace.vue +4258 -0
- package/src/views/gen-ai/build-report/components/ReportConfigPanel.vue +727 -0
- package/src/views/gen-ai/build-report/components/ReportGenerateSettingsPanel.vue +2361 -0
- package/src/views/gen-ai/build-report/components/ReportGeneratingPanel.vue +1765 -0
- package/src/views/gen-ai/build-report/components/ReportPromptPanel.vue +614 -0
- package/src/views/gen-ai/build-report/components/ReportSourceFileSummary.vue +656 -0
- package/src/views/gen-ai/build-report/components/ReportSourcesPanel.vue +1327 -0
- package/src/views/gen-ai/build-report/components/ReportTemplateDetail.vue +191 -0
- package/src/views/gen-ai/build-report/components/ReportTemplateEditorFormPanel.vue +667 -0
- package/src/views/gen-ai/build-report/components/ReportTemplatePicker.vue +302 -0
- package/src/views/gen-ai/build-report/components/ReportWorkflowSidebar.vue +148 -0
- package/src/views/gen-ai/build-report/components/paramsSetting.vue +886 -0
- package/src/views/gen-ai/build-report/components/reportEdit.vue +2283 -0
- package/src/views/gen-ai/build-report/index.vue +856 -0
- package/src/views/gen-ai/build-report/report-domain.ts +1133 -0
- package/src/views/gen-ai/build-report/report-template-data.ts +295 -0
- package/src/views/gen-ai/build-report/useReportWorkflow.ts +1408 -0
- package/src/views/gen-ai/common/assets/NotoSans-SC.woff2 +0 -0
- package/src/views/gen-ai/common/assets/NotoSans-TC.woff2 +0 -0
- package/src/views/gen-ai/common/assets/NotoSans.woff2 +0 -0
- package/src/views/gen-ai/common/assets/fonts.css +278 -0
- package/src/views/gen-ai/common/assets/pagination.css +163 -0
- package/src/views/gen-ai/common/directives/btnLoading.ts +455 -0
- package/src/views/gen-ai/common/directives/debounce.ts +98 -0
- package/src/views/gen-ai/common/directives/index.ts +30 -0
- package/src/views/gen-ai/common/en.md +417 -0
- package/src/views/gen-ai/common/index.ts +36 -0
- package/src/views/gen-ai/common/types/component-expose.ts +9 -0
- package/src/views/gen-ai/common/utils/api-res-status.ts +7 -0
- package/src/views/gen-ai/common/utils/navigation.ts +59 -0
- package/src/views/gen-ai/common/utils/remote-request.ts +45 -0
- package/src/views/gen-ai/common/utils/scroll.ts +577 -0
- package/src/views/gen-ai/components/AIExport/docx-export.ts +950 -0
- package/src/views/gen-ai/components/AIExport/docxExport.ts +2566 -0
- package/src/views/gen-ai/components/AIExport/index.ts +52 -0
- package/src/views/gen-ai/components/AIExport/parseMarkdownHtml.ts +649 -0
- package/src/views/gen-ai/components/CapsuleScrollbar.vue +149 -0
- package/src/views/gen-ai/components/ChapterTitleScroll.vue +468 -0
- package/src/views/gen-ai/components/EntryHeroNavCards.vue +379 -0
- package/src/views/gen-ai/components/GenAiPagination.vue +52 -0
- package/src/views/gen-ai/components/ModelCard.vue +1114 -0
- package/src/views/gen-ai/components/Preview/FilePreviewPage.vue +947 -0
- package/src/views/gen-ai/components/Preview/PreviewCapsuleScroll.vue +205 -0
- package/src/views/gen-ai/components/Preview/PreviewOutlineTree.vue +148 -0
- package/src/views/gen-ai/components/Preview/index.ts +11 -0
- package/src/views/gen-ai/components/Preview/preview.ts +722 -0
- package/src/views/gen-ai/components/PromptField.vue +505 -0
- package/src/views/gen-ai/components/RecentReportsTable.vue +974 -0
- package/src/views/gen-ai/components/ReportCollapsibleSection.vue +172 -0
- package/src/views/gen-ai/components/ReportHistoryList.vue +637 -0
- package/src/views/gen-ai/components/ReportSectionConfirmPopover.vue +251 -0
- package/src/views/gen-ai/components/editor/ChapterTiptapEditor.vue +3139 -0
- package/src/views/gen-ai/components/editor/StructureBlockView.vue +441 -0
- package/src/views/gen-ai/components/editor/TiptapToolbar.vue +1036 -0
- package/src/views/gen-ai/components/editor/TypewriterDocEditor.vue +398 -0
- package/src/views/gen-ai/components/editor/_StructureOrgNode.vue +326 -0
- package/src/views/gen-ai/components/editor/index.ts +12 -0
- package/src/views/gen-ai/components/editor/parseMarkdownHtml.ts +667 -0
- package/src/views/gen-ai/gen-entry/api/index.ts +32 -0
- package/src/views/gen-ai/gen-entry/index.vue +287 -0
- package/src/views/gen-ai/gen-entry/types/index.ts +4 -0
- package/src/views/gen-ai/i.mark +2 -0
- package/src/views/gen-ai/management-center/index.vue +1815 -0
- package/src/views/gen-ai/management-center/mockData.ts +248 -0
- package/src/views/gen-ai/management-center/types/index.ts +37 -0
- package/src/views/gen-ai/model-manager/TemplateEditorFormPanel.vue +661 -0
- package/src/views/gen-ai/model-manager/api/index.ts +32 -0
- package/src/views/gen-ai/model-manager/api/mockApi.ts +33 -0
- package/src/views/gen-ai/model-manager/index.vue +1583 -0
- package/src/views/gen-ai/model-manager/mockData.ts +177 -0
- package/src/views/gen-ai/model-manager/template-detail-shared.ts +154 -0
- package/src/views/gen-ai/model-manager/template-editor.vue +345 -0
- package/src/views/gen-ai/model-manager/types/index.ts +40 -0
- package/src/views/gen-ai/model-manager/utils.ts +25 -0
- package/src/views/greenMortgage.vue +1834 -0
- package/src/vite-env.d.ts +46 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GEN-AI TipTap 編輯器(高度集成)
|
|
3
|
+
*
|
|
4
|
+
* - ChapterTiptapEditor.vue — 章節富文本(業務入口)
|
|
5
|
+
* - TiptapToolbar.vue — 格式 + 插入工具欄(僅編輯器內部)
|
|
6
|
+
* - StructureBlockView.vue — 結構區塊 NodeView(內含 _StructureOrgNode)
|
|
7
|
+
* - TypewriterDocEditor.vue — 多章打字機編輯器
|
|
8
|
+
*/
|
|
9
|
+
export { default as ReportChapterTiptapEditor } from './ChapterTiptapEditor.vue'
|
|
10
|
+
export { default as ReportTypewriterDocEditor } from './TypewriterDocEditor.vue'
|
|
11
|
+
export type { EditorReviewMarker } from './ChapterTiptapEditor.vue'
|
|
12
|
+
export { parseMarkdownToHtml } from './parseMarkdownHtml'
|
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 接口 sectionContent → HTML
|
|
3
|
+
*
|
|
4
|
+
* tip 兼容顺序:Markdown → HTML → Element Plus
|
|
5
|
+
* +link 继续走原解析:Markdown [text](url) / HTML <a> / el-link → a.report-tiptap-link
|
|
6
|
+
* tip 转成原 citation 标记(点击弹出 report-chapter-review-popover)
|
|
7
|
+
* [ref:uuid] → chartAsStream REF(chunkId)匹配后的 tip
|
|
8
|
+
*/
|
|
9
|
+
import { Marked } from 'marked'
|
|
10
|
+
import { getChartStreamRef } from '../../build-report/chartStreamRefs'
|
|
11
|
+
|
|
12
|
+
function escapeHtml(value: string) {
|
|
13
|
+
return value
|
|
14
|
+
.replace(/&/g, '&')
|
|
15
|
+
.replace(/</g, '<')
|
|
16
|
+
.replace(/>/g, '>')
|
|
17
|
+
.replace(/"/g, '"')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function escapeAttr(value: string) {
|
|
21
|
+
return escapeHtml(String(value).replace(/\s+/g, ' ').trim())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function splitTipLink(tip: string) {
|
|
25
|
+
const raw = String(tip || '').trim()
|
|
26
|
+
const md = raw.match(/\[([^\]]+)\]\((https?:[^)\s]+)(?:\s+"[^"]*")?\)/i)
|
|
27
|
+
if (md?.[1] != null && md[2] != null) {
|
|
28
|
+
return {
|
|
29
|
+
text: raw.replace(md[0], '').replace(/\s+/g, ' ').trim(),
|
|
30
|
+
href: md[2].trim(),
|
|
31
|
+
linkLabel: md[1].trim(),
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const html = raw.match(/<a\b[^>]*href=["'](https?:[^"']+)["'][^>]*>([\s\S]*?)<\/a>/i)
|
|
35
|
+
if (html?.[1] != null && html[2] != null) {
|
|
36
|
+
return {
|
|
37
|
+
text: raw.replace(html[0], '').replace(/\s+/g, ' ').trim(),
|
|
38
|
+
href: html[1].trim(),
|
|
39
|
+
linkLabel: html[2].replace(/<[^>]+>/g, '').trim(),
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const paren = raw.match(/^(.*?)\s*\((https?:[^)\s]+)\)\s*$/i)
|
|
43
|
+
if (paren?.[1] != null && paren[2] != null) {
|
|
44
|
+
return {
|
|
45
|
+
text: paren[1].replace(/\s+/g, ' ').trim(),
|
|
46
|
+
href: paren[2].trim(),
|
|
47
|
+
linkLabel: paren[2].trim(),
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { text: raw, href: '', linkLabel: '' }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function citationHtml(
|
|
54
|
+
id: string,
|
|
55
|
+
tip = '',
|
|
56
|
+
extra?: {
|
|
57
|
+
sourceName?: string
|
|
58
|
+
filePath?: string
|
|
59
|
+
filePreviewUrl?: string
|
|
60
|
+
chunkId?: string
|
|
61
|
+
}
|
|
62
|
+
) {
|
|
63
|
+
const n = String(id).trim() || '1'
|
|
64
|
+
const { text, href, linkLabel } = splitTipLink(tip)
|
|
65
|
+
const tipAttr = text ? ` data-tip="${escapeAttr(text)}"` : ''
|
|
66
|
+
const hrefAttr = href ? ` data-href="${escapeAttr(href)}"` : ''
|
|
67
|
+
const labelAttr = linkLabel ? ` data-link-label="${escapeAttr(linkLabel)}"` : ''
|
|
68
|
+
const sourceNameAttr = extra?.sourceName
|
|
69
|
+
? ` data-source-name="${escapeAttr(extra.sourceName)}"`
|
|
70
|
+
: ''
|
|
71
|
+
const filePathAttr = extra?.filePath ? ` data-file-path="${escapeAttr(extra.filePath)}"` : ''
|
|
72
|
+
const filePreviewAttr = extra?.filePreviewUrl
|
|
73
|
+
? ` data-file-preview-url="${escapeAttr(extra.filePreviewUrl)}"`
|
|
74
|
+
: ''
|
|
75
|
+
const chunkIdAttr = extra?.chunkId ? ` data-chunk-id="${escapeAttr(extra.chunkId)}"` : ''
|
|
76
|
+
return `<span class="report-tiptap-citation" data-citation="${escapeHtml(n)}"${tipAttr}${hrefAttr}${labelAttr}${sourceNameAttr}${filePathAttr}${filePreviewAttr}${chunkIdAttr}>${escapeHtml(n)}</span>`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const REF_MARKER_RE =
|
|
80
|
+
/\[ref:([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\]/gi
|
|
81
|
+
|
|
82
|
+
function isRealHref(href: string) {
|
|
83
|
+
const value = String(href || '').trim()
|
|
84
|
+
if (!value || value === '#') return false
|
|
85
|
+
if (/^#(?:fn|footnote|cite|ref)/i.test(value)) return false
|
|
86
|
+
return /^(https?:|mailto:|tel:|\/|\.\/|\.\.\/|#\/)/i.test(value) || /^[a-z][a-z0-9+.-]*:/i.test(value)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function isCitationLabel(text: string) {
|
|
90
|
+
const label = text.replace(/<[^>]+>/g, '').trim()
|
|
91
|
+
return /^\[?\d+\]?$/.test(label) || /^[+*]?\d+$/.test(label)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function idFromLabel(label: string, fallback: string) {
|
|
95
|
+
const m = String(label).replace(/<[^>]+>/g, '').trim().match(/(\d+)/)
|
|
96
|
+
return m?.[1] ?? fallback
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let citationSeq = 0
|
|
100
|
+
function nextId(label: string) {
|
|
101
|
+
const fromLabel = idFromLabel(label, '')
|
|
102
|
+
if (fromLabel) return fromLabel
|
|
103
|
+
citationSeq += 1
|
|
104
|
+
return String(citationSeq)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function extractAttr(tag: string, name: string) {
|
|
108
|
+
const m = tag.match(new RegExp(`(?:^|\\s)${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, 'i'))
|
|
109
|
+
return (m?.[1] ?? m?.[2] ?? m?.[3] ?? '').trim()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const PLACEHOLDER = '%%GENAI_BLOCK_'
|
|
113
|
+
|
|
114
|
+
function protectBlocks(source: string) {
|
|
115
|
+
const slots: string[] = []
|
|
116
|
+
const stash = (html: string) => {
|
|
117
|
+
const i = slots.length
|
|
118
|
+
slots.push(html)
|
|
119
|
+
return `${PLACEHOLDER}${i}%%`
|
|
120
|
+
}
|
|
121
|
+
let text = source.replace(
|
|
122
|
+
/<span\b[^>]*class="[^"]*report-tiptap-citation[^"]*"[^>]*>[\s\S]*?<\/span>/gi,
|
|
123
|
+
(m) => stash(m)
|
|
124
|
+
)
|
|
125
|
+
text = text.replace(/<(table|pre|blockquote|details)[\s\S]*?<\/\1>/gi, (m) => stash(m))
|
|
126
|
+
text = text.replace(/<(el-(?:tooltip|popover|link))\b[\s\S]*?<\/\1>/gi, (m) => stash(m))
|
|
127
|
+
return { text, slots }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function restoreBlocks(html: string, slots: string[]) {
|
|
131
|
+
return html.replace(new RegExp(`${PLACEHOLDER}(\\d+)%%`, 'g'), (_, i) => slots[Number(i)] ?? '')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Markdown 层 tip:脚注定义、[^n]、[+n]、[ref:uuid];[+link] / [text](url) 不在此当 tip */
|
|
135
|
+
function applyMarkdownTips(source: string) {
|
|
136
|
+
const footnotes = new Map<string, string>()
|
|
137
|
+
let text = source.replace(/^\[\^(\d+)\]:\s*(.+)$/gm, (_, id: string, tip: string) => {
|
|
138
|
+
footnotes.set(id, tip.trim())
|
|
139
|
+
return ''
|
|
140
|
+
})
|
|
141
|
+
text = text.replace(REF_MARKER_RE, (full, chunkId: string) => {
|
|
142
|
+
const ref = getChartStreamRef(chunkId)
|
|
143
|
+
// 匹配不到的 [ref:uuid] 全部过滤,不进打字机 / 预览正文
|
|
144
|
+
if (!ref) return ''
|
|
145
|
+
return citationHtml(String(ref.index), ref.sourceText, {
|
|
146
|
+
sourceName: ref.sourceName,
|
|
147
|
+
filePath: ref.filePath,
|
|
148
|
+
filePreviewUrl: ref.filePreviewUrl,
|
|
149
|
+
chunkId: ref.chunkId,
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
text = text.replace(/\[\+(\d+)\]/g, (_, id: string) => citationHtml(id))
|
|
153
|
+
text = text.replace(/\[\^(\d+)\](?!:)/g, (_, id: string) =>
|
|
154
|
+
citationHtml(id, footnotes.get(id) || '')
|
|
155
|
+
)
|
|
156
|
+
return { text, footnotes }
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function linkHtml(href: string, inner: string, title = '') {
|
|
160
|
+
const titleAttr = title ? ` title="${escapeAttr(title)}"` : ''
|
|
161
|
+
return `<a href="${escapeHtml(href)}" class="report-tiptap-link" target="_blank" rel="noopener noreferrer"${titleAttr}>${inner}</a>`
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function isTipHref(href: string, title: string, _label?: string) {
|
|
165
|
+
if (isRealHref(href)) return false
|
|
166
|
+
if (title) return true
|
|
167
|
+
return /^#(?:fn|footnote|cite|ref)/i.test(href)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const markedParser = new Marked({
|
|
171
|
+
gfm: true,
|
|
172
|
+
breaks: true,
|
|
173
|
+
renderer: {
|
|
174
|
+
link({ href, title, tokens }) {
|
|
175
|
+
const text = this.parser.parseInline(tokens)
|
|
176
|
+
const tip = String(title || '').trim()
|
|
177
|
+
if (isTipHref(String(href || ''), tip)) {
|
|
178
|
+
return citationHtml(nextId(text), tip)
|
|
179
|
+
}
|
|
180
|
+
return linkHtml(String(href || '#'), text, tip)
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
/** HTML 层:已有 citation / title / 脚注 a / 数字 sup → tip;真实 a → +link */
|
|
186
|
+
function applyHtmlTipsAndLinks(html: string) {
|
|
187
|
+
let out = html.replace(/<a\b([^>]*)>([\s\S]*?)<\/a>/gi, (m, attrs: string, inner: string) => {
|
|
188
|
+
if (/report-tiptap-citation/i.test(attrs)) return m
|
|
189
|
+
const href = extractAttr(attrs, 'href')
|
|
190
|
+
const title = extractAttr(attrs, 'title') || extractAttr(attrs, 'data-tip')
|
|
191
|
+
const label = inner.replace(/<[^>]+>/g, '').trim()
|
|
192
|
+
if (isTipHref(href, title) && !isRealHref(href)) {
|
|
193
|
+
return citationHtml(nextId(label || '1'), title)
|
|
194
|
+
}
|
|
195
|
+
if (/report-tiptap-link/i.test(attrs) && isRealHref(href)) return m
|
|
196
|
+
return linkHtml(href || '#', inner, title)
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
out = out.replace(/<(?:abbr|span)\b([^>]*)>([\s\S]*?)<\/(?:abbr|span)>/gi, (m, attrs: string, inner: string) => {
|
|
200
|
+
if (/report-tiptap-citation/i.test(attrs) || /report-tiptap-link/i.test(attrs)) return m
|
|
201
|
+
const citationId = extractAttr(attrs, 'data-citation')
|
|
202
|
+
const tip = extractAttr(attrs, 'data-tip') || extractAttr(attrs, 'title')
|
|
203
|
+
const label = inner.replace(/<[^>]+>/g, '').trim()
|
|
204
|
+
if (citationId) return citationHtml(citationId, tip)
|
|
205
|
+
if (tip && isCitationLabel(label)) return citationHtml(nextId(label), tip)
|
|
206
|
+
return m
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
out = out.replace(/<sup\b([^>]*)>([\s\S]*?)<\/sup>/gi, (m, attrs: string, inner: string) => {
|
|
210
|
+
const tip = extractAttr(attrs, 'title') || extractAttr(attrs, 'data-tip')
|
|
211
|
+
const label = inner.replace(/<[^>]+>/g, '').trim()
|
|
212
|
+
if (!tip || !isCitationLabel(label)) return m
|
|
213
|
+
return citationHtml(nextId(label), tip)
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
return out
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Element Plus 层:el-tooltip / el-popover → 原 tip;el-link → +link */
|
|
220
|
+
function applyElementPlusTipsAndLinks(html: string) {
|
|
221
|
+
let out = html.replace(
|
|
222
|
+
/<el-tooltip\b([^>]*)>([\s\S]*?)<\/el-tooltip>/gi,
|
|
223
|
+
(_m, attrs: string, inner: string) => {
|
|
224
|
+
const tip = extractAttr(attrs, 'content') || extractAttr(attrs, 'title')
|
|
225
|
+
const label = inner.replace(/<[^>]+>/g, '').trim()
|
|
226
|
+
if (!tip) return inner
|
|
227
|
+
return citationHtml(nextId(label || '1'), tip)
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
out = out.replace(
|
|
231
|
+
/<el-popover\b([^>]*)>([\s\S]*?)<\/el-popover>/gi,
|
|
232
|
+
(_m, attrs: string, inner: string) => {
|
|
233
|
+
const slotTip =
|
|
234
|
+
inner
|
|
235
|
+
.match(/<template[^>]*(?:#|v-slot:|slot=)[^>]*(?:content|default)[^>]*>([\s\S]*?)<\/template>/i)?.[1]
|
|
236
|
+
?.replace(/<[^>]+>/g, ' ')
|
|
237
|
+
.replace(/\s+/g, ' ')
|
|
238
|
+
.trim() || ''
|
|
239
|
+
const tip = extractAttr(attrs, 'content') || extractAttr(attrs, 'title') || slotTip
|
|
240
|
+
const label = inner.replace(/<template[\s\S]*?<\/template>/gi, '').replace(/<[^>]+>/g, '').trim()
|
|
241
|
+
if (!tip) return inner.replace(/<template[\s\S]*?<\/template>/gi, '')
|
|
242
|
+
return citationHtml(nextId(label || '1'), tip)
|
|
243
|
+
}
|
|
244
|
+
)
|
|
245
|
+
out = out.replace(/<el-link\b([^>]*)>([\s\S]*?)<\/el-link>/gi, (_m, attrs: string, inner: string) => {
|
|
246
|
+
const href = extractAttr(attrs, 'href')
|
|
247
|
+
const title = extractAttr(attrs, 'title') || extractAttr(attrs, 'content')
|
|
248
|
+
const label = inner.replace(/<[^>]+>/g, '').trim()
|
|
249
|
+
if (!isRealHref(href) && title) {
|
|
250
|
+
return citationHtml(nextId(label || '1'), title)
|
|
251
|
+
}
|
|
252
|
+
return linkHtml(href || '#', inner, title)
|
|
253
|
+
})
|
|
254
|
+
return out
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function applyFootnoteTips(html: string, footnotes: Map<string, string>) {
|
|
258
|
+
if (!footnotes.size) return html
|
|
259
|
+
return html.replace(
|
|
260
|
+
/<span class="report-tiptap-citation" data-citation="(\d+)"([^>]*)>([\s\S]*?)<\/span>/gi,
|
|
261
|
+
(m, id: string, rest: string, inner: string) => {
|
|
262
|
+
const raw = footnotes.get(id)
|
|
263
|
+
if (!raw) return m
|
|
264
|
+
const { text, href, linkLabel } = splitTipLink(raw)
|
|
265
|
+
if (/data-href=/.test(rest) && /data-tip=/.test(rest)) return m
|
|
266
|
+
const cleaned = String(rest).replace(/\sdata-(?:tip|href|link-label)="[^"]*"/gi, '')
|
|
267
|
+
const tipAttr = text ? ` data-tip="${escapeAttr(text)}"` : ''
|
|
268
|
+
const hrefAttr = href ? ` data-href="${escapeAttr(href)}"` : ''
|
|
269
|
+
const labelAttr = linkLabel ? ` data-link-label="${escapeAttr(linkLabel)}"` : ''
|
|
270
|
+
return `<span class="report-tiptap-citation" data-citation="${id}"${tipAttr}${hrefAttr}${labelAttr}${cleaned}>${inner}</span>`
|
|
271
|
+
}
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const UNCLOSED_BLOCK_RE =
|
|
276
|
+
/<(table|thead|tbody|tr|td|th|pre|blockquote|ul|ol|li|p|h[1-6]|think|strong|b|em|i|u|s|code|mark|div|section|article|figure|figcaption|header|footer|details|summary|font|ins|del|caption|el-tooltip|el-popover|el-link|span|a)\b[^>]*>(?:(?!<\/\1>)[\s\S])*$/i
|
|
277
|
+
|
|
278
|
+
export function stripThinkBlocks(source: string) {
|
|
279
|
+
return source
|
|
280
|
+
.replace(/<think\b[^>]*>[\s\S]*?<\/think>/gi, '')
|
|
281
|
+
.replace(/<think\b[\s\S]*$/i, '')
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const CJK_CHAR =
|
|
285
|
+
/[\u3400-\u9FFF\uF900-\uFAFF\u3000-\u303F\u3040-\u30FF\uAC00-\uD7AF]/
|
|
286
|
+
|
|
287
|
+
/** 補回可讀空格:空白規範化、中西交界(保留換行);英文分詞交由後端保證 */
|
|
288
|
+
export function normalizeReadableSpacing(source: string): string {
|
|
289
|
+
let text = String(source ?? '')
|
|
290
|
+
if (!text) return ''
|
|
291
|
+
text = text.replace(/\u00a0/g, ' ')
|
|
292
|
+
// 0914:空白規範化 + 中西交界(英文粘詞由後端輸出正常空格)
|
|
293
|
+
text = text.replace(/[^\S\n\r]+/g, ' ')
|
|
294
|
+
text = text.replace(
|
|
295
|
+
new RegExp(`(${CJK_CHAR.source})([A-Za-z0-9])`, 'g'),
|
|
296
|
+
'$1 $2'
|
|
297
|
+
)
|
|
298
|
+
text = text.replace(
|
|
299
|
+
new RegExp(`([A-Za-z0-9])(${CJK_CHAR.source})`, 'g'),
|
|
300
|
+
'$1 $2'
|
|
301
|
+
)
|
|
302
|
+
// 0914
|
|
303
|
+
return text
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** 是否像 Markdown(ATX 標題 / 列表 / 加粗),用於決定要不要拆 <p>/<br>/字面 \\n */
|
|
307
|
+
function looksLikeMarkdown(text: string) {
|
|
308
|
+
const probe = String(text || '')
|
|
309
|
+
.replace(/<(?:br)\s*\/?>/gi, '\n')
|
|
310
|
+
.replace(/<\/(?:p|div)\s*>/gi, '\n')
|
|
311
|
+
.replace(/<(?:p|div)\b[^>]*>/gi, '')
|
|
312
|
+
return /(?:^|\n)\s{0,3}#{1,6}\s|(?:^|\n)\s{0,3}(?:[-*+]|\d{1,9}[.)])\s|\*{3}[^*\n]+\*{3}|\*{2}[^*\n]+\*{2}/m.test(
|
|
313
|
+
probe
|
|
314
|
+
)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* 接口正文常把 Markdown 塞进 <p>/<br> 或字面 \\n,
|
|
319
|
+
* marked 無法把行首 ### / * 認成標題與列表,畫面就會打出原始符號。
|
|
320
|
+
* 閉合 HTML 結構保留;僅在「像 Markdown」時拆開陷阱標籤。
|
|
321
|
+
*/
|
|
322
|
+
function normalizeMarkdownSource(source: string) {
|
|
323
|
+
let text = String(source ?? '')
|
|
324
|
+
if (!text) return ''
|
|
325
|
+
|
|
326
|
+
const realNl = (text.match(/\r?\n/g) || []).length
|
|
327
|
+
const escapedNl = (text.match(/\\n/g) || []).length
|
|
328
|
+
if (escapedNl > 0 && escapedNl >= realNl) {
|
|
329
|
+
const unescaped = text.replace(/\\r\\n/g, '\n').replace(/\\n/g, '\n').replace(/\\t/g, '\t')
|
|
330
|
+
if (looksLikeMarkdown(unescaped)) text = unescaped
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (!looksLikeMarkdown(text)) {
|
|
334
|
+
return normalizeReadableSpacing(text)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (/<br\s*\/?>/i.test(text)) {
|
|
338
|
+
text = text.replace(/<br\s*\/?>/gi, '\n')
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (/<(?:p|div)\b/i.test(text)) {
|
|
342
|
+
const unwrapped = text
|
|
343
|
+
.replace(/<\/(?:p|div)\s*>\s*<(?:p|div)\b[^>]*>/gi, '\n\n')
|
|
344
|
+
.replace(/<(?:p|div)\b[^>]*>/gi, '')
|
|
345
|
+
.replace(/<\/(?:p|div)\s*>/gi, '\n')
|
|
346
|
+
if (looksLikeMarkdown(unwrapped)) text = unwrapped
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return normalizeReadableSpacing(text)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* 在 source 上掃描,自動跳過 ```fence``` 與行內 `code`,
|
|
354
|
+
* 避免代碼裡的 ** / * / # 被當成未閉合 Markdown。
|
|
355
|
+
*/
|
|
356
|
+
function scanMarkdownOutsideCode(
|
|
357
|
+
source: string,
|
|
358
|
+
visit: (i: number, ch: string) => number | void
|
|
359
|
+
) {
|
|
360
|
+
let i = 0
|
|
361
|
+
let inFence = false
|
|
362
|
+
while (i < source.length) {
|
|
363
|
+
if (source.startsWith('```', i)) {
|
|
364
|
+
inFence = !inFence
|
|
365
|
+
i += 3
|
|
366
|
+
continue
|
|
367
|
+
}
|
|
368
|
+
if (inFence) {
|
|
369
|
+
i += 1
|
|
370
|
+
continue
|
|
371
|
+
}
|
|
372
|
+
if (source.charAt(i) === '`') {
|
|
373
|
+
let n = 0
|
|
374
|
+
while (source.charAt(i + n) === '`') n += 1
|
|
375
|
+
const open = source.slice(i, i + n)
|
|
376
|
+
const closeAt = source.indexOf(open, i + n)
|
|
377
|
+
if (closeAt < 0) {
|
|
378
|
+
// 半截行內 code:剩餘整段視為 code,不再掃描標記
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
i = closeAt + n
|
|
382
|
+
continue
|
|
383
|
+
}
|
|
384
|
+
const step = visit(i, source.charAt(i))
|
|
385
|
+
i += typeof step === 'number' && step > 0 ? step : 1
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function lastUnpairedFence(source: string, fence: string) {
|
|
390
|
+
let odd = false
|
|
391
|
+
let last = -1
|
|
392
|
+
scanMarkdownOutsideCode(source, (i) => {
|
|
393
|
+
// 統計 ** 時整段跳過 ***,避免三星被拆成 ** + *
|
|
394
|
+
if (fence === '**' && source.startsWith('***', i)) return 3
|
|
395
|
+
if (fence === '__' && source.startsWith('___', i)) return 3
|
|
396
|
+
if (source.startsWith(fence, i)) {
|
|
397
|
+
odd = !odd
|
|
398
|
+
last = i
|
|
399
|
+
return fence.length
|
|
400
|
+
}
|
|
401
|
+
})
|
|
402
|
+
return odd ? last : -1
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function lastUnpairedInlineTick(source: string) {
|
|
406
|
+
let odd = false
|
|
407
|
+
let last = -1
|
|
408
|
+
let i = 0
|
|
409
|
+
let inFence = false
|
|
410
|
+
while (i < source.length) {
|
|
411
|
+
if (source.startsWith('```', i)) {
|
|
412
|
+
inFence = !inFence
|
|
413
|
+
i += 3
|
|
414
|
+
continue
|
|
415
|
+
}
|
|
416
|
+
if (inFence) {
|
|
417
|
+
i += 1
|
|
418
|
+
continue
|
|
419
|
+
}
|
|
420
|
+
if (source.charAt(i) === '`') {
|
|
421
|
+
odd = !odd
|
|
422
|
+
last = i
|
|
423
|
+
}
|
|
424
|
+
i += 1
|
|
425
|
+
}
|
|
426
|
+
return odd ? last : -1
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function lastUnpairedSingleStar(source: string) {
|
|
430
|
+
let odd = false
|
|
431
|
+
let last = -1
|
|
432
|
+
scanMarkdownOutsideCode(source, (i) => {
|
|
433
|
+
if (source.startsWith('***', i)) return 3
|
|
434
|
+
if (source.startsWith('**', i)) return 2
|
|
435
|
+
if (source.charAt(i) !== '*') return
|
|
436
|
+
const atLineStart = i === 0 || source.charAt(i - 1) === '\n'
|
|
437
|
+
const next = source.charAt(i + 1)
|
|
438
|
+
if (atLineStart && (next === ' ' || next === '\t')) return 1
|
|
439
|
+
odd = !odd
|
|
440
|
+
last = i
|
|
441
|
+
})
|
|
442
|
+
return odd ? last : -1
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function lastUnpairedSingleUnderscore(source: string) {
|
|
446
|
+
let odd = false
|
|
447
|
+
let last = -1
|
|
448
|
+
scanMarkdownOutsideCode(source, (i) => {
|
|
449
|
+
if (source.startsWith('__', i)) return 2
|
|
450
|
+
if (source.charAt(i) !== '_') return
|
|
451
|
+
const prev = i === 0 ? ' ' : source.charAt(i - 1)
|
|
452
|
+
const next = source.charAt(i + 1)
|
|
453
|
+
if (/[A-Za-z0-9]/.test(prev) && /[A-Za-z0-9]/.test(next)) return 1
|
|
454
|
+
odd = !odd
|
|
455
|
+
last = i
|
|
456
|
+
})
|
|
457
|
+
return odd ? last : -1
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** 完整主題分隔線(HR),不應當半截標記 */
|
|
461
|
+
function isCompleteThematicBreak(text: string) {
|
|
462
|
+
return /^\s*(?:-{3,}|\*{3,}|_{3,})\s*$/.test(text)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** 打字机:切开已闭合前缀 vs 半截标记;半截内文继续打出,避免整段闪没 */
|
|
466
|
+
function splitStreamStablePrefix(source: string) {
|
|
467
|
+
const cuts: number[] = []
|
|
468
|
+
const lastLt = source.lastIndexOf('<')
|
|
469
|
+
const lastGt = source.lastIndexOf('>')
|
|
470
|
+
if (lastLt > lastGt && /[a-zA-Z/!]/.test(source.charAt(lastLt + 1) || '')) {
|
|
471
|
+
cuts.push(lastLt)
|
|
472
|
+
}
|
|
473
|
+
const block = source.match(UNCLOSED_BLOCK_RE)
|
|
474
|
+
if (block && block.index != null) cuts.push(block.index)
|
|
475
|
+
const linkOpen = source.search(/\[[^\]]*$|\[[^\]]*\]\([^)]*$/)
|
|
476
|
+
if (linkOpen >= 0) cuts.push(linkOpen)
|
|
477
|
+
const plusOpen = source.search(/\[\+(?:\d+)?$|\[\^\d*$/)
|
|
478
|
+
if (plusOpen >= 0) cuts.push(plusOpen)
|
|
479
|
+
// 半截 [ref:uuid] 先不进正文,避免打出 ref:... 再闪成 tip
|
|
480
|
+
const refOpen = source.search(/\[ref:[0-9a-f-]*$/i)
|
|
481
|
+
if (refOpen >= 0) cuts.push(refOpen)
|
|
482
|
+
// 完整 [ref:uuid] 贴在末尾时也先切出:等后续字符再解析,避免打字光标贴在 tip 上像「3|」
|
|
483
|
+
const trailingRef = source.search(
|
|
484
|
+
/\[ref:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\]$/i
|
|
485
|
+
)
|
|
486
|
+
if (trailingRef >= 0) cuts.push(trailingRef)
|
|
487
|
+
for (const fence of ['```', '***', '**', '__', '~~'] as const) {
|
|
488
|
+
const at = lastUnpairedFence(source, fence)
|
|
489
|
+
if (at >= 0) cuts.push(at)
|
|
490
|
+
}
|
|
491
|
+
const tick = lastUnpairedInlineTick(source)
|
|
492
|
+
if (tick >= 0) cuts.push(tick)
|
|
493
|
+
const star = lastUnpairedSingleStar(source)
|
|
494
|
+
if (star >= 0) cuts.push(star)
|
|
495
|
+
const under = lastUnpairedSingleUnderscore(source)
|
|
496
|
+
if (under >= 0) cuts.push(under)
|
|
497
|
+
const mdBlock = lastIncompleteMarkdownCut(source)
|
|
498
|
+
if (mdBlock >= 0) cuts.push(mdBlock)
|
|
499
|
+
if (!cuts.length) return { stable: source, rest: '' }
|
|
500
|
+
const at = Math.min(...cuts)
|
|
501
|
+
const stable = source.slice(0, at)
|
|
502
|
+
const rest = source.slice(at)
|
|
503
|
+
// 誤切:rest 本身已是完整 HR,應併入 stable 交給 marked
|
|
504
|
+
if (rest && isCompleteThematicBreak(rest.trim())) {
|
|
505
|
+
return { stable: source, rest: '' }
|
|
506
|
+
}
|
|
507
|
+
return { stable, rest }
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** 半截标题 / 列表 / 引用 / 表格 / setext 先不交给 marked,避免结构来回跳 */
|
|
511
|
+
function lastIncompleteMarkdownCut(source: string) {
|
|
512
|
+
const cuts: number[] = []
|
|
513
|
+
const pushMarker = (re: RegExp) => {
|
|
514
|
+
const m = source.match(re)
|
|
515
|
+
if (!m || m.index == null) return
|
|
516
|
+
cuts.push(m[0].startsWith('\n') ? m.index + 1 : m.index)
|
|
517
|
+
}
|
|
518
|
+
// 僅標題符號、尚無正文
|
|
519
|
+
pushMarker(/(?:^|\n) {0,3}#{1,6}$/)
|
|
520
|
+
// ### 後只有空格、標題字還在打
|
|
521
|
+
pushMarker(/(?:^|\n) {0,3}#{1,6}[ \t]+$/)
|
|
522
|
+
pushMarker(/(?:^|\n) {0,3}(?:[-*+]|\d{1,9}[.)])$/)
|
|
523
|
+
pushMarker(/(?:^|\n) {0,3}>$/)
|
|
524
|
+
// 僅 1~2 個 -/_(等第 3 個才成 HR);完整 --- 不切
|
|
525
|
+
pushMarker(/(?:^|\n) {0,3}(?:-{1,2}|_{1,2})$/)
|
|
526
|
+
// setext 下劃線未完成(1~2);≥3 交給 marked 解析為標題/HR
|
|
527
|
+
const setext = source.match(/\n[ \t]*([-=])\1{0,1}$/)
|
|
528
|
+
if (setext && setext.index != null) cuts.push(setext.index + 1)
|
|
529
|
+
const table = incompleteTableCut(source)
|
|
530
|
+
if (table >= 0) cuts.push(table)
|
|
531
|
+
return cuts.length ? Math.min(...cuts) : -1
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function incompleteTableCut(source: string) {
|
|
535
|
+
const lines = source.split('\n')
|
|
536
|
+
const lastLine = lines[lines.length - 1]
|
|
537
|
+
if (!lines.length || lastLine == null || !/^\s*\|/.test(lastLine)) return -1
|
|
538
|
+
let i = lines.length - 1
|
|
539
|
+
while (i > 0 && /^\s*\|/.test(lines[i - 1] ?? '')) i -= 1
|
|
540
|
+
const block = lines.slice(i)
|
|
541
|
+
const hasSep = block.some((line) => /^\s*\|?\s*:?-{2,}/.test(line))
|
|
542
|
+
const last = block[block.length - 1] || ''
|
|
543
|
+
const lastComplete = /\|.+\|/.test(last.trim())
|
|
544
|
+
if (hasSep && lastComplete && block.length >= 3) return -1
|
|
545
|
+
return i === 0 ? 0 : lines.slice(0, i).join('\n').length + 1
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function htmlInnerPlain(rest: string) {
|
|
549
|
+
const gt = rest.indexOf('>')
|
|
550
|
+
if (gt < 0) return ''
|
|
551
|
+
let inner = rest.slice(gt + 1)
|
|
552
|
+
inner = inner.replace(/<\/[a-zA-Z][^>]*>/g, '').replace(/<[a-zA-Z][^>]*\/?>/g, '')
|
|
553
|
+
const lastLt = inner.lastIndexOf('<')
|
|
554
|
+
const lastGt = inner.lastIndexOf('>')
|
|
555
|
+
if (lastLt > lastGt) inner = inner.slice(0, lastLt)
|
|
556
|
+
return inner
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* 半截标记不把内文藏掉:`**xxx` / `<strong>xxx` 继续打字,只不画出 ** / 标签本身。
|
|
561
|
+
* 闭合后走正式解析变成粗体,与打字机进度同步、避免整段闪没。
|
|
562
|
+
*/
|
|
563
|
+
function visiblePlainFromIncomplete(rest: string) {
|
|
564
|
+
if (!rest) return ''
|
|
565
|
+
const leadWs = rest.match(/^[ \t]*/)?.[0] ?? ''
|
|
566
|
+
const t = rest.slice(leadWs.length)
|
|
567
|
+
if (!t) return ''
|
|
568
|
+
if (isCompleteThematicBreak(t)) return ''
|
|
569
|
+
if (t.startsWith('```')) {
|
|
570
|
+
const nl = t.indexOf('\n')
|
|
571
|
+
return nl < 0 ? '' : leadWs + t.slice(nl + 1)
|
|
572
|
+
}
|
|
573
|
+
if (t.startsWith('***')) return leadWs + t.slice(3)
|
|
574
|
+
if (t.startsWith('**')) return leadWs + t.slice(2)
|
|
575
|
+
if (t.startsWith('__')) return leadWs + t.slice(2)
|
|
576
|
+
if (t.startsWith('~~')) return leadWs + t.slice(2)
|
|
577
|
+
if (/^#{1,6}$/.test(t)) return ''
|
|
578
|
+
if (/^#{1,6}[ \t]+$/.test(t)) return ''
|
|
579
|
+
if (/^#{1,6}[ \t]+/.test(t)) return leadWs + t.replace(/^#{1,6}[ \t]+/, '')
|
|
580
|
+
if (/^([-*+]|\d{1,9}[.)])$/.test(t)) return ''
|
|
581
|
+
if (/^([-*+]|\d{1,9}[.)]) /.test(t)) return leadWs + t.replace(/^([-*+]|\d{1,9}[.)]) /, '')
|
|
582
|
+
if (t === '>') return ''
|
|
583
|
+
if (/^\s*\|/.test(t)) return leadWs + t.replace(/^\s*\|/, '').replace(/\|/g, ' ')
|
|
584
|
+
if (t.startsWith('`')) return leadWs + t.slice(1).replace(/`/g, '')
|
|
585
|
+
if (/^\[ref:/i.test(t)) return ''
|
|
586
|
+
if (t.startsWith('[') || t.startsWith('![')) {
|
|
587
|
+
const inner = t.replace(/^!?\[/, '')
|
|
588
|
+
const close = inner.indexOf(']')
|
|
589
|
+
return leadWs + (close < 0 ? inner : inner.slice(0, close))
|
|
590
|
+
}
|
|
591
|
+
if (t.startsWith('<')) return leadWs + htmlInnerPlain(t)
|
|
592
|
+
if (t.startsWith('*')) return leadWs + t.slice(1)
|
|
593
|
+
if (t.startsWith('_')) return leadWs + t.slice(1)
|
|
594
|
+
return leadWs + t
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function appendPlainTail(html: string, rest: string) {
|
|
598
|
+
if (!rest) return html
|
|
599
|
+
const escaped = escapeHtml(rest).replace(/\n/g, '<br>')
|
|
600
|
+
if (!html) return `<p>${escaped}</p>`
|
|
601
|
+
// 表格 / 标题半截单独成段,避免塞进上一块后再整体跳结构
|
|
602
|
+
if (/^\s*\|/.test(rest.trimStart()) || /^\s*#{1,6}(?:\s|$)/.test(rest.trimStart())) {
|
|
603
|
+
return `${html}<p>${escaped}</p>`
|
|
604
|
+
}
|
|
605
|
+
const closeRe = /<\/(?:p|li|h[1-6]|td|th)\b[^>]*>/gi
|
|
606
|
+
let last = -1
|
|
607
|
+
let m: RegExpExecArray | null
|
|
608
|
+
while ((m = closeRe.exec(html))) last = m.index
|
|
609
|
+
if (last >= 0) return html.slice(0, last) + escaped + html.slice(last)
|
|
610
|
+
return `${html}<p>${escaped}</p>`
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** 将 Markdown / HTML / Element Plus 解析为 TipTap 可 setContent 的片段 */
|
|
614
|
+
export function parseMarkdownToHtml(source: string): string {
|
|
615
|
+
const text = normalizeMarkdownSource(String(source ?? ''))
|
|
616
|
+
if (!text.trim()) return ''
|
|
617
|
+
citationSeq = 0
|
|
618
|
+
try {
|
|
619
|
+
const withoutThink = stripThinkBlocks(text)
|
|
620
|
+
if (!withoutThink.trim()) return ''
|
|
621
|
+
const mdTips = applyMarkdownTips(withoutThink)
|
|
622
|
+
const protectedSrc = protectBlocks(mdTips.text)
|
|
623
|
+
const parsed = markedParser.parse(protectedSrc.text, { async: false })
|
|
624
|
+
let html = typeof parsed === 'string' ? parsed.trim() : ''
|
|
625
|
+
html = restoreBlocks(html, protectedSrc.slots)
|
|
626
|
+
html = applyHtmlTipsAndLinks(html)
|
|
627
|
+
html = applyElementPlusTipsAndLinks(html)
|
|
628
|
+
html = applyFootnoteTips(html, mdTips.footnotes)
|
|
629
|
+
return html
|
|
630
|
+
} catch {
|
|
631
|
+
return `<p>${escapeHtml(stripThinkBlocks(text))}</p>`
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
let streamStableCacheKey = ''
|
|
636
|
+
let streamStableCacheHtml = ''
|
|
637
|
+
|
|
638
|
+
/** 打字机逐帧:完整语法走解析;半截 ** / 标签不画,内文继续打出,打完再灌全文
|
|
639
|
+
* @param flushTrailingRef 收流追平时把末尾完整 [ref:uuid] 一并解析成 tip
|
|
640
|
+
*/
|
|
641
|
+
export function parseMarkdownToHtmlStream(
|
|
642
|
+
source: string,
|
|
643
|
+
options?: { flushTrailingRef?: boolean }
|
|
644
|
+
): string {
|
|
645
|
+
const text = stripThinkBlocks(normalizeMarkdownSource(String(source ?? '')))
|
|
646
|
+
if (!text) return ''
|
|
647
|
+
const { stable, rest } = splitStreamStablePrefix(text)
|
|
648
|
+
const trailingRefOnly =
|
|
649
|
+
!!rest &&
|
|
650
|
+
/^\[ref:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\]$/i.test(rest.trim())
|
|
651
|
+
if (!rest || (options?.flushTrailingRef && trailingRefOnly)) {
|
|
652
|
+
const html = parseMarkdownToHtml(text)
|
|
653
|
+
streamStableCacheKey = text
|
|
654
|
+
streamStableCacheHtml = html
|
|
655
|
+
return html
|
|
656
|
+
}
|
|
657
|
+
let html = ''
|
|
658
|
+
if (stable) {
|
|
659
|
+
if (stable === streamStableCacheKey) html = streamStableCacheHtml
|
|
660
|
+
else {
|
|
661
|
+
html = parseMarkdownToHtml(stable)
|
|
662
|
+
streamStableCacheKey = stable
|
|
663
|
+
streamStableCacheHtml = html
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
return appendPlainTail(html, visiblePlainFromIncomplete(rest))
|
|
667
|
+
}
|