zhuliu před 7 měsíci
rodič
revize
46068f8318
1 změnil soubory, kde provedl 31 přidání a 26 odebrání
  1. 31 26
      src/utils/model/AIChat/AIChatBox.vue

+ 31 - 26
src/utils/model/AIChat/AIChatBox.vue

@@ -467,12 +467,12 @@ export default {
       
       // 使用正则表达式匹配代码块
       let regex = /```(\w*)\n([\s\S]+?)```/g;
-      // regex = /```(\w*)\n|([\s\S]+?)```/g
       let lastIndex = 0;
       let match;
+      // console.log(regex.exec(text))
       while ((match = regex.exec(text)) !== null) {
+        this.startCode = false
         // 添加代码块之前的文本
-        console.log(match.index,match)
         if (match.index > lastIndex) {
           blocks.push({
             type: 'text',
@@ -494,32 +494,37 @@ export default {
       
       // 添加剩余的文本
       if (lastIndex < text.length) {
+        let lastContent = text.slice(lastIndex)
+        let regex2 = /```(\w*)\n([\s\S]+)/g;
+        let lastResult
+        let index = 0
+        while((lastResult = regex2.exec(lastContent)) !== null){
+          this.startCode = true
+          if(lastResult.index > index){
+            blocks.push({
+              type: 'text',
+              content: lastContent.slice(index, lastResult.index),
+              isComplete: false
+            });
+          }
+          blocks.push({
+            type: 'code',
+            language: lastResult[1] || '',
+            content: lastResult[2].trim(),
+            isComplete: true  // 代码块总是完整显示
+          });
+          
+          index = regex2.lastIndex;
+        }
+        lastIndex = lastIndex + index
+      }
+      if(lastIndex < text.length && !this.startCode){
         blocks.push({
-          type: 'text',
-          content: text.slice(lastIndex),
-          isComplete: false
-        });
+            type: 'text',
+            content: text.slice(lastIndex),
+            isComplete: false
+          });
       }
-      // let currentIndex = text.length;
-      // let lastIndex = 0
-      // var startCode = text.slice(currentIndex).startsWith('```')
-      // if(!this.startCode && startCode){
-      //   this.startCode = true
-      //   blocks.push({
-      //     type: 'code',
-      //     content: text.slice(currentIndex),
-      //     isComplete: false
-      //   });
-      // }else if(this.startCode && startCode){
-      //   this.startCode = false
-      // }else{
-      //   blocks.push({
-      //     type: 'text',
-      //     content: text.slice(currentIndex),
-      //     isComplete: false
-      //   });
-      // }
-      
       return blocks;
     },
     renderMarkdown(text) {