Skip to content

Commit f632e92

Browse files
committed
fix(web): keep inline markdown images proportional
1 parent 84eb288 commit f632e92

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -982,8 +982,10 @@ const MarkdownLinkFavicon = memo(function MarkdownLinkFavicon({ host }: { host:
982982
});
983983

984984
const CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME = "max-h-[30rem] max-w-[min(100%,30rem)]";
985+
const CHAT_MARKDOWN_IMAGE_ALIGNMENT_CLASS_NAME = "align-bottom";
985986
const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(
986987
"h-auto w-auto object-contain",
988+
CHAT_MARKDOWN_IMAGE_ALIGNMENT_CLASS_NAME,
987989
CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,
988990
);
989991

@@ -993,22 +995,29 @@ function authoredImageSizeStyle(
993995
): CSSProperties | undefined {
994996
const parsedWidth = Number(width);
995997
const parsedHeight = Number(height);
996-
const style: CSSProperties = {};
997-
if (Number.isFinite(parsedWidth) && parsedWidth > 0) style.width = parsedWidth;
998-
if (Number.isFinite(parsedHeight) && parsedHeight > 0) style.height = parsedHeight;
999-
if (style.width !== undefined && style.height !== undefined) {
1000-
style.aspectRatio = `${parsedWidth} / ${parsedHeight}`;
1001-
style.height = "auto";
1002-
style.maxWidth = `min(100%, 30rem, ${(30 * parsedWidth) / parsedHeight}rem)`;
998+
const hasWidth = Number.isFinite(parsedWidth) && parsedWidth > 0;
999+
const hasHeight = Number.isFinite(parsedHeight) && parsedHeight > 0;
1000+
if (hasWidth && hasHeight) {
1001+
return {
1002+
width: parsedWidth,
1003+
height: "auto",
1004+
aspectRatio: `${parsedWidth} / ${parsedHeight}`,
1005+
maxWidth: `min(100%, 30rem, ${(30 * parsedWidth) / parsedHeight}rem)`,
1006+
};
10031007
}
1004-
return style.width === undefined && style.height === undefined ? undefined : style;
1008+
if (hasWidth) return { maxWidth: `min(100%, 30rem, ${parsedWidth}px)` };
1009+
if (hasHeight) return { maxHeight: `min(30rem, ${parsedHeight}px)` };
1010+
return undefined;
10051011
}
10061012

1007-
const CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME = "inline-block! align-bottom";
1013+
const CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME = cn(
1014+
"inline-block!",
1015+
CHAT_MARKDOWN_IMAGE_ALIGNMENT_CLASS_NAME,
1016+
);
10081017
const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn(
10091018
CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME,
10101019
CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME,
1011-
"my-1 rounded-lg border border-border/40",
1020+
"rounded-lg border border-border/40",
10121021
);
10131022

10141023
function ChatMarkdownImageFallback(props: {
@@ -1020,7 +1029,7 @@ function ChatMarkdownImageFallback(props: {
10201029
data-markdown-copy={props.copyMarkdown}
10211030
className={cn(
10221031
CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME,
1023-
"my-1 rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground",
1032+
"rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground",
10241033
)}
10251034
>
10261035
<span className="inline-flex items-center gap-1.5">
@@ -1058,7 +1067,7 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
10581067
aria-label="Loading image"
10591068
className={cn(
10601069
CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME,
1061-
"my-1 aspect-video w-64 max-w-full rounded-lg bg-muted/60",
1070+
"aspect-video w-64 max-w-full rounded-lg bg-muted/60",
10621071
CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,
10631072
)}
10641073
style={props.style}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,35 @@ describe("ChatMarkdown workspace images", () => {
186186
expect(loadingStyle).toEqual(loadedStyle);
187187
});
188188

189+
it.each([
190+
["width", "max-width", "min(100%, 30rem, 300px)"],
191+
["height", "max-height", "min(30rem, 300px)"],
192+
])("treats a lone authored %s as a cap", (axis, constraint, expectedValue) => {
193+
const markdown = `<img src=".t3/workspace-image.svg" alt="sized" ${axis}="300">`;
194+
const loadedStyle = firstInlineStyle(render(markdown));
195+
testState.assetState = "loading";
196+
const loadingStyle = firstInlineStyle(render(markdown));
197+
198+
expect(loadedStyle).not.toHaveProperty(axis);
199+
expect(loadedStyle).toHaveProperty(constraint, expectedValue);
200+
expect(loadingStyle).toEqual(loadedStyle);
201+
});
202+
203+
it("aligns direct and workspace images the same way in inline rows", () => {
204+
const html = render(
205+
"![remote](https://example.com/badge.svg) ![workspace](.t3/workspace-image.svg)",
206+
);
207+
const classNames = Array.from(html.matchAll(/<img[^>]*class="([^"]*)"/g), (match) =>
208+
match[1]?.split(" "),
209+
);
210+
211+
expect(classNames).toHaveLength(2);
212+
for (const classes of classNames) {
213+
expect(classes).toContain("align-bottom");
214+
expect(classes).not.toContain("my-1");
215+
}
216+
});
217+
189218
it("retains an authored SVG fragment on the signed URL", () => {
190219
const html = render("![logo](icons.svg#logo)");
191220

0 commit comments

Comments
 (0)