|
1 | 1 | import { FC, memo } from 'react'; |
2 | 2 | import ReactMarkdown, { Options } from 'react-markdown'; |
3 | 3 |
|
| 4 | +const ProcessContent = ({ children }: { children: string }) => { |
| 5 | + const parts = []; |
| 6 | + let currentIndex = 0; |
| 7 | + // First check if there's a lone </think> tag |
| 8 | + const endThinkMatch = children.match(/<\/think>/); |
| 9 | + if (endThinkMatch && !children.slice(0, endThinkMatch.index).includes('<think>')) { |
| 10 | + // If we find a lone </think>, treat all content before it as think content |
| 11 | + console.log(endThinkMatch) |
| 12 | + console.log(endThinkMatch.input) |
| 13 | + if(children.slice(0, endThinkMatch.index) && children.slice(0, endThinkMatch.index).trim() !== "") { |
| 14 | + if (endThinkMatch.index! > 0) { |
| 15 | + parts.push( |
| 16 | + <div key="think-start" className="bg-gray-200 dark:bg-gray-600 p-4 my-2 rounded-lg border-l-4 border-gray-500 dark:border-gray-200"> |
| 17 | + <ReactMarkdown>{children.slice(0, endThinkMatch.index)}</ReactMarkdown> |
| 18 | + </div> |
| 19 | + ); |
| 20 | + } |
| 21 | + } |
| 22 | + // Add remaining content after </think> |
| 23 | + if (endThinkMatch.index! + 8 < children.length) { |
| 24 | + parts.push( |
| 25 | + <ReactMarkdown key="after-think"> |
| 26 | + {children.slice(endThinkMatch.index! + 8)} |
| 27 | + </ReactMarkdown> |
| 28 | + ); |
| 29 | + } |
| 30 | + } else { |
| 31 | + // Regular processing for properly paired think tags |
| 32 | + const regex = /<think>([\s\S]*?)<\/think>/g; |
| 33 | + let match; |
| 34 | + console.log(children) |
| 35 | + while ((match = regex.exec(children)) !== null) { |
| 36 | + // Add content before the think tag |
| 37 | + if (match.index > currentIndex) { |
| 38 | + parts.push( |
| 39 | + <ReactMarkdown key={`text-${currentIndex}`}> |
| 40 | + {children.slice(currentIndex, match.index)} |
| 41 | + </ReactMarkdown> |
| 42 | + ); |
| 43 | + } |
| 44 | + if(match[1].trim() !== "") { |
| 45 | + parts.push( |
| 46 | + <div key={`think-${match.index}`} |
| 47 | + className="bg-gray-200 dark:bg-gray-600 p-4 my-2 rounded-lg border-l-4 border-gray-500 dark:border-gray-200"> |
| 48 | + <ReactMarkdown>{match[1]}</ReactMarkdown> |
| 49 | + </div> |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + currentIndex = match.index + match[0].length; |
| 54 | + } |
| 55 | + |
| 56 | + // Add any remaining content |
| 57 | + if (currentIndex < children.length) { |
| 58 | + parts.push( |
| 59 | + <ReactMarkdown key={`text-${currentIndex}`}> |
| 60 | + {children.slice(currentIndex)} |
| 61 | + </ReactMarkdown> |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return <div>{parts}</div>; |
| 67 | +}; |
| 68 | + |
4 | 69 | export const MemoizedReactMarkdown: FC<Options> = memo( |
5 | | - ReactMarkdown, |
6 | | - (prevProps, nextProps) => ( |
7 | | - prevProps.children === nextProps.children |
8 | | - ) |
| 70 | + ({ children, ...props }) => ( |
| 71 | + <ProcessContent {...props}> |
| 72 | + {typeof children === 'string' ? children : ''} |
| 73 | + </ProcessContent> |
| 74 | + ), |
| 75 | + (prevProps, nextProps) => prevProps.children === nextProps.children |
9 | 76 | ); |
0 commit comments