2023-01-23 06:41:24 -05:00
|
|
|
import { useRef, useEffect, type FC } from 'react'
|
|
|
|
// @ts-ignore
|
|
|
|
import Linkify from 'react-linkify'
|
|
|
|
import useIsMounted from '../../../shared/hooks/use-is-mounted'
|
|
|
|
import { configureMathJax } from '../../mathjax/configure'
|
2023-01-25 05:13:29 -05:00
|
|
|
import { loadMathJax } from '../../mathjax/load-mathjax'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2023-01-23 06:41:24 -05:00
|
|
|
|
|
|
|
const MessageContent: FC<{ content: string }> = ({ content }) => {
|
|
|
|
const root = useRef<HTMLDivElement | null>(null)
|
|
|
|
const mounted = useIsMounted()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (root.current) {
|
|
|
|
// adds attributes to all the links generated by <Linkify/>, required due to https://github.com/tasti/react-linkify/issues/99
|
|
|
|
for (const a of root.current.getElementsByTagName('a')) {
|
|
|
|
a.setAttribute('target', '_blank')
|
|
|
|
a.setAttribute('rel', 'noreferrer noopener')
|
|
|
|
}
|
|
|
|
|
|
|
|
// MathJax v2 typesetting
|
|
|
|
if (window.MathJax?.Hub) {
|
2023-01-25 05:13:05 -05:00
|
|
|
const { Hub } = window.MathJax
|
|
|
|
|
2023-01-23 06:41:24 -05:00
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
configureMathJax()
|
2023-01-25 05:13:05 -05:00
|
|
|
Hub.Queue(['Typeset', Hub, root.current])
|
2023-01-23 06:41:24 -05:00
|
|
|
}, 0)
|
|
|
|
|
|
|
|
return () => clearTimeout(timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MathJax v3 typesetting
|
|
|
|
loadMathJax()
|
|
|
|
.then(MathJax => {
|
|
|
|
if (mounted.current) {
|
2023-09-27 05:45:49 -04:00
|
|
|
MathJax.typesetPromise([root.current]).catch(debugConsole.error)
|
2023-01-23 06:41:24 -05:00
|
|
|
}
|
|
|
|
})
|
2023-09-27 05:45:49 -04:00
|
|
|
.catch(debugConsole.error)
|
2023-01-23 06:41:24 -05:00
|
|
|
}
|
|
|
|
}, [content, mounted])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<p ref={root}>
|
|
|
|
<Linkify>{content}</Linkify>
|
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MessageContent
|