2023-10-23 23:33:39 +02:00
|
|
|
import { useMemo } from "react"
|
|
|
|
import ReactMarkdown from "react-markdown"
|
|
|
|
import gfm from "remark-gfm"
|
|
|
|
import remarkBreaks from "remark-breaks"
|
|
|
|
import remarkMath from "remark-math"
|
|
|
|
import rehypeKatex from "rehype-katex"
|
|
|
|
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
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import "katex/dist/katex.min.css"
|
2022-01-01 20:42:25 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { Emoji, emojiPlugin, isStringWithOnlyOneEmoji } from "../../../../Emoji"
|
|
|
|
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
|
2023-10-23 23:33:39 +02:00
|
|
|
disallowedElements={["table"]}
|
2022-01-01 20:42:25 +01:00
|
|
|
unwrapDisallowed
|
2022-01-07 21:21:38 +01:00
|
|
|
remarkPlugins={[[gfm], [remarkBreaks], [remarkMath]]}
|
|
|
|
rehypePlugins={[[emojiPlugin], [rehypeKatex]]}
|
2023-10-23 23:33:39 +02:00
|
|
|
linkTarget="_blank"
|
2022-01-01 20:42:25 +01:00
|
|
|
components={{
|
2022-08-23 22:01:43 +02:00
|
|
|
a: (props) => {
|
|
|
|
return (
|
|
|
|
<a
|
2023-10-23 23:33:39 +02:00
|
|
|
className="text-green-800 hover:underline dark:text-green-400"
|
2022-08-23 22:01:43 +02:00
|
|
|
{...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
|
2023-10-23 23:33:39 +02:00
|
|
|
const match = /language-(\w+)/.exec(className ?? "")
|
2022-05-12 20:49:20 +02:00
|
|
|
return !(inline as boolean) && match != null ? (
|
|
|
|
<SyntaxHighlighter
|
|
|
|
style={vscDarkPlus as any}
|
|
|
|
language={match[1]}
|
2023-10-23 23:33:39 +02:00
|
|
|
PreTag="div"
|
2022-05-12 20:49:20 +02:00
|
|
|
{...props}
|
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
{String(children).replace(/\n$/, "")}
|
2022-05-12 20:49:20 +02:00
|
|
|
</SyntaxHighlighter>
|
|
|
|
) : (
|
|
|
|
<code className={className} {...props}>
|
|
|
|
{children}
|
|
|
|
</code>
|
|
|
|
)
|
2023-10-23 23:33:39 +02:00
|
|
|
},
|
2022-01-01 20:42:25 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{message.value}
|
|
|
|
</ReactMarkdown>
|
|
|
|
)
|
|
|
|
}
|