11import { FormEvent , useContext , useEffect , useMemo , useState } from 'react'
22import ReactMarkdown from 'react-markdown'
3+ import { Components } from 'react-markdown' ;
34import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
45import { nord } from 'react-syntax-highlighter/dist/esm/styles/prism'
56import { Checkbox , DefaultButton , Dialog , FontIcon , Stack , Text } from '@fluentui/react'
@@ -225,19 +226,38 @@ export const Answer = ({ answer, onCitationClicked }: Props) => {
225226 )
226227 }
227228
228- const components = {
229- code ( { node, ...props } : { node : any ; [ key : string ] : any } ) {
230- let language
231- if ( props . className ) {
232- const match = props . className . match ( / l a n g u a g e - ( \w + ) / )
233- language = match ? match [ 1 ] : undefined
229+ const components : Components = {
230+ a : ( { href, children, ...props } ) => (
231+ < a href = { href } target = "_blank" rel = "noopener noreferrer" { ...props } >
232+ { children }
233+ </ a >
234+ ) ,
235+ code ( { inline, className, children, ...props } : {
236+ inline ?: boolean ;
237+ className ?: string ;
238+ children ?: React . ReactNode ;
239+ [ key : string ] : any
240+ } ) {
241+ const match = / l a n g u a g e - ( \w + ) / . exec ( className || '' ) ;
242+ // Handle inline and block code rendering
243+ if ( inline ) {
244+ return (
245+ < code className = { className } { ...props } >
246+ { children }
247+ </ code >
248+ ) ;
249+ } else if ( match ) {
250+ return (
251+ < SyntaxHighlighter
252+ style = { nord }
253+ language = { match [ 1 ] }
254+ PreTag = "div"
255+ { ...props }
256+ >
257+ { String ( children ) . replace ( / \n $ / , '' ) }
258+ </ SyntaxHighlighter >
259+ ) ;
234260 }
235- const codeString = node . children [ 0 ] . value ?? ''
236- return (
237- < SyntaxHighlighter style = { nord } language = { language } PreTag = "div" { ...props } >
238- { codeString }
239- </ SyntaxHighlighter >
240- )
241261 }
242262 }
243263 return (
@@ -247,7 +267,6 @@ export const Answer = ({ answer, onCitationClicked }: Props) => {
247267 < Stack horizontal grow >
248268 < Stack . Item grow >
249269 < ReactMarkdown
250- linkTarget = "_blank"
251270 remarkPlugins = { [ remarkGfm , supersub ] }
252271 children = {
253272 SANITIZE_ANSWER
0 commit comments