MCPcopy Create free account
hub / github.com/dailydotdev/apps / Markdown

Function Markdown

packages/shared/src/components/Markdown.tsx:106–279  ·  view source on GitHub ↗
({
  className,
  content,
  appendTooltipTo,
}: MarkdownProps)

Source from the content-addressed store, hash-verified

104}
105
106export 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 }

Callers

nothing calls this directly

Calls 12

useDomPurifyFunction · 0.90
useImageViewerFunction · 0.90
generateQueryKeyFunction · 0.90
getUserShortInfoFunction · 0.90
isImageUrlFunction · 0.90
useDebounceFnFunction · 0.85
getWrappingAnchorFunction · 0.85
shouldOpenAnchorImageFunction · 0.85
isMentionLinkFunction · 0.85
getTooltipOffsetFunction · 0.85
getTargetElementFunction · 0.85
isImageElementFunction · 0.85

Tested by

no test coverage detected