Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions frontend/components/prompt-kit/markdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,51 @@ describe("Markdown links", () => {
}
});

test("reads a typed chip label from a formatted link text", () => {
const bold = renderToStaticMarkup(
<Markdown>{"[**Quarterly** report](https://example.com/report.pdf)"}</Markdown>,
);
expect(bold).toContain('data-chip="true"');
expect(bold).toContain('<span class="max-w-56 truncate">Quarterly report</span>');
expect(bold).toContain(">Q<"); // round badge shows the label initial
expect(bold).not.toContain("[object Object]");
expect(bold).not.toContain(">Open<");

const code = renderToStaticMarkup(
<Markdown>{"[`data.csv`](https://example.com/data.csv)"}</Markdown>,
);
expect(code).toContain('data-chip="true"');
expect(code).toContain('<span class="max-w-56 truncate">data.csv</span>');
expect(code).toContain(">D<");
expect(code).not.toContain(">Open<");

const mixed = renderToStaticMarkup(
<Markdown>{"[**Quarterly** `data.csv` report](https://example.com/data.csv)"}</Markdown>,
);
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(
<Markdown>
{"[![Chart legend](https://cdn.example.com/a.png)](https://example.com/a.pdf)"}
</Markdown>,
);
expect(image).toContain("Chart legend");

const broken = renderToStaticMarkup(
<Markdown>{"[Quarterly\nreport](https://example.com/report.pdf)"}</Markdown>,
);
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(<Markdown>{"[](https://example.com/report.pdf)"}</Markdown>);
expect(html).toContain('<span class="max-w-56 truncate">Open</span>');
});

test("preserves formatted labels on inert local links", () => {
const html = renderToStaticMarkup(
<WorkspaceOpenProvider value={() => {}}>
Expand Down
25 changes: 18 additions & 7 deletions frontend/components/prompt-kit/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<Components> = {
code: function CodeComponent({ className, children, ...props }) {
const isInline =
Expand Down Expand Up @@ -277,12 +293,7 @@ const INITIAL_COMPONENTS: Partial<Components> = {
// 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.
Expand Down
Loading