Skip to content

Commit 6d6472c

Browse files
fix(web): forward sanitized image attributes to the rendered image
The direct-image path used to spread the sanitized props onto its img; keep doing that so an authored id or align survives, and put the id on the loading slot too so a fragment link resolves before the image does. Co-Authored-By: Claude Code <[email protected]>
1 parent 03a9de5 commit 6d6472c

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import React, {
5252
Children,
5353
Suspense,
5454
type CSSProperties,
55+
type ComponentProps,
5556
type ClipboardEvent as ReactClipboardEvent,
5657
type KeyboardEvent as ReactKeyboardEvent,
5758
type MouseEvent as ReactMouseEvent,
@@ -1362,6 +1363,10 @@ function ChatMarkdownImage(props: {
13621363
readonly standalone: boolean;
13631364
readonly className?: string | undefined;
13641365
readonly style?: CSSProperties | undefined;
1366+
/** Sanitized authored attributes (`id`, `align`, …) that fragment links and layout rely on. */
1367+
readonly imageProps?:
1368+
| Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
1369+
| undefined;
13651370
readonly actionsSource: MediaActionSource;
13661371
readonly originalUrl?: string | undefined;
13671372
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
@@ -1391,6 +1396,7 @@ function ChatMarkdownImage(props: {
13911396
return (
13921397
<MediaActions source={props.actionsSource}>
13931398
<img
1399+
{...props.imageProps}
13941400
ref={markLoadedIfComplete}
13951401
src={src}
13961402
alt={props.alt}
@@ -1424,6 +1430,7 @@ function ChatMarkdownImage(props: {
14241430
/>
14251431
) : (
14261432
<span
1433+
id={props.imageProps?.id}
14271434
data-markdown-copy={props.copyMarkdown}
14281435
role="status"
14291436
aria-label="Loading image"
@@ -1434,6 +1441,7 @@ function ChatMarkdownImage(props: {
14341441
return (
14351442
<MediaActions source={props.actionsSource}>
14361443
<span
1444+
id={props.imageProps?.id}
14371445
data-markdown-copy={props.copyMarkdown}
14381446
className={cn(
14391447
CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
@@ -2940,7 +2948,8 @@ const CHAT_MARKDOWN_COMPONENTS = {
29402948
typeof localSrc === "string" ? srcString.replaceAll("\\", "/") : srcString;
29412949
const altText = alt ?? "";
29422950
const copyMarkdown = markdownImageCopy(altText, srcString, authoredTitle);
2943-
const authoredSizeStyle = authoredImageSizeStyle(props.width, props.height);
2951+
const { className, style: _style, width, height, ...imageProps } = props;
2952+
const authoredSizeStyle = authoredImageSizeStyle(width, height);
29442953
const imageSource = classifyMarkdownImageSource(classifiedSrc, imageBaseDir ?? cwd);
29452954
const kind = mediaKindFromPath(classifiedSrc) ?? "image";
29462955
if (imageSource._tag === "Direct") {
@@ -2973,8 +2982,9 @@ const CHAT_MARKDOWN_COMPONENTS = {
29732982
alt={altText}
29742983
copyMarkdown={copyMarkdown}
29752984
standalone={standalone}
2976-
className={props.className}
2985+
className={className}
29772986
style={authoredSizeStyle}
2987+
imageProps={imageProps}
29782988
actionsSource={actionsSource}
29792989
originalUrl={originalUrl}
29802990
onImageExpand={imageExpand}

‎apps/web/src/components/ChatMarkdown.workspace-images.test.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ describe("ChatMarkdown workspace images", () => {
238238
expect(render(markdown)).not.toContain("aspect-video");
239239
});
240240

241+
it("keeps an authored id on a remote image so fragment links resolve", () => {
242+
const html = render('<img id="diagram" src="https://example.com/diagram.png" alt="diagram">');
243+
244+
// The sanitizer prefixes authored ids; the loading slot carries it too.
245+
expect(html).toContain('<span id="user-content-diagram"');
246+
});
247+
241248
it("reserves a slot for an image that is alone in a list item", () => {
242249
expect(render("- ![shot](.t3/workspace-image.svg)")).toContain("aspect-video");
243250
});

0 commit comments

Comments
 (0)