feat(message-box): add syntax highlighted code

This commit is contained in:
ItzCrazyKns
2025-08-05 19:11:09 +05:30
parent b1b87d3b52
commit ebfb103911
2 changed files with 35 additions and 0 deletions

View File

@ -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,
},
},
};

View File

@ -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<HTMLDivElement>) => {
const isDarkTheme = document.documentElement.classList.contains('dark');
const language = props.className?.match(/lang-([a-zA-Z0-9_-]+)/)![1];
return language ? (
<div className="not-prose">
<SyntaxHighlighter
customStyle={{
margin: 0,
backgroundColor: isDarkTheme ? '#111111' : '#f3f3ee',
}}
language={language}
style={isDarkTheme ? coldarkDark : coldarkCold}
>
{props.children as string}
</SyntaxHighlighter>
</div>
) : (
<code className="inline bg-light-100 dark:bg-dark-100 px-2 py-1 rounded-lg text-sm not-prose">
{props.children}
</code>
);
};