diff --git a/frontend/components/prompt-kit/markdown.test.tsx b/frontend/components/prompt-kit/markdown.test.tsx index 13209094..edea66da 100644 --- a/frontend/components/prompt-kit/markdown.test.tsx +++ b/frontend/components/prompt-kit/markdown.test.tsx @@ -58,6 +58,51 @@ describe("Markdown links", () => { } }); + test("reads a typed chip label from a formatted link text", () => { + const bold = renderToStaticMarkup( + {"[**Quarterly** report](https://example.com/report.pdf)"}, + ); + expect(bold).toContain('data-chip="true"'); + expect(bold).toContain('Quarterly report'); + expect(bold).toContain(">Q<"); // round badge shows the label initial + expect(bold).not.toContain("[object Object]"); + expect(bold).not.toContain(">Open<"); + + const code = renderToStaticMarkup( + {"[`data.csv`](https://example.com/data.csv)"}, + ); + expect(code).toContain('data-chip="true"'); + expect(code).toContain('data.csv'); + expect(code).toContain(">D<"); + expect(code).not.toContain(">Open<"); + + const mixed = renderToStaticMarkup( + {"[**Quarterly** `data.csv` report](https://example.com/data.csv)"}, + ); + expect(mixed).toContain("Quarterly data.csv report"); + expect(mixed).toContain(">Q<"); + }); + + test("keeps a link label when it is an image or spans lines", () => { + const image = renderToStaticMarkup( + + {"[![Chart legend](https://cdn.example.com/a.png)](https://example.com/a.pdf)"} + , + ); + expect(image).toContain("Chart legend"); + + const broken = renderToStaticMarkup( + {"[Quarterly\nreport](https://example.com/report.pdf)"}, + ); + expect(broken).toContain("Quarterly"); + expect(broken).not.toContain("[object Object]"); + }); + + test("falls back to Open when a link carries no readable text", () => { + const html = renderToStaticMarkup({"[](https://example.com/report.pdf)"}); + expect(html).toContain('Open'); + }); + test("preserves formatted labels on inert local links", () => { const html = renderToStaticMarkup( {}}> diff --git a/frontend/components/prompt-kit/markdown.tsx b/frontend/components/prompt-kit/markdown.tsx index 640b8b03..172b9f60 100644 --- a/frontend/components/prompt-kit/markdown.tsx +++ b/frontend/components/prompt-kit/markdown.tsx @@ -4,7 +4,7 @@ // per-block memoization keep re-renders cheap while text streams in. import { marked } from "marked"; -import { memo, useEffect, useId, useMemo, useState } from "react"; +import { isValidElement, memo, useEffect, useId, useMemo, useState, type ReactNode } from "react"; import ReactMarkdown, { type Components, defaultUrlTransform } from "react-markdown"; import remarkBreaks from "remark-breaks"; import remarkGfm from "remark-gfm"; @@ -197,6 +197,22 @@ function isArtifactRoute(url: string): boolean { return /^\/(?:api\/artifacts\/[^/]+\/content|agent\/artifacts\/[^/]+)$/.test(parsed.pathname); } +/** The words a reader sees inside a link, however Markdown formats them. + * `[**Quarterly** report]` and ``[`data.csv`]`` arrive as React elements, + * which stringify to "[object Object]", and a single wrapped run fell through + * to "Open". Walk the rendered children to their text instead: a line break + * reads as a space, and an image with no text of its own reads as its alt. */ +function linkLabelText(children: ReactNode): string { + if (typeof children === "string" || typeof children === "number") return String(children); + if (Array.isArray(children)) return children.map(linkLabelText).join(""); + if (!isValidElement(children)) return ""; + if (children.type === "br") return " "; + const props = children.props as { children?: ReactNode; alt?: unknown }; + const nested = linkLabelText(props.children); + if (nested) return nested; + return typeof props.alt === "string" ? props.alt : ""; +} + const INITIAL_COMPONENTS: Partial = { code: function CodeComponent({ className, children, ...props }) { const isInline = @@ -277,12 +293,7 @@ const INITIAL_COMPONENTS: Partial = { // Artifact/media links render as dense source chips (type badge + label + // arrow), matching the retrieval-chip grammar; ordinary links stay links. const workspaceLinks = openWorkpiece !== null; - const label = - typeof children === "string" - ? children - : Array.isArray(children) - ? children.join("") - : "Open"; + const label = linkLabelText(children).trim() || "Open"; // A local filesystem path cannot be opened from the browser, whatever its // file type, so name it without pretending it was published or is a link. // Artifact routes, web URLs, protocol-relative links and anchors stay links.