fix(web/mobile): render project images in nested markdown file previews and harden markdown images #7857
main fromrefs/pull/7857/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
flamboh(GitHub profile, opens in a new tab) changed the title
- apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx line 154OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -145,4 +148,7 @@145148 </View>146149 ) : null}147150 {props.activeMode === "preview" && isMarkdown ? (148- <FileMarkdownPreview markdown={props.fileContents} onRefresh={props.onRefresh} />151+ <FileMarkdownPreview152+ markdown={props.fileContents}153+ onRefresh={props.onRefresh}154+ renderImage={props.renderMarkdownImage}
- apps/web/src/markdown-links.ts lines 239 to 239OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -188,0 +219,21 @@219+ hasExternalScheme(decodedPath)220+ ) {221+ return null;222+ }223+ if (!isWorkspaceImagePreviewPath(decodedPath)) return null;224+225+ if (!isRelativePath(decodedPath)) return decodedPath;226+ if (!baseDir) return null;227+ return resolvePathLinkTarget(decodedPath, baseDir);228+}229+230+interface HastNodeLike {231+ type: string;232+ tagName?: string;233+ properties?: Record<string, unknown>;234+ children?: Array<HastNodeLike>;235+}236+237+/** `C:/...` and `C:\...` hrefs read as a "c:" scheme, so URL filters drop them. */238+export function isWindowsDrivePathHref(href: string): boolean {239+ return WINDOWS_DRIVE_PATH_PATTERN.test(href);apps/web/src/markdown-links.ts lines 248 to 251OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -188,0 +231,21 @@231+ type: string;232+ tagName?: string;233+ properties?: Record<string, unknown>;234+ children?: Array<HastNodeLike>;235+}236+237+/** `C:/...` and `C:\...` hrefs read as a "c:" scheme, so URL filters drop them. */238+export function isWindowsDrivePathHref(href: string): boolean {239+ return WINDOWS_DRIVE_PATH_PATTERN.test(href);240+}241+242+/**243+ * A src the sanitizer's protocol allowlist would drop even though it names a244+ * local file: a Windows drive path parses as a scheme ("c:"), and file: urls245+ * are not http(s). Tested on the decoded form — the markdown parser percent-246+ * encodes backslashes, turning `C:\repo` into `C:%5Crepo`.247+ */248+function isSanitizerStrippedImageSrc(src: string): boolean {249+ const decoded = safeDecode(src);250+ return WINDOWS_DRIVE_PATH_PATTERN.test(decoded) || decoded.toLowerCase().startsWith("file:");251+}
- apps/web/src/components/ChatMarkdown.tsx lines 1771 to 1776OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1713,4 +1758,19 @@17131758 );17141759 },1715- img({ node: _node, title: _title, ...props }) {1716- return <img {...props} />;1760+ img({ node: _node, title: _title, src, ...props }) {1761+ // Relative and workspace-absolute sources are unreachable from the1762+ // browser; swap them for a signed asset URL. External URLs pass1763+ // through. Windows drive and file: srcs only survive sanitization as1764+ // data-local-src (see rehypePreserveLocalImageSrc).1765+ const preservedSrc = (props as Record<string, unknown>)["data-local-src"];1766+ const localSrc = typeof preservedSrc === "string" ? preservedSrc : src;1767+ if (threadRef && typeof localSrc === "string") {1768+ const workspacePath = resolveMarkdownImagePath(localSrc, imageBaseDir ?? cwd);1769+ if (workspacePath !== null) {1770+ return (1771+ <MarkdownWorkspaceImage1772+ threadRef={threadRef}1773+ absolutePath={workspacePath}1774+ src={localSrc}1775+ {...props}1776+ />
- apps/web/src/components/ChatMarkdown.tsx line 1393OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1354,0 +1373,21 @@1373+function MarkdownWorkspaceImage({1374+ threadRef,1375+ absolutePath,1376+ src,1377+ ...imgProps1378+}: Omit<React.ComponentProps<"img">, "src"> & {1379+ threadRef: ScopedThreadRef;1380+ absolutePath: string;1381+ src: string;1382+}) {1383+ const assetUrl = useAssetUrlState(threadRef.environmentId, {1384+ _tag: "workspace-file",1385+ threadId: threadRef.threadId,1386+ path: absolutePath,1387+ });1388+ if (assetUrl._tag === "Loading") {1389+ // No src yet: an empty box holds any author-set width/height without1390+ // flashing a broken-image glyph.1391+ return <img {...imgProps} />;1392+ }1393+ return <img {...imgProps} src={assetUrl._tag === "Success" ? assetUrl.url : src} />;
flamboh(GitHub profile, opens in a new tab) changed the title
flamboh(GitHub profile, opens in a new tab) changed the title
- apps/web/src/components/ChatMarkdown.tsx lines 1030 to 1031OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1010,11 +1019,13 @@10101019 const [failedUrl, setFailedUrl] = useState<string | null>(null);1011102010121021 if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {1013- return <ChatMarkdownImageFallback alt={props.alt} />;1022+ return <ChatMarkdownImageFallback alt={props.alt} copyMarkdown={props.copyMarkdown} />;10141023 }10151024 if (assetUrl._tag !== "Success") {10161025 return (10171026 <span1027+ data-markdown-copy={props.copyMarkdown}10181028 role="status"10191029 aria-label="Loading image"10201030 className="my-1 block aspect-video w-full max-w-[30rem] rounded-lg bg-muted/60"1031+ style={props.style}
- apps/web/src/components/ChatMarkdown.tsx lines 979 to 981OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -981 +962,20 @@981- "h-auto w-auto max-h-[30rem] max-w-[min(100%,30rem)] object-contain";962+const CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME = "max-h-[30rem] max-w-[min(100%,30rem)]";963+const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(964+ "h-auto w-auto object-contain",965+ CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,966+);967+968+/**969+ * Applies authored image dimensions as inline pixel sizes on rendered images and970+ * workspace placeholders. Inline styles beat the default auto-sizing utilities,971+ * while their max-width and max-height caps still clamp oversized images.972+ */973+function authoredImageSizeStyle(974+ width: string | number | undefined,975+ height: string | number | undefined,976+): CSSProperties | undefined {977+ const parsedWidth = Number(width);978+ const parsedHeight = Number(height);979+ const style: CSSProperties = {};980+ if (!Number.isNaN(parsedWidth) && parsedWidth > 0) style.width = parsedWidth;981+ if (!Number.isNaN(parsedHeight) && parsedHeight > 0) style.height = parsedHeight;
- apps/web/src/components/ChatMarkdown.tsx lines 982 to 985OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -981,0 +965,21 @@965+ CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,966+);967+968+/**969+ * Applies authored image dimensions to rendered images and workspace placeholders.970+ * When both axes are present, the authored ratio keeps either size cap from971+ * distorting the box.972+ */973+function authoredImageSizeStyle(974+ width: string | number | undefined,975+ height: string | number | undefined,976+): CSSProperties | undefined {977+ const parsedWidth = Number(width);978+ const parsedHeight = Number(height);979+ const style: CSSProperties = {};980+ if (!Number.isNaN(parsedWidth) && parsedWidth > 0) style.width = parsedWidth;981+ if (!Number.isNaN(parsedHeight) && parsedHeight > 0) style.height = parsedHeight;982+ if (style.width !== undefined && style.height !== undefined) {983+ style.aspectRatio = `${parsedWidth} / ${parsedHeight}`;984+ style.height = "auto";985+ }
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/components/ChatMarkdown.tsx lines 1072 to 1074OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1010,11 +1061,14 @@10101061 const [failedUrl, setFailedUrl] = useState<string | null>(null);1011106210121063 if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {1013- return <ChatMarkdownImageFallback alt={props.alt} />;1064+ return <ChatMarkdownImageFallback alt={props.alt} copyMarkdown={props.copyMarkdown} />;10141065 }10151066 if (assetUrl._tag !== "Success") {10161067 return (10171068 <span1069+ data-markdown-copy={props.copyMarkdown}10181070 role="status"10191071 aria-label="Loading image"1020- className="my-1 block aspect-video w-full max-w-[30rem] rounded-lg bg-muted/60"1072+ className={cn(1073+ CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME,1074+ "my-1 aspect-video w-full rounded-lg bg-muted/60",
flamboh(GitHub profile, opens in a new tab) changed the title
- apps/web/src/components/ChatMarkdown.tsx lines 999 to 1004OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -981,0 +984,21 @@984+const CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME = "max-h-[30rem] max-w-[min(100%,30rem)]";985+const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(986+ "h-auto w-auto object-contain",987+ CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,988+);989+990+function authoredImageSizeStyle(991+ width: string | number | undefined,992+ height: string | number | undefined,993+): CSSProperties | undefined {994+ const parsedWidth = Number(width);995+ 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)`;1003+ }1004+ return style.width === undefined && style.height === undefined ? undefined : style;apps/web/src/components/ChatMarkdown.tsx lines 1007 to 1007OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -982,3 +989,19 @@989+990+function authoredImageSizeStyle(991+ width: string | number | undefined,992+ height: string | number | undefined,993+): CSSProperties | undefined {994+ const parsedWidth = Number(width);995+ 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)`;1003+ }1004+ return style.width === undefined && style.height === undefined ? undefined : style;1005+}9821006983-// block! outranks the unlayered `.chat-markdown img { display: inline-block }`984-// rule, keeping workspace images on the same block layout as their placeholder.1007+const CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME = "inline-block! align-bottom";
- apps/web/src/components/ChatMarkdown.tsx lines 997 to 1004OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -981,0 +984,21 @@984+const CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME = "max-h-[30rem] max-w-[min(100%,30rem)]";985+const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(986+ "h-auto w-auto object-contain",987+ CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME,988+);989+990+function authoredImageSizeStyle(991+ width: string | number | undefined,992+ height: string | number | undefined,993+): CSSProperties | undefined {994+ const parsedWidth = Number(width);995+ 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)`;1003+ }1004+ return style.width === undefined && style.height === undefined ? undefined : style;
t3dotgg(GitHub profile, opens in a new tab) closed this
flamboh(GitHub profile, opens in a new tab) changed the title
flamboh(GitHub profile, opens in a new tab) changed the title
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
cd695e75-8500-41a0-b420-80cef0d58105You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share