-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathMdRenderer.vue
More file actions
313 lines (298 loc) · 9.63 KB
/
MdRenderer.vue
File metadata and controls
313 lines (298 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<template>
<div>
<!-- 推理过程组件 -->
<ReasoningRander :content="reasoning_content" v-if="reasoning_content?.trim()" />
<template v-for="(item, index) in md_view_list" :key="index">
<div
v-if="item.type === 'question'"
@click="
sendMessage && type !== 'log' ? sendMessage(item.content, 'new') : (content: string) => {}
"
class="problem-button mt-4 mb-4"
:class="sendMessage && type !== 'log' ? 'cursor' : 'disabled'"
>
<el-space :size="8" alignment="flex-start">
<AppIcon iconName="app-edit" class="color-primary" style="margin-top: 3px"></AppIcon>
{{ item.content }}
</el-space>
</div>
<HtmlRander v-else-if="item.type === 'html_rander'" :source="item.content"></HtmlRander>
<EchartsRander
v-else-if="item.type === 'echarts_rander'"
:option="item.content"
></EchartsRander>
<FormRander
:chat_record_id="chat_record_id"
:runtime_node_id="runtime_node_id"
:child_node="child_node"
:disabled="disabled"
:send-message="sendMessage"
v-else-if="item.type === 'form_rander'"
:form_setting="item.content"
></FormRander>
<MdPreview
v-else
ref="editorRef"
editorId="preview-only"
:modelValue="item.content"
:key="index"
class="maxkb-md"
/>
</template>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { config } from 'md-editor-v3'
import HtmlRander from './HtmlRander.vue'
import EchartsRander from './EchartsRander.vue'
import FormRander from './FormRander.vue'
import ReasoningRander from './ReasoningRander.vue'
import { nanoid } from 'nanoid'
config({
markdownItConfig(md) {
md.renderer.rules.image = (tokens, idx, options, env, self) => {
tokens[idx].attrSet('style', 'display:inline-block;min-height:33px;padding:0;margin:0')
if (tokens[idx].content) {
tokens[idx].attrSet('title', tokens[idx].content)
}
tokens[idx].attrSet(
'onerror',
`this.src="./assets/load_error.png";this.onerror=null;this.height="33px"`,
)
return md.renderer.renderToken(tokens, idx, options)
}
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
tokens[idx].attrSet('target', '_blank')
return md.renderer.renderToken(tokens, idx, options)
}
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
document.appendChild
},
})
const props = withDefaults(
defineProps<{
source?: string
reasoning_content?: string
inner_suffix?: boolean
sendMessage?: (question: string, type: 'old' | 'new', other_params_data?: any) => void
child_node?: any
chat_record_id?: string
runtime_node_id?: string
disabled?: boolean
type?: 'log' | 'ai-chat' | 'debug-ai-chat'
}>(),
{
source: '',
disabled: false,
},
)
const editorRef = ref()
const md_view_list = computed(() => {
const temp_source = props.source
return split_form_rander(
split_echarts_rander(split_html_rander(split_quick_question([temp_source]))),
)
})
const split_quick_question = (result: Array<string>) => {
return result
.map((item) => split_quick_question_(item))
.reduce((x: any, y: any) => {
return [...x, ...y]
}, [])
}
const split_quick_question_ = (source: string) => {
const temp_md_quick_question_list = source.match(/<quick_question>[\d\D]*?<\/quick_question>/g)
const md_quick_question_list = temp_md_quick_question_list
? temp_md_quick_question_list.filter((i) => i)
: []
const split_quick_question_value = source
.split(/<quick_question>[\d\D]*?<\/quick_question>/g)
.filter((item) => item !== undefined)
.filter((item) => !md_quick_question_list?.includes(item))
const result = Array.from(
{ length: md_quick_question_list.length + split_quick_question_value.length },
(v, i) => i,
).map((index) => {
if (index % 2 == 0) {
return { type: 'md', content: split_quick_question_value[Math.floor(index / 2)] }
} else {
return {
type: 'question',
content: md_quick_question_list[Math.floor(index / 2)]
.replace('<quick_question>', '')
.replace('</quick_question>', ''),
}
}
})
return result
}
const split_html_rander = (result: Array<any>) => {
return result
.map((item) => split_html_rander_(item.content, item.type))
.reduce((x: any, y: any) => {
return [...x, ...y]
}, [])
}
const split_html_rander_ = (source: string, type: string) => {
const temp_md_quick_question_list = source.match(/<html_rander>[\d\D]*?<\/html_rander>/g)
const md_quick_question_list = temp_md_quick_question_list
? temp_md_quick_question_list.filter((i) => i)
: []
const split_quick_question_value = source
.split(/<html_rander>[\d\D]*?<\/html_rander>/g)
.filter((item) => item !== undefined)
.filter((item) => !md_quick_question_list?.includes(item))
const result = Array.from(
{ length: md_quick_question_list.length + split_quick_question_value.length },
(v, i) => i,
).map((index) => {
if (index % 2 == 0) {
return { type: type, content: split_quick_question_value[Math.floor(index / 2)] }
} else {
return {
type: 'html_rander',
content: md_quick_question_list[Math.floor(index / 2)]
.replace('<html_rander>', '')
.replace('</html_rander>', ''),
}
}
})
return result
}
const split_echarts_rander = (result: Array<any>) => {
return result
.map((item) => split_echarts_rander_(item.content, item.type))
.reduce((x: any, y: any) => {
return [...x, ...y]
}, [])
}
const split_echarts_rander_ = (source: string, type: string) => {
const temp_md_quick_question_list = source.match(/<echarts_rander>[\d\D]*?<\/echarts_rander>/g)
const md_quick_question_list = temp_md_quick_question_list
? temp_md_quick_question_list.filter((i) => i)
: []
const split_quick_question_value = source
.split(/<echarts_rander>[\d\D]*?<\/echarts_rander>/g)
.filter((item) => item !== undefined)
.filter((item) => !md_quick_question_list?.includes(item))
const result = Array.from(
{ length: md_quick_question_list.length + split_quick_question_value.length },
(v, i) => i,
).map((index) => {
if (index % 2 == 0) {
return { type: type, content: split_quick_question_value[Math.floor(index / 2)] }
} else {
return {
type: 'echarts_rander',
content: md_quick_question_list[Math.floor(index / 2)]
.replace('<echarts_rander>', '')
.replace('</echarts_rander>', ''),
}
}
})
return result
}
const split_form_rander = (result: Array<any>) => {
return result
.map((item) => split_form_rander_(item.content, item.type))
.reduce((x: any, y: any) => {
return [...x, ...y]
}, [])
}
function extractFormRanderContent(html: string) {
const results = []
const startTag = '<form_rander>'
const endTag = '</form_rander>'
let startIndex = html.indexOf(startTag)
while (startIndex !== -1) {
let endIndex = html.indexOf(endTag, startIndex)
let depth = 1
let tempIndex = startIndex + startTag.length
// 查找匹配的结束标签
while (depth > 0 && tempIndex < html.length) {
const nextStart = html.indexOf(startTag, tempIndex)
const nextEnd = html.indexOf(endTag, tempIndex)
if (nextStart !== -1 && nextStart < nextEnd) {
depth++
tempIndex = nextStart + startTag.length
} else if (nextEnd !== -1) {
depth--
tempIndex = nextEnd + endTag.length
if (depth === 0) {
endIndex = nextEnd
}
} else {
break
}
}
if (endIndex !== -1) {
// 提取内容(去掉开始和结束标签)
const contentStart = startIndex + startTag.length
const content = html.substring(contentStart, endIndex)
results.push(content)
startIndex = html.indexOf(startTag, endIndex + endTag.length)
} else {
break
}
}
return results
}
const _split_form_rander = (source: string, form_rander_list: Array<string>) => {
const uuid = nanoid()
if (form_rander_list.length > 0) {
form_rander_list.forEach((item) => {
source = source.replace(`<form_rander>${item}</form_rander>`, uuid)
})
}
return source
.split(uuid)
.filter((item) => item !== undefined)
.filter((item) => !form_rander_list?.includes(item))
}
const split_form_rander_ = (source: string, type: string) => {
const temp_md_quick_question_list = extractFormRanderContent(source)
const md_quick_question_list = temp_md_quick_question_list
? temp_md_quick_question_list.filter((i) => i)
: []
const split_quick_question_value = _split_form_rander(source, md_quick_question_list)
const result = Array.from(
{ length: md_quick_question_list.length + split_quick_question_value.length },
(v, i) => i,
).map((index) => {
if (index % 2 == 0) {
return { type: type, content: split_quick_question_value[Math.floor(index / 2)] }
} else {
return {
type: 'form_rander',
content: md_quick_question_list[Math.floor(index / 2)],
}
}
})
return result
}
</script>
<style lang="scss" scoped>
.problem-button {
width: 100%;
border: none;
border-radius: 8px;
background: var(--app-layout-bg-color);
padding: 12px;
box-sizing: border-box;
color: var(--el-text-color-regular);
word-break: break-all;
&:hover {
background: var(--el-color-primary-light-9);
}
&.disabled {
&:hover {
background: var(--app-layout-bg-color);
}
}
// :deep(.el-icon) {
// color: var(--el-color-primary);
// margin-top: 3px;
// }
}
</style>