2022-01-01 20:42:25 +01:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import ReactMarkdown from 'react-markdown'
|
|
|
|
import gfm from 'remark-gfm'
|
|
|
|
import remarkBreaks from 'remark-breaks'
|
2022-01-07 21:21:38 +01:00
|
|
|
import remarkMath from 'remark-math'
|
|
|
|
import rehypeKatex from 'rehype-katex'
|
2022-05-12 20:49:20 +02:00
|
|
|
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
|
|
|
|
import { vscDarkPlus } from 'react-syntax-highlighter/dist/cjs/styles/prism'
|
2022-01-07 21:21:38 +01:00
|
|
|
|
|
|
|
import 'katex/dist/katex.min.css'
|
2022-01-01 20:42:25 +01:00
|
|
|
|
|
|
|
import { Emoji, emojiPlugin, isStringWithOnlyOneEmoji } from '../../../../Emoji'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { MessageWithMember } from '../../../../../models/Message'
|
2022-01-01 20:42:25 +01:00
|
|
|
|
|
|
|
export interface MessageContentProps {
|
|
|
|
message: MessageWithMember
|
|
|
|
}
|
|
|
|
|
2022-01-07 21:21:38 +01:00
|
|
|
export const MessageText: React.FC<MessageContentProps> = (props) => {
|
2022-01-01 20:42:25 +01:00
|
|
|
const { message } = props
|
|
|
|
|
|
|
|
const isMessageWithOnlyOneEmoji = useMemo(() => {
|
|
|
|
return isStringWithOnlyOneEmoji(message.value)
|
|
|
|
}, [message.value])
|
|
|
|
|
|
|
|
if (isMessageWithOnlyOneEmoji) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p>
|
2022-08-29 21:34:24 +02:00
|
|
|
<Emoji value={message.value} size={40} tooltip />
|
2022-01-01 20:42:25 +01:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ReactMarkdown
|
|
|
|
disallowedElements={['table']}
|
|
|
|
unwrapDisallowed
|
2022-01-07 21:21:38 +01:00
|
|
|
remarkPlugins={[[gfm], [remarkBreaks], [remarkMath]]}
|
|
|
|
rehypePlugins={[[emojiPlugin], [rehypeKatex]]}
|
2022-01-01 20:42:25 +01:00
|
|
|
linkTarget='_blank'
|
|
|
|
components={{
|
2022-08-23 22:01:43 +02:00
|
|
|
a: (props) => {
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
className='text-green-800 hover:underline dark:text-green-400'
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
},
|
2022-01-01 20:42:25 +01:00
|
|
|
emoji: (props) => {
|
2023-07-22 16:34:23 +02:00
|
|
|
const { value } = props
|
|
|
|
return <Emoji value={value} size={20} tooltip />
|
2022-05-12 20:49:20 +02:00
|
|
|
},
|
|
|
|
code: (properties) => {
|
|
|
|
const { inline, className, children, ...props } = properties
|
|
|
|
const match = /language-(\w+)/.exec(className ?? '')
|
|
|
|
return !(inline as boolean) && match != null ? (
|
|
|
|
<SyntaxHighlighter
|
|
|
|
style={vscDarkPlus as any}
|
|
|
|
language={match[1]}
|
|
|
|
PreTag='div'
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{String(children).replace(/\n$/, '')}
|
|
|
|
</SyntaxHighlighter>
|
|
|
|
) : (
|
|
|
|
<code className={className} {...props}>
|
|
|
|
{children}
|
|
|
|
</code>
|
|
|
|
)
|
2022-01-01 20:42:25 +01:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{message.value}
|
|
|
|
</ReactMarkdown>
|
|
|
|
)
|
|
|
|
}
|