Skip to content

Commit

Permalink
refactor: 语音播放时将内容处理成纯文本
Browse files Browse the repository at this point in the history
  • Loading branch information
liuruibin committed Sep 19, 2024
1 parent 204676f commit a4bce9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions ui/src/components/ai-chat/OperationButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/ai-chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a4bce9b

Please sign in to comment.