Skip to content

Commit 8007824

Browse files
t3dotggclaude
andcommitted
fix(web): images on their own line no longer sit beside their label
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1dfb572 commit 8007824

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎packages/shared/src/markdownPipeline.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,25 @@ it("keeps backslashes that CommonMark would read as escapes in prose Windows pat
5555
String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,
5656
);
5757
});
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‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";
12
import type { Processor } from "unified";
23
import { isWindowsAbsolutePath } from "./path.ts";
34
import type { PluggableList } from "unified";
@@ -167,6 +168,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
167168
remarkGithubAlerts,
168169
remarkNormalizeListItemIndentation,
169170
remarkCodexDirectives,
171+
remarkBreakAroundImageLines,
170172
remarkPreserveCodeMeta,
171173
remarkNormalizeLinksAndTagInlineCode,
172174
];
@@ -177,6 +179,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [
177179
remarkGithubAlerts,
178180
remarkNormalizeListItemIndentation,
179181
remarkCodexDirectives,
182+
remarkBreakAroundImageLines,
180183
remarkBreaks,
181184
remarkPreserveCodeMeta,
182185
remarkNormalizeLinksAndTagInlineCode,
@@ -189,6 +192,61 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
189192
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
190193
];
191194

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+
192250
type MarkdownAstNode = {
193251
type?: string;
194252
meta?: unknown;

0 commit comments

Comments
 (0)