mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-08-11 18:28:48 +00:00
feat(message-box): add syntax highlighted code
This commit is contained in:
@ -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,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
30
src/components/SyntaxHighlightedCode.tsx
Normal file
30
src/components/SyntaxHighlightedCode.tsx
Normal 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>
|
||||
);
|
||||
};
|
Reference in New Issue
Block a user