Skip to content

Commit 9c6a48a

Browse files
committed
fix: AI 对话中的链接未正确的使用外部浏览器打开
1 parent 7f0a2a6 commit 9c6a48a

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

src/app/core/record/chat/chat-input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ ${ragContext}
265265
<footer className="relative flex flex-col border rounded-xl p-2 gap-2 mb-2 lg:w-[calc(100%-1rem)] w-full">
266266
<div className="relative w-full flex items-start">
267267
<Textarea
268-
className="flex-1 p-2 relative border-none text-xs placeholder:text-xs lg:text-sm focus-visible:ring-0 shadow-none min-h-[36px] max-h-[240px] resize-none overflow-y-auto"
268+
className="flex-1 p-2 relative border-none text-xs placeholder:text-xs lg:placeholder:text-sm lg:text-sm focus-visible:ring-0 shadow-none min-h-[36px] max-h-[240px] resize-none overflow-y-auto"
269269
rows={1}
270270
disabled={!apiKey || loading}
271271
value={text}

src/app/core/record/chat/chat-preview.tsx

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import useSettingStore from "@/stores/setting";
22
import React, { useEffect, useRef, useState } from 'react';
33
import { useTheme } from 'next-themes'
4-
import useChatStore from '@/stores/chat';
5-
import { debounce } from 'lodash-es'
64
import MarkdownIt from 'markdown-it';
75
import hljs from 'highlight.js/lib/core';
86
import javascript from 'highlight.js/lib/languages/javascript';
@@ -31,7 +29,6 @@ export default function ChatPreview({text}: {text: string, themeReverse?: boolea
3129
const { theme } = useTheme()
3230
const [mdTheme, setMdTheme] = useState<ThemeType>('light')
3331
const { codeTheme } = useSettingStore()
34-
const { chats } = useChatStore()
3532
const [htmlContent, setHtmlContent] = useState<string>('');
3633

3734
const md = useRef<MarkdownIt | null>(null);
@@ -57,6 +54,13 @@ export default function ChatPreview({text}: {text: string, themeReverse?: boolea
5754
'</code></pre>';
5855
}
5956
});
57+
58+
md.current.renderer.rules.link_open = function (tokens, idx, options, env, self) {
59+
tokens[idx].attrSet('target', '_blank');
60+
tokens[idx].attrSet('rel', 'noopener noreferrer');
61+
return self.renderToken(tokens, idx, options);
62+
}
63+
6064

6165
// Re-render content when instance is updated
6266
if (text) {
@@ -73,24 +77,6 @@ export default function ChatPreview({text}: {text: string, themeReverse?: boolea
7377
}
7478
}, [text]);
7579

76-
function bindPreviewLink() {
77-
setTimeout(() => {
78-
const previewContent = previewRef.current;
79-
if (!previewContent) return;
80-
81-
previewContent.querySelectorAll('a').forEach(item => {
82-
item.setAttribute('target', '_blank')
83-
item.setAttribute('rel', 'noopener noreferrer')
84-
});
85-
}, 100);
86-
}
87-
88-
const bindPreviewLinkDebounce = debounce(bindPreviewLink, 1000)
89-
90-
useEffect(() => {
91-
bindPreviewLinkDebounce()
92-
}, [chats, htmlContent])
93-
9480
useEffect(() => {
9581
if (theme === 'system') {
9682
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {

0 commit comments

Comments
 (0)