({
className,
content,
appendTooltipTo,
openLinksInNewTab = false,
}: MarkdownProps)
| 59 | } |
| 60 | |
| 61 | export default function Markdown({ |
| 62 | className, |
| 63 | content, |
| 64 | appendTooltipTo, |
| 65 | openLinksInNewTab = false, |
| 66 | }: MarkdownProps): ReactElement { |
| 67 | const purify = useDomPurify(); |
| 68 | const { openModal } = useLazyModal(); |
| 69 | const { isCompanion } = useRequestProtocol(); |
| 70 | const containerRef = useRef<HTMLDivElement>(null); |
| 71 | const [userId, setUserId] = useState(''); |
| 72 | const [offset, setOffset] = useState<CaretOffset>([0, 0]); |
| 73 | const [clearUser, cancelUserClearing] = useDebounceFn( |
| 74 | () => setUserId(''), |
| 75 | 200, |
| 76 | ); |
| 77 | const { data } = useQuery({ |
| 78 | queryKey: generateQueryKey(RequestKey.UserShortById, { id: userId }), |
| 79 | queryFn: () => { |
| 80 | return getUserShortInfo(userId); |
| 81 | }, |
| 82 | enabled: !!userId, |
| 83 | }); |
| 84 | |
| 85 | // Add accessibility attributes to images after render |
| 86 | useEffect(() => { |
| 87 | const container = containerRef.current; |
| 88 | if (!container) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | const images = container.querySelectorAll('img[src]'); |
| 93 | images.forEach((img) => { |
| 94 | img.setAttribute('tabindex', '0'); |
| 95 | img.setAttribute('role', 'button'); |
| 96 | img.setAttribute('aria-label', 'Open image'); |
| 97 | }); |
| 98 | }); |
| 99 | |
| 100 | useEffect(() => { |
| 101 | if (!openLinksInNewTab) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | const container = containerRef.current; |
| 106 | if (!container) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | const links = container.querySelectorAll('a[href]'); |
| 111 | links.forEach((link) => { |
| 112 | link.setAttribute('target', '_blank'); |
| 113 | link.setAttribute('rel', 'noopener noreferrer'); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | const onHoverHandler: MouseEventHandler<HTMLDivElement> = useCallback( |
| 118 | (e) => { |
nothing calls this directly
no test coverage detected