Skip to content

Commit 2ca2b78

Browse files
committed
fix(web): render nested markdown images correctly
1 parent c8aba25 commit 2ca2b78

8 files changed

Lines changed: 329 additions & 38 deletions

File tree

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

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ import {
2424
squashAtomCommandFailure,
2525
type AtomCommandResult,
2626
} from "@t3tools/client-runtime/state/runtime";
27-
import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images";
27+
import {
28+
classifyMarkdownImageSource,
29+
markdownImageSourceFragment,
30+
} from "@t3tools/client-runtime/markdown-images";
2831
import * as Cause from "effect/Cause";
2932
import { AsyncResult } from "effect/unstable/reactivity";
3033
import React, {
3134
Children,
3235
Suspense,
36+
type CSSProperties,
3337
type ClipboardEvent as ReactClipboardEvent,
3438
type MouseEvent as ReactMouseEvent,
3539
isValidElement,
@@ -90,6 +94,7 @@ import {
9094
import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation";
9195
import {
9296
extractMarkdownLinkHrefs,
97+
isWindowsDrivePathHref,
9398
normalizeMarkdownLinkDestination,
9499
resolveInlineCodeFileLinkMeta,
95100
resolveMarkdownFileLinkMeta,
@@ -149,6 +154,7 @@ interface ChatMarkdownProps {
149154
lineBreaks?: boolean;
150155
/** Parse sanitized raw HTML instead of displaying its source text. */
151156
parseRawHtml?: boolean;
157+
imageBaseDir?: string | undefined;
152158
}
153159

154160
export function canUseMarkdownFileShellActions(
@@ -237,28 +243,25 @@ export function orderedListGutterStyle(
237243
return { "--list-gutter": `${markerWidth + 1}ch` };
238244
}
239245

240-
type MarkdownHtmlAstNode = {
246+
type MarkdownImageHastNode = {
241247
type?: string;
242248
tagName?: string;
243249
properties?: Record<string, unknown>;
244-
children?: MarkdownHtmlAstNode[];
250+
children?: MarkdownImageHastNode[];
245251
};
246252

247-
/** Preserve Windows drive paths through the protocol allowlist in rehype-sanitize. */
248-
function rehypeNormalizeWindowsImageSrc() {
249-
return (tree: MarkdownHtmlAstNode) => {
250-
const visit = (node: MarkdownHtmlAstNode) => {
253+
/** Carries raw Windows drive paths through the sanitizer to the image renderer. */
254+
function rehypePreserveWindowsImageSrc() {
255+
return (tree: MarkdownImageHastNode) => {
256+
const visit = (node: MarkdownImageHastNode) => {
251257
const src = node.properties?.src;
252258
if (
253259
node.type === "element" &&
254260
node.tagName === "img" &&
255261
typeof src === "string" &&
256-
WINDOWS_DRIVE_PATH_REGEX.test(src)
262+
isWindowsDrivePathHref(src)
257263
) {
258-
node.properties = {
259-
...node.properties,
260-
src: `file:///${src.replaceAll("\\", "/")}`,
261-
};
264+
node.properties = { ...node.properties, dataLocalSrc: src };
262265
}
263266
node.children?.forEach(visit);
264267
};
@@ -274,6 +277,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
274277
"*": (defaultSchema.attributes?.["*"] ?? []).filter((attribute) => attribute !== "title"),
275278
code: [...(defaultSchema.attributes?.code ?? []), "dataCodeMeta", "dataInlineCode"],
276279
blockquote: [...(defaultSchema.attributes?.blockquote ?? []), "dataAlert"],
280+
img: [...(defaultSchema.attributes?.img ?? []), "dataLocalSrc"],
277281
},
278282
protocols: {
279283
...defaultSchema.protocols,
@@ -301,7 +305,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
301305

302306
const CHAT_MARKDOWN_REHYPE_PLUGINS = [
303307
rehypeRaw,
304-
rehypeNormalizeWindowsImageSrc,
308+
rehypePreserveWindowsImageSrc,
305309
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
306310
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
307311

@@ -1045,21 +1049,60 @@ const MarkdownLinkFavicon = memo(function MarkdownLinkFavicon({ host }: { host:
10451049
);
10461050
});
10471051

1048-
const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME =
1049-
"h-auto w-auto max-h-[30rem] max-w-[min(100%,30rem)] object-contain";
1052+
const CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME = "max-h-[30rem] max-w-[min(100%,30rem)]";
1053+
const CHAT_MARKDOWN_IMAGE_ALIGNMENT_CLASS_NAME = "align-bottom";
1054+
const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(
1055+
"h-auto w-auto object-contain",
1056+
CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,
1057+
);
10501058

1051-
// block! outranks the unlayered `.chat-markdown img { display: inline-block }`
1052-
// rule, keeping workspace images on the same block layout as their placeholder.
1059+
function authoredImageSizeStyle(
1060+
width: string | number | undefined,
1061+
height: string | number | undefined,
1062+
): CSSProperties | undefined {
1063+
const parsedWidth = Number(width);
1064+
const parsedHeight = Number(height);
1065+
const hasWidth = Number.isFinite(parsedWidth) && parsedWidth > 0;
1066+
const hasHeight = Number.isFinite(parsedHeight) && parsedHeight > 0;
1067+
if (hasWidth && hasHeight) {
1068+
return {
1069+
width: parsedWidth,
1070+
height: "auto",
1071+
aspectRatio: `${parsedWidth} / ${parsedHeight}`,
1072+
maxWidth: `min(100%, 30rem, ${(30 * parsedWidth) / parsedHeight}rem)`,
1073+
};
1074+
}
1075+
if (hasWidth) return { maxWidth: `min(100%, 30rem, ${parsedWidth}px)` };
1076+
if (hasHeight) return { maxHeight: `min(30rem, ${parsedHeight}px)` };
1077+
return undefined;
1078+
}
1079+
1080+
const CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME = cn(
1081+
"inline-block!",
1082+
CHAT_MARKDOWN_IMAGE_ALIGNMENT_CLASS_NAME,
1083+
);
10531084
const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn(
10541085
CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME,
1055-
"my-1 block! rounded-lg border border-border/40",
1086+
CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME,
1087+
"rounded-lg border border-border/40",
10561088
);
10571089

1058-
function ChatMarkdownImageFallback(props: { readonly alt: string }) {
1090+
function ChatMarkdownImageFallback(props: {
1091+
readonly alt: string;
1092+
readonly copyMarkdown?: string | undefined;
1093+
}) {
10591094
return (
1060-
<span className="my-1 inline-flex items-center gap-1.5 rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground">
1061-
<TriangleAlertIcon aria-hidden className="size-3.5 shrink-0" />
1062-
{props.alt.length > 0 ? `Image unavailable · ${props.alt}` : "Image unavailable"}
1095+
<span
1096+
data-markdown-copy={props.copyMarkdown}
1097+
className={cn(
1098+
CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME,
1099+
"rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground",
1100+
)}
1101+
>
1102+
<span className="inline-flex items-center gap-1.5">
1103+
<TriangleAlertIcon aria-hidden className="size-3.5 shrink-0" />
1104+
{props.alt.length > 0 ? `Image unavailable · ${props.alt}` : "Image unavailable"}
1105+
</span>
10631106
</span>
10641107
);
10651108
}
@@ -1069,6 +1112,9 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
10691112
readonly threadRef: ScopedThreadRef;
10701113
readonly path: string;
10711114
readonly alt: string;
1115+
readonly copyMarkdown: string;
1116+
readonly srcFragment: string;
1117+
readonly style?: CSSProperties | undefined;
10721118
}) {
10731119
const assetUrl = useAssetUrlState(props.threadRef.environmentId, {
10741120
_tag: "workspace-file",
@@ -1078,24 +1124,32 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
10781124
const [failedUrl, setFailedUrl] = useState<string | null>(null);
10791125

10801126
if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {
1081-
return <ChatMarkdownImageFallback alt={props.alt} />;
1127+
return <ChatMarkdownImageFallback alt={props.alt} copyMarkdown={props.copyMarkdown} />;
10821128
}
10831129
if (assetUrl._tag !== "Success") {
10841130
return (
10851131
<span
1132+
data-markdown-copy={props.copyMarkdown}
10861133
role="status"
10871134
aria-label="Loading image"
1088-
className="my-1 block aspect-video w-full max-w-[30rem] rounded-lg bg-muted/60"
1135+
className={cn(
1136+
CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME,
1137+
"aspect-video w-64 max-w-full rounded-lg bg-muted/60",
1138+
CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,
1139+
)}
1140+
style={props.style}
10891141
/>
10901142
);
10911143
}
10921144
return (
10931145
<img
1094-
src={assetUrl.url}
1146+
src={assetUrl.url + props.srcFragment}
10951147
alt={props.alt}
1148+
data-markdown-copy={props.copyMarkdown}
10961149
loading="lazy"
10971150
draggable={false}
10981151
className={CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME}
1152+
style={props.style}
10991153
onError={() => setFailedUrl(assetUrl.url)}
11001154
/>
11011155
);
@@ -1636,6 +1690,7 @@ function ChatMarkdown({
16361690
className,
16371691
lineBreaks = false,
16381692
parseRawHtml = true,
1693+
imageBaseDir,
16391694
}: ChatMarkdownProps) {
16401695
const { resolvedTheme } = useTheme();
16411696
const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
@@ -1729,6 +1784,7 @@ function ChatMarkdown({
17291784
return buildFileLinkParentSuffixByPath(filePaths);
17301785
}, [inlineCodeFileLinkMetaByText, markdownFileLinkMetaByHref]);
17311786
const markdownUrlTransform = useCallback((href: string) => {
1787+
if (isWindowsDrivePathHref(href)) return href;
17321788
return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
17331789
}, []);
17341790
// Re-emit highlighted content as markdown so copying out of the rendered
@@ -2146,10 +2202,16 @@ function ChatMarkdown({
21462202
</code>
21472203
);
21482204
},
2149-
img({ node: _node, title: _title, src, alt, ...props }) {
2150-
const srcString = typeof src === "string" ? normalizeMarkdownLinkDestination(src) : "";
2205+
img({ node, title: _title, src, alt, ...props }) {
2206+
const localSrc = node?.properties?.dataLocalSrc;
2207+
const authoredSrc = typeof localSrc === "string" ? localSrc : src;
2208+
const srcString =
2209+
typeof authoredSrc === "string" ? normalizeMarkdownLinkDestination(authoredSrc) : "";
2210+
const classifiedSrc =
2211+
typeof localSrc === "string" ? srcString.replaceAll("\\", "/") : srcString;
21512212
const altText = alt ?? "";
2152-
const imageSource = classifyMarkdownImageSource(srcString, cwd);
2213+
const authoredSizeStyle = authoredImageSizeStyle(props.width, props.height);
2214+
const imageSource = classifyMarkdownImageSource(classifiedSrc, imageBaseDir ?? cwd);
21532215
if (imageSource._tag === "Direct") {
21542216
return (
21552217
<img
@@ -2158,6 +2220,7 @@ function ChatMarkdown({
21582220
alt={altText}
21592221
loading="lazy"
21602222
className={cn(props.className, CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME)}
2223+
style={authoredSizeStyle}
21612224
/>
21622225
);
21632226
}
@@ -2167,10 +2230,15 @@ function ChatMarkdown({
21672230
threadRef={threadRef}
21682231
path={imageSource.path}
21692232
alt={altText}
2233+
copyMarkdown={`![${altText}](${srcString})`}
2234+
srcFragment={markdownImageSourceFragment(classifiedSrc)}
2235+
style={authoredSizeStyle}
21702236
/>
21712237
);
21722238
}
2173-
return <ChatMarkdownImageFallback alt={altText} />;
2239+
return (
2240+
<ChatMarkdownImageFallback alt={altText} copyMarkdown={`![${altText}](${srcString})`} />
2241+
);
21742242
},
21752243
table({ node: _node, ...props }) {
21762244
return <MarkdownTable {...props} />;
@@ -2213,6 +2281,7 @@ function ChatMarkdown({
22132281
diffThemeName,
22142282
fileLinkParentSuffixByPath,
22152283
inlineCodeFileLinkMetaByText,
2284+
imageBaseDir,
22162285
isStreaming,
22172286
markdownFileLinkMetaByHref,
22182287
onTaskListChange,

0 commit comments

Comments
 (0)