overleaf/services/web/frontend/js/features/chat/components/message-content.js
Alasdair Smith f1b42a3d0d Merge pull request #3192 from overleaf/msm-chat-react
Chat reactification

GitOrigin-RevId: ee1268b412513a8656703257febad4975adb74e7
2020-10-13 02:04:15 +00:00

52 lines
1.3 KiB
JavaScript

import React, { useRef, useEffect } from 'react'
import PropTypes from 'prop-types'
import Linkify from 'react-linkify'
function MessageContent({ content }) {
const root = useRef(null)
useEffect(() => {
const MJHub = window.MathJax.Hub
const inlineMathConfig = MJHub.config && MJHub.config.tex2jax.inlineMath
const alreadyConfigured = inlineMathConfig.some(
c => c[0] === '$' && c[1] === '$'
)
if (!alreadyConfigured) {
MJHub.Config({
tex2jax: {
inlineMath: inlineMathConfig.concat([['$', '$']])
}
})
}
}, [])
useEffect(
() => {
// adds attributes to all the links generated by <Linkify/>, required due to https://github.com/tasti/react-linkify/issues/99
root.current.getElementsByTagName('a').forEach(a => {
a.setAttribute('target', '_blank')
a.setAttribute('rel', 'noreferrer noopener')
})
// MathJax typesetting
const MJHub = window.MathJax.Hub
const timeoutHandler = setTimeout(() => {
MJHub.Queue(['Typeset', MJHub, root.current])
}, 0)
return () => clearTimeout(timeoutHandler)
},
[content]
)
return (
<p ref={root}>
<Linkify>{content}</Linkify>
</p>
)
}
MessageContent.propTypes = {
content: PropTypes.string.isRequired
}
export default MessageContent