message-content.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { useRef, useEffect, type FC } from 'react'
  2. import Linkify from 'react-linkify'
  3. import useIsMounted from '../../../shared/hooks/use-is-mounted'
  4. import { loadMathJax } from '../../mathjax/load-mathjax'
  5. import { debugConsole } from '@/utils/debugging'
  6. const MessageContent: FC<{ content: string }> = ({ content }) => {
  7. const root = useRef<HTMLDivElement | null>(null)
  8. const mounted = useIsMounted()
  9. useEffect(() => {
  10. if (root.current) {
  11. // adds attributes to all the links generated by <Linkify/>, required due to https://github.com/tasti/react-linkify/issues/99
  12. for (const a of root.current.getElementsByTagName('a')) {
  13. a.setAttribute('target', '_blank')
  14. a.setAttribute('rel', 'noreferrer noopener')
  15. }
  16. // MathJax v3 typesetting
  17. loadMathJax()
  18. .then(MathJax => {
  19. if (mounted.current) {
  20. MathJax.typesetPromise([root.current]).catch(debugConsole.error)
  21. }
  22. })
  23. .catch(debugConsole.error)
  24. }
  25. }, [content, mounted])
  26. return (
  27. <p ref={root}>
  28. <Linkify>{content}</Linkify>
  29. </p>
  30. )
  31. }
  32. export default MessageContent