diff --git a/src/components/MessageBox.tsx b/src/components/MessageBox.tsx index dc43aa2..d567aa8 100644 --- a/src/components/MessageBox.tsx +++ b/src/components/MessageBox.tsx @@ -20,6 +20,8 @@ import SearchImages from './SearchImages'; import SearchVideos from './SearchVideos'; import { useSpeech } from 'react-text-to-speech'; import ThinkBox from './ThinkBox'; +import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; +import { SyntaxHighlightedCode } from './SyntaxHighlightedCode'; const ThinkTagProcessor = ({ children, @@ -136,6 +138,9 @@ const MessageBox = ({ thinkingEnded: thinkingEnded, }, }, + code: { + component: SyntaxHighlightedCode, + }, }, }; diff --git a/src/components/SyntaxHighlightedCode.tsx b/src/components/SyntaxHighlightedCode.tsx new file mode 100644 index 0000000..2615786 --- /dev/null +++ b/src/components/SyntaxHighlightedCode.tsx @@ -0,0 +1,30 @@ +import React, { HTMLProps } from 'react'; +import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; +import { + coldarkDark, + coldarkCold, +} from 'react-syntax-highlighter/dist/esm/styles/prism'; + +export const SyntaxHighlightedCode = (props: HTMLProps) => { + const isDarkTheme = document.documentElement.classList.contains('dark'); + const language = props.className?.match(/lang-([a-zA-Z0-9_-]+)/)![1]; + + return language ? ( +
+ + {props.children as string} + +
+ ) : ( + + {props.children} + + ); +};