message-content.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(async MathJax => {
  19. if (mounted.current) {
  20. const element = root.current
  21. try {
  22. await MathJax.typesetPromise([element])
  23. MathJax.typesetClear([element])
  24. } catch (error) {
  25. debugConsole.error(error)
  26. }
  27. }
  28. })
  29. .catch(debugConsole.error)
  30. }
  31. }, [content, mounted])
  32. return (
  33. <p ref={root} translate="no">
  34. <Linkify>{content}</Linkify>
  35. </p>
  36. )
  37. }
  38. export default MessageContent