2021-06-23 05:37:08 -04:00
|
|
|
import { useRef, useEffect } from 'react'
|
2020-10-12 06:25:59 -04:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import Linkify from 'react-linkify'
|
|
|
|
|
|
|
|
function MessageContent({ content }) {
|
|
|
|
const root = useRef(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-01-27 05:11:25 -05:00
|
|
|
if (!(window.MathJax && window.MathJax.Hub)) {
|
|
|
|
return
|
|
|
|
}
|
2020-10-12 06:25:59 -04:00
|
|
|
const MJHub = window.MathJax.Hub
|
2021-05-17 05:37:58 -04:00
|
|
|
const inlineMathConfig =
|
|
|
|
(MJHub.config &&
|
|
|
|
MJHub.config.tex2jax &&
|
|
|
|
MJHub.config.tex2jax.inlineMath) ||
|
|
|
|
[]
|
2020-10-12 06:25:59 -04:00
|
|
|
const alreadyConfigured = inlineMathConfig.some(
|
|
|
|
c => c[0] === '$' && c[1] === '$'
|
|
|
|
)
|
|
|
|
if (!alreadyConfigured) {
|
|
|
|
MJHub.Config({
|
|
|
|
tex2jax: {
|
2021-04-27 03:52:58 -04:00
|
|
|
inlineMath: inlineMathConfig.concat([['$', '$']]),
|
|
|
|
},
|
2020-10-12 06:25:59 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2020-12-15 05:23:54 -05: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 09:05:04 -04:00
|
|
|
for (const a of root.current.getElementsByTagName('a')) {
|
2020-12-15 05:23:54 -05:00
|
|
|
a.setAttribute('target', '_blank')
|
|
|
|
a.setAttribute('rel', 'noreferrer noopener')
|
|
|
|
}
|
2020-10-12 06:25:59 -04:00
|
|
|
|
2020-12-15 05:23:54 -05: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 06:25:59 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<p ref={root}>
|
|
|
|
<Linkify>{content}</Linkify>
|
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageContent.propTypes = {
|
2021-04-27 03:52:58 -04:00
|
|
|
content: PropTypes.string.isRequired,
|
2020-10-12 06:25:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default MessageContent
|