({
className,
content,
appendTooltipTo,
}: MarkdownProps)
| 104 | } |
| 105 | |
| 106 | export default function Markdown({ |
| 107 | className, |
| 108 | content, |
| 109 | appendTooltipTo, |
| 110 | }: MarkdownProps): ReactElement { |
| 111 | const purify = useDomPurify(); |
| 112 | const openImage = useImageViewer(); |
| 113 | const containerRef = useRef<HTMLDivElement>(null); |
| 114 | const [userId, setUserId] = useState(''); |
| 115 | const [offset, setOffset] = useState<CaretOffset>([0, 0]); |
| 116 | const [clearUser, cancelUserClearing] = useDebounceFn( |
| 117 | () => setUserId(''), |
| 118 | 200, |
| 119 | ); |
| 120 | const { data } = useQuery({ |
| 121 | queryKey: generateQueryKey(RequestKey.UserShortById, { id: userId }), |
| 122 | queryFn: () => { |
| 123 | return getUserShortInfo(userId); |
| 124 | }, |
| 125 | enabled: !!userId, |
| 126 | }); |
| 127 | |
| 128 | // Add accessibility attributes to images and keep code and mentions untranslated |
| 129 | useEffect(() => { |
| 130 | const container = containerRef.current; |
| 131 | if (!container) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | const images = container.querySelectorAll<HTMLImageElement>('img[src]'); |
| 136 | images.forEach((img) => { |
| 137 | const anchor = getWrappingAnchor(img, container); |
| 138 | const opensImage = shouldOpenAnchorImage(anchor, img.src); |
| 139 | img.setAttribute('data-image-action', opensImage ? 'view' : 'link'); |
| 140 | if (!opensImage) { |
| 141 | img.removeAttribute('tabindex'); |
| 142 | img.removeAttribute('role'); |
| 143 | img.removeAttribute('aria-label'); |
| 144 | return; |
| 145 | } |
| 146 | img.setAttribute('tabindex', '0'); |
| 147 | img.setAttribute('role', 'button'); |
| 148 | img.setAttribute('aria-label', 'Open image'); |
| 149 | }); |
| 150 | |
| 151 | container |
| 152 | .querySelectorAll('pre, code, a[data-mention-id]') |
| 153 | .forEach((element) => element.setAttribute('translate', 'no')); |
| 154 | }); |
| 155 | |
| 156 | const onHoverHandler: MouseEventHandler<HTMLDivElement> = useCallback( |
| 157 | (e) => { |
| 158 | const element = e.target; |
| 159 | |
| 160 | if (!isMentionLink(element)) { |
| 161 | if (userId) { |
| 162 | clearUser(); |
| 163 | } |
nothing calls this directly
no test coverage detected