@devries-cameron20 / t3code-1 / commits / e27a03e94d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): images on their own line no longer sit beside their label (#22787)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 21, 2026 at 02:43 UTC e27a03e94d43b504cfc3933282af913763312afd
2 files changed +80
packages/shared/src/markdownPipeline.test.ts
+22
@@ -55,3 +55,25 @@ it("keeps backslashes that CommonMark would read as escapes in prose Windows pat
55 String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,
56 );
57 });
58 +
59 +it("keeps an image on its own line under a label but leaves inline images inline", () => {
60 + const processor = unified()
61 + .use(remarkParse)
62 + .use(CHAT_MARKDOWN_REMARK_PLUGINS)
63 + .use(remarkRehype, { allowDangerousHtml: true })
64 + .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
65 + type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
66 + const shape = (node: Node): string =>
67 + node.type === "text"
68 + ? (node.value ?? "")
69 + : node.tagName === "br" || node.tagName === "img"
70 + ? `<${node.tagName}>`
71 + : (node.children ?? []).map(shape).join("");
72 + const render = (markdown: string) => shape(processor.runSync(processor.parse(markdown)) as Node);
73 +
74 + expect(
75 + render("**Palette**\n![Palette](/tmp/palette.png)\n**Hero** (dark)\n![Hero](/tmp/hero.png)"),
76 + ).toBe("Palette<br>\n<img><br>\nHero (dark)<br>\n<img>");
77 + expect(render("Built [![CI](/ci.svg)](/ci)\nand wrapped")).toBe("Built <img>\nand wrapped");
78 + expect(render("Hard-wrapped\nprose")).toBe("Hard-wrapped\nprose");
79 +});
packages/shared/src/markdownPipeline.ts
+58
@@ -1,3 +1,4 @@
1 +import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";
2 import type { Processor } from "unified";
3 import { isWindowsAbsolutePath } from "./path.ts";
4 import type { PluggableList } from "unified";
@@ -167,6 +168,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
168 remarkGithubAlerts,
169 remarkNormalizeListItemIndentation,
170 remarkCodexDirectives,
171 + remarkBreakAroundImageLines,
172 remarkPreserveCodeMeta,
173 remarkNormalizeLinksAndTagInlineCode,
174 ];
@@ -177,6 +179,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [
179 remarkGithubAlerts,
180 remarkNormalizeListItemIndentation,
181 remarkCodexDirectives,
182 + remarkBreakAroundImageLines,
183 remarkBreaks,
184 remarkPreserveCodeMeta,
185 remarkNormalizeLinksAndTagInlineCode,
@@ -189,6 +192,61 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
192 [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
193 ];
194
195 +const isImageNode = (node: PhrasingContent | undefined): boolean =>
196 + node?.type === "image" ||
197 + node?.type === "imageReference" ||
198 + ((node?.type === "link" || node?.type === "linkReference") &&
199 + node.children.length === 1 &&
200 + isImageNode(node.children[0]));
201 +const endsLine = (node: PhrasingContent | undefined) =>
202 + node === undefined ||
203 + node.type === "break" ||
204 + (node.type === "text" && /\n[ \t]*$/.test(node.value));
205 +const startsLine = (node: PhrasingContent | undefined) =>
206 + node === undefined ||
207 + node.type === "break" ||
208 + (node.type === "text" && /^[ \t]*\n/.test(node.value));
209 +
210 +function breakLinesAroundImages(paragraph: Paragraph) {
211 + const children = paragraph.children;
212 + const ownsLine = (index: number) =>
213 + isImageNode(children[index]) &&
214 + endsLine(children[index - 1]) &&
215 + startsLine(children[index + 1]);
216 + paragraph.children = children.flatMap((child, index): PhrasingContent[] => {
217 + if (child.type !== "text") return [child];
218 + const afterImage = ownsLine(index - 1);
219 + const beforeImage = ownsLine(index + 1);
220 + if (!afterImage && !beforeImage) return [child];
221 + const parts: PhrasingContent[] = [];
222 + let value = child.value;
223 + if (afterImage) {
224 + value = value.replace(/^[ \t]*\n/, "");
225 + parts.push({ type: "break" });
226 + }
227 + const breakAfter = beforeImage && /\n[ \t]*$/.test(value);
228 + if (breakAfter) value = value.replace(/[ \t]*\n[ \t]*$/, "");
229 + if (value) parts.push({ type: "text", value });
230 + if (breakAfter) parts.push({ type: "break" });
231 + return parts;
232 + });
233 +}
234 +
235 +/**
236 + * Agents label screenshots with a line like `**Palette**` right above the image. CommonMark
237 + * joins those lines into one paragraph, which puts the image inline beside its label. An
238 + * image alone on its source line keeps that line; prose and inline badges still wrap.
239 + */
240 +function remarkBreakAroundImageLines() {
241 + return (tree: Root) => {
242 + const visit = (node: Root | RootContent) => {
243 + if (node.type === "paragraph") breakLinesAroundImages(node);
244 + else if ("children" in node) node.children.forEach(visit);
245 + };
246 + visit(tree);
247 + };
248 +}
249 +
250 type MarkdownAstNode = {
251 type?: string;
252 meta?: unknown;