TypeScript
fix(web): images on their own line no longer sit beside their label (#22787)
Theo Brownee27a03e94d43
diff --git a/packages/shared/src/markdownPipeline.test.ts b/packages/shared/src/markdownPipeline.test.tsindex b546fcc57c6..ea1a6138cd7 100644--- a/packages/shared/src/markdownPipeline.test.ts+++ b/packages/shared/src/markdownPipeline.test.ts@@ -55,3 +55,25 @@ it("keeps backslashes that CommonMark would read as escapes in prose Windows patString.raw`<code>C:\me\.t3</code> and C:\me\.t3`,);});++it("keeps an image on its own line under a label but leaves inline images inline", () => {+ const processor = unified()+ .use(remarkParse)+ .use(CHAT_MARKDOWN_REMARK_PLUGINS)+ .use(remarkRehype, { allowDangerousHtml: true })+ .use(CHAT_MARKDOWN_REHYPE_PLUGINS);+ type Node = { type: string; value?: string; tagName?: string; children?: Node[] };+ const shape = (node: Node): string =>+ node.type === "text"+ ? (node.value ?? "")+ : node.tagName === "br" || node.tagName === "img"+ ? `<${node.tagName}>`+ : (node.children ?? []).map(shape).join("");+ const render = (markdown: string) => shape(processor.runSync(processor.parse(markdown)) as Node);++ expect(+ render("**Palette**\n\n**Hero** (dark)\n"),+ ).toBe("Palette<br>\n<img><br>\nHero (dark)<br>\n<img>");+ expect(render("Built [](/ci)\nand wrapped")).toBe("Built <img>\nand wrapped");+ expect(render("Hard-wrapped\nprose")).toBe("Hard-wrapped\nprose");+});diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.tsindex a3282584064..767ffdf8365 100644--- a/packages/shared/src/markdownPipeline.ts+++ b/packages/shared/src/markdownPipeline.ts@@ -1,3 +1,4 @@+import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";import type { Processor } from "unified";import { isWindowsAbsolutePath } from "./path.ts";import type { PluggableList } from "unified";@@ -167,6 +168,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [remarkGithubAlerts,remarkNormalizeListItemIndentation,remarkCodexDirectives,+ remarkBreakAroundImageLines,remarkPreserveCodeMeta,remarkNormalizeLinksAndTagInlineCode,];@@ -177,6 +179,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [remarkGithubAlerts,remarkNormalizeListItemIndentation,remarkCodexDirectives,+ remarkBreakAroundImageLines,remarkBreaks,remarkPreserveCodeMeta,remarkNormalizeLinksAndTagInlineCode,@@ -189,6 +192,61 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],];+const isImageNode = (node: PhrasingContent | undefined): boolean =>+ node?.type === "image" ||+ node?.type === "imageReference" ||+ ((node?.type === "link" || node?.type === "linkReference") &&+ node.children.length === 1 &&+ isImageNode(node.children[0]));+const endsLine = (node: PhrasingContent | undefined) =>+ node === undefined ||+ node.type === "break" ||+ (node.type === "text" && /\n[ \t]*$/.test(node.value));+const startsLine = (node: PhrasingContent | undefined) =>+ node === undefined ||+ node.type === "break" ||+ (node.type === "text" && /^[ \t]*\n/.test(node.value));++function breakLinesAroundImages(paragraph: Paragraph) {+ const children = paragraph.children;+ const ownsLine = (index: number) =>+ isImageNode(children[index]) &&+ endsLine(children[index - 1]) &&+ startsLine(children[index + 1]);+ paragraph.children = children.flatMap((child, index): PhrasingContent[] => {+ if (child.type !== "text") return [child];+ const afterImage = ownsLine(index - 1);+ const beforeImage = ownsLine(index + 1);+ if (!afterImage && !beforeImage) return [child];+ const parts: PhrasingContent[] = [];+ let value = child.value;+ if (afterImage) {+ value = value.replace(/^[ \t]*\n/, "");+ parts.push({ type: "break" });+ }+ const breakAfter = beforeImage && /\n[ \t]*$/.test(value);+ if (breakAfter) value = value.replace(/[ \t]*\n[ \t]*$/, "");+ if (value) parts.push({ type: "text", value });+ if (breakAfter) parts.push({ type: "break" });+ return parts;+ });+}++/**+ * Agents label screenshots with a line like `**Palette**` right above the image. CommonMark+ * joins those lines into one paragraph, which puts the image inline beside its label. An+ * image alone on its source line keeps that line; prose and inline badges still wrap.+ */+function remarkBreakAroundImageLines() {+ return (tree: Root) => {+ const visit = (node: Root | RootContent) => {+ if (node.type === "paragraph") breakLinesAroundImages(node);+ else if ("children" in node) node.children.forEach(visit);+ };+ visit(tree);+ };+}+type MarkdownAstNode = {type?: string;meta?: unknown;