2021-06-23 09:37:08 +00:00
|
|
|
import { useRef, useEffect } from 'react'
|
2020-10-12 10:25:59 +00:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import Linkify from 'react-linkify'
|
|
|
|
|
|
|
|
function MessageContent({ content }) {
|
|
|
|
const root = useRef(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-01-27 10:11:25 +00:00
|
|
|
if (!(window.MathJax && window.MathJax.Hub)) {
|
|
|
|
return
|
|
|
|
}
|
2020-10-12 10:25:59 +00:00
|
|
|
const MJHub = window.MathJax.Hub
|
2021-05-17 09:37:58 +00:00
|
|
|
const inlineMathConfig =
|
|
|
|
(MJHub.config &&
|
|
|
|
MJHub.config.tex2jax &&
|
|
|
|
MJHub.config.tex2jax.inlineMath) ||
|
|
|
|
[]
|
2020-10-12 10:25:59 +00:00
|
|
|
const alreadyConfigured = inlineMathConfig.some(
|
|
|
|
c => c[0] === '$' && c[1] === '$'
|
|
|
|
)
|
|
|
|
if (!alreadyConfigured) {
|
|
|
|
MJHub.Config({
|
|
|
|
tex2jax: {
|
2021-04-27 07:52:58 +00:00
|
|
|
inlineMath: inlineMathConfig.concat([['$', '$']]),
|
|
|
|
},
|
2020-10-12 10:25:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2020-12-15 10:23:54 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// adds attributes to all the links generated by <Linkify/>, required due to https://github.com/tasti/react-linkify/issues/99
|
2021-05-05 13:05:04 +00:00
|
|
|
for (const a of root.current.getElementsByTagName('a')) {
|
2020-12-15 10:23:54 +00:00
|
|
|
a.setAttribute('target', '_blank')
|
|
|
|
a.setAttribute('rel', 'noreferrer noopener')
|
|
|
|
}
|
2020-10-12 10:25:59 +00:00
|
|
|
|
2020-12-15 10:23:54 +00:00
|
|
|
// MathJax typesetting
|
|
|
|
const MJHub = window.MathJax.Hub
|
|
|
|
const timeoutHandler = setTimeout(() => {
|
|
|
|
MJHub.Queue(['Typeset', MJHub, root.current])
|
|
|
|
}, 0)
|
|
|
|
return () => clearTimeout(timeoutHandler)
|
|
|
|
}, [content])
|
2020-10-12 10:25:59 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<p ref={root}>
|
|
|
|
<Linkify>{content}</Linkify>
|
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageContent.propTypes = {
|
2021-04-27 07:52:58 +00:00
|
|
|
content: PropTypes.string.isRequired,
|
2020-10-12 10:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default MessageContent
|