refactor: 语音播放时将内容处理成纯文本

v3.2
CaptainB 2024-09-19 17:18:53 +08:00 committed by 刘瑞斌
parent f2dae7b3e6
commit a1ae3283ec
2 changed files with 26 additions and 1 deletions

View File

@ -118,7 +118,32 @@ function voteHandle(val: string) {
})
}
function markdownToPlainText(md: string) {
return md
// 移除图片 ![alt](url)
.replace(/!\[.*?\]\(.*?\)/g, '')
// 移除链接 [text](url)
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
// 移除 Markdown 标题符号 (#, ##, ###)
.replace(/^#{1,6}\s+/gm, '')
// 移除加粗 **text** 或 __text__
.replace(/\*\*(.*?)\*\*/g, '$1')
.replace(/__(.*?)__/g, '$1')
// 移除斜体 *text* 或 _text_
.replace(/\*(.*?)\*/g, '$1')
.replace(/_(.*?)_/g, '$1')
// 移除行内代码 `code`
.replace(/`(.*?)`/g, '$1')
// 移除代码块 ```code```
.replace(/```[\s\S]*?```/g, '')
// 移除多余的换行符
.replace(/\n{2,}/g, '\n')
.trim();
}
const playAnswerText = (text: string) => {
// text 处理成纯文本
text = markdownToPlainText(text)
if (props.tts_type === 'BROWSER') {
// 创建一个新的 SpeechSynthesisUtterance 实例
const utterance = new SpeechSynthesisUtterance(text)

View File

@ -357,7 +357,7 @@ function handleInputFieldList() {
const record = chatList.value[chatList.value.length - 1]
let default_value: any = {}
if (record) {
record.execution_details[0].global_fields.reduce((pre: any, next: any) => {
record.execution_details[0].global_fields?.reduce((pre: any, next: any) => {
pre[next.key] = next.value
return pre
}, default_value)