11import useSettingStore from "@/stores/setting" ;
22import React , { useEffect , useRef , useState } from 'react' ;
33import { useTheme } from 'next-themes'
4- import useChatStore from '@/stores/chat' ;
5- import { debounce } from 'lodash-es'
64import MarkdownIt from 'markdown-it' ;
75import hljs from 'highlight.js/lib/core' ;
86import 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