diff --git a/webui/src/components/MarkdownTextRenderer.tsx b/webui/src/components/MarkdownTextRenderer.tsx index 448498ed7..3982bad38 100644 --- a/webui/src/components/MarkdownTextRenderer.tsx +++ b/webui/src/components/MarkdownTextRenderer.tsx @@ -14,6 +14,7 @@ import remarkGfm from "remark-gfm"; import remarkMath from "remark-math"; import { Streamdown, type Components, type StreamdownProps } from "streamdown"; import remend from "remend"; +import type { ListItem, Nodes, Root } from "mdast"; import { parseMathAwareMarkdownBlocks } from "@/lib/markdown-streaming-blocks"; @@ -75,6 +76,13 @@ type InlineLinkPreview = { title: string; }; +type InlineLinkPreviewProperties = { + "data-link-preview-href": string; + "data-link-preview-host": string; + "data-link-preview-title": string; + "data-link-preview-prefix"?: string; +}; + type AvailabilityResult = { available: boolean; path: string; @@ -308,6 +316,7 @@ const remarkPlugins: NonNullable = [ [remarkMath, { singleDollarTextMath: false }], remarkTexMath, remarkSafeHtmlSubset, + remarkInlineLinkPreviews, ]; type MathPlugin = typeof import("@/lib/markdown-math").default; let loadedMathPlugin: MathPlugin | undefined; @@ -430,28 +439,6 @@ function sessionReferenceHref(href: string): string | null { } } -function linkPreviewParts(value: ReactNode): { text: string; href?: string } { - let text = ""; - let href: string | undefined; - for (const child of Children.toArray(value)) { - if (typeof child === "string" || typeof child === "number") { - text += String(child); - continue; - } - if (!isValidElement(child)) { - continue; - } - const props = child.props as { href?: unknown; children?: ReactNode }; - if (!href && typeof props.href === "string" && /^https?:\/\//i.test(props.href)) { - href = props.href; - } - const nested = linkPreviewParts(props.children); - text += nested.text; - href ||= nested.href; - } - return { text, href }; -} - function cleanLinkPreviewText(value: string): string { return value .replace(/\s+/g, " ") @@ -459,36 +446,57 @@ function cleanLinkPreviewText(value: string): string { .trim(); } -function inlineLinkPreviewFromChildren(children: ReactNode): InlineLinkPreview | null { - const { text: rawText, href } = linkPreviewParts(children); - if (!href) return null; +function inlineLinkPreviewFromListItem(item: ListItem): InlineLinkPreview | null { + if (typeof item.checked === "boolean" || item.children.length !== 1) return null; + const paragraph = item.children[0]; + if (paragraph.type !== "paragraph" || paragraph.children.length !== 3) return null; + const [heading, lineBreak, link] = paragraph.children; + if (heading.type !== "text" || lineBreak.type !== "break" || link.type !== "link" + || link.title || link.children.length !== 1 || link.children[0].type !== "text") return null; let url: URL; try { - url = new URL(href); + url = new URL(link.url); + if (new URL(link.children[0].value).href !== url.href) return null; } catch { return null; } if (url.protocol !== "http:" && url.protocol !== "https:") return null; - const strippedUrl = rawText - .replace(/\s+/g, " ") - .replace(href, "") - .replace(url.toString(), "") - .replace(/https?:\/\/\S+/i, "") - .trim(); - if (!strippedUrl || strippedUrl.length < 4) return null; - - const sourceMatch = /^(.*?)\s*(?:[—–]| - |:)\s*(.+)$/.exec(strippedUrl); + const headingText = heading.value.trim(); + if (headingText.length < 4) return null; + const sourceMatch = /^(.*?)\s*(?:[—–]| - |:)\s*(.+)$/.exec(headingText); const prefix = sourceMatch?.[1] ? cleanLinkPreviewText(sourceMatch[1]) : undefined; - const title = cleanLinkPreviewText(sourceMatch?.[2] ?? strippedUrl); + const title = cleanLinkPreviewText(sourceMatch?.[2] ?? headingText); if (!title || /^https?:\/\//i.test(title)) return null; - return { - href, - host: url.hostname, - prefix, - title, + return { href: link.url, host: url.hostname, prefix, title }; +} + +function remarkInlineLinkPreviews() { + return (tree: Root) => { + function visit(node: Nodes) { + // Only the syntax tree knows both the whole item and its list's numbering. + // Never infer a preview by flattening rendered descendants. + if (node.type === "list" && !node.ordered) { + for (const item of node.children) { + const link = inlineLinkPreviewFromListItem(item); + if (!link) continue; + const properties: InlineLinkPreviewProperties = { + "data-link-preview-href": link.href, + "data-link-preview-host": link.host, + "data-link-preview-title": link.title, + "data-link-preview-prefix": link.prefix, + }; + item.data = { + ...item.data, + hProperties: { ...item.data?.hProperties, ...properties }, + }; + } + } + if ("children" in node) node.children.forEach(visit); + } + visit(tree); }; } @@ -793,12 +801,17 @@ export default function MarkdownTextRenderer({ ); }, - li({ children: markdownChildren, className: itemClassName }) { - const link = !documentPath && inlineLinkPreviewFromChildren(markdownChildren); - if (link) { + li({ children: markdownChildren, className: itemClassName, node }) { + const preview = node?.properties as InlineLinkPreviewProperties | undefined; + if (!documentPath && preview?.["data-link-preview-href"]) { return (
  • - +
  • ); } diff --git a/webui/src/tests/markdown-text-renderer.test.tsx b/webui/src/tests/markdown-text-renderer.test.tsx index 2a9353bec..4aed21e7c 100644 --- a/webui/src/tests/markdown-text-renderer.test.tsx +++ b/webui/src/tests/markdown-text-renderer.test.tsx @@ -63,6 +63,24 @@ describe("MarkdownTextRenderer", () => { expect(container.querySelector("img")).toBeNull(); }); + it("does not promote unsafe title-plus-URL items through preview metadata", () => { + const { container } = render( + + {[ + "- Unsafe script title", + " [javascript:alert(1)](javascript:alert(1))", + "- Unsafe data title", + " [data:text/html,alert](data:text/html,alert)", + ].join("\n")} + , + ); + + expect(container.querySelectorAll("li")).toHaveLength(2); + expect(container.querySelector("li.list-none, a, img")).toBeNull(); + expect(container).toHaveTextContent("javascript:alert(1)"); + expect(container).toHaveTextContent("data:text/html,alert"); + }); + it("keeps safe external, mail, relative, and fragment links", () => { render( @@ -318,6 +336,19 @@ describe("MarkdownTextRenderer", () => { ).toHaveAttribute("href", "https://polymarket.com/event/when-will-gpt-5pt6-be-released"); }); + it("keeps document title-plus-URL lists ordinary without loading favicons", () => { + const { container } = render( + + {"- Example source title\n https://example.com/source"} + , + ); + + expect(container.querySelector("li")).toHaveTextContent("Example source title"); + expect(container.querySelector("li.list-none, img")).toBeNull(); + expect(screen.getByRole("link", { name: "https://example.com/source" })) + .toHaveAttribute("href", "https://example.com/source"); + }); + it("falls back through favicon sources before showing a globe for compact link rows", () => { const { container } = render( @@ -360,6 +391,225 @@ describe("MarkdownTextRenderer", () => { expect(container).not.toHaveTextContent("SC"); }); + describe("compact link preview boundaries", () => { + const reportedMessage = [ + "已完成:", + "", + "- **#6267、#6268、#6271** 已设为 **Draft**,未重写它们的分支。", + "- **[#6269](https://github.com/HKUDS/nanobot/pull/6269)** 已单独 rebase 到最新 `main`,PR base 也已改为 `main`,保持非 Draft。", + " - 新 head:`16128a60a`", + " - 仅保留自身一个提交,未带入 #6267、#6268。", + " - 已用 `--force-with-lease` 推送,并更新 PR 描述。", + "", + "本地验证:**2,441 项通过、1 项跳过**;Ruff、BasedPyright、import boundaries、diff 检查均通过。", + "", + "[新 head 的 CI](https://github.com/HKUDS/nanobot/actions/runs/39046574215) 已启动,目前排队中,尚未出结果。", + ].join("\n"); + + function expectReportedList(container: HTMLElement) { + expect(container.querySelectorAll("li")).toHaveLength(5); + const topItems = container.querySelectorAll(".markdown-content > ul > li"); + expect(topItems).toHaveLength(2); + expect(topItems[1].querySelectorAll("ul > li")).toHaveLength(3); + expect(topItems[1].querySelectorAll("code")).toHaveLength(4); + expect(Array.from(topItems[1].querySelectorAll("code"), (code) => code.textContent)) + .toEqual(["main", "main", "16128a60a", "--force-with-lease"]); + const prLink = topItems[1].querySelector("a"); + expect(prLink).toHaveTextContent(/^#6269$/); + expect(prLink).toHaveAttribute("href", "https://github.com/HKUDS/nanobot/pull/6269"); + expect(prLink?.closest("strong")).not.toBeNull(); + expect(topItems[1]).not.toHaveClass("list-none"); + expect(container).toHaveTextContent("已用 --force-with-lease 推送,并更新 PR 描述。"); + expect(container.querySelectorAll("a")).toHaveLength(2); + } + + describe.each([ + { label: "static", streaming: false, preserveStreamingLayout: false }, + { label: "streaming", streaming: true, preserveStreamingLayout: false }, + { label: "preserved static", streaming: false, preserveStreamingLayout: true }, + { label: "preserved streaming", streaming: true, preserveStreamingLayout: true }, + ])("$label", ({ streaming, preserveStreamingLayout }) => { + it("preserves the reported prose, emphasis, code, and three nested items", () => { + const { container } = render( + + {reportedMessage} + , + ); + + expectReportedList(container); + }); + + it("keeps prose links, multiple targets, and links in child items independent", () => { + const { container } = render( + + {[ + "- Read [the guide](https://example.com/guide) before running `setup`.", + "- Compare [first](https://example.com/first) and [second](https://example.org/second).", + "- Parent **summary**", + " - Read [child](https://example.net/child) and keep `child-code`.", + "- A title with its own URL", + " https://example.com/parent", + " - Keep this nested item too.", + ].join("\n")} + , + ); + + expect(container.querySelectorAll("li")).toHaveLength(6); + expect(container.querySelectorAll("ul ul")).toHaveLength(2); + expect(container.querySelectorAll("code")).toHaveLength(2); + expect(container.querySelector("strong")).toHaveTextContent("summary"); + expect(Array.from(container.querySelectorAll("a"), (link) => [ + link.textContent, link.getAttribute("href"), + ])).toEqual([ + ["the guide", "https://example.com/guide"], + ["first", "https://example.com/first"], + ["second", "https://example.org/second"], + ["child", "https://example.net/child"], + ["https://example.com/parent", "https://example.com/parent"], + ]); + expect(container.querySelector(".list-none")).toBeNull(); + }); + + it("does not compact same-line URLs, formatted titles, extra paragraphs, or URL commentary", () => { + const { container } = render( + + {[ + "- A title https://example.com/same-line", + "", + "- A **formatted** title", + " https://example.com/formatted", + "", + "- A paragraph title", + "", + " https://example.com/paragraph", + "", + "- A title with commentary", + " https://example.com/commentary and more prose.", + "", + "- A title with two URLs", + " https://example.com/one", + " https://example.org/two", + "", + "- A title with a labelled link", + " [label](https://example.com/labelled)", + "", + "- A title with a link tooltip", + " [https://example.com/tooltip](https://example.com/tooltip \"Keep this tooltip\")", + ].join("\n")} + , + ); + + expect(container.querySelectorAll("li")).toHaveLength(7); + expect(container.querySelectorAll("a")).toHaveLength(8); + expect(container.querySelector("strong")).toHaveTextContent("formatted"); + expect(container).toHaveTextContent("and more prose."); + expect(container.querySelector("a[href='https://example.com/labelled']")) + .toHaveTextContent(/^label$/); + expect(container.querySelector("a[href='https://example.com/tooltip']")) + .toHaveAttribute("title", "Keep this tooltip"); + expect(container.querySelector(".list-none")).toBeNull(); + }); + + it("preserves ordered numbering and both task states even for title plus URL items", () => { + const { container } = render( + + {[ + "3. Ordered title", + " https://example.com/ordered", + "4. Next ordered title", + " https://example.com/next", + "", + "- [ ] Pending title", + " https://example.com/pending", + "- [x] Completed title", + " https://example.com/completed", + ].join("\n")} + , + ); + + expect(container.querySelector("ol")).toHaveAttribute("start", "3"); + expect(container.querySelectorAll("ol > li")).toHaveLength(2); + expect(container.querySelectorAll(".task-list-item")).toHaveLength(2); + expect(Array.from(container.querySelectorAll("[data-task-checked]"), (marker) => + marker.getAttribute("data-task-checked"))).toEqual(["false", "true"]); + expect(container.querySelectorAll("a")).toHaveLength(4); + expect(container.querySelector(".list-none")).toBeNull(); + }); + + it("still compacts standalone title and single URL items without a source heading", () => { + const { container } = render( + + {[ + "- Example — “A useful title”", + " https://example.com/title", + "", + "- Another useful title", + " ", + "", + "- Hard break title ", + " https://example.net/hard-break", + ].join("\r\n")} + , + ); + + expect(screen.getByRole("link", { name: "Open link: Example — A useful title" })) + .toHaveAttribute("href", "https://example.com/title"); + expect(screen.getByRole("link", { name: "Open link: Another useful title" })) + .toHaveAttribute("href", "https://example.org/another"); + expect(screen.getByRole("link", { name: "Open link: Hard break title" })) + .toHaveAttribute("href", "https://example.net/hard-break"); + expect(container.querySelectorAll("li.list-none")).toHaveLength(3); + expect(container.querySelectorAll("img")).toHaveLength(3); + }); + }); + + it.each([false, true])("restores normal content when a preview gains more streamed content (preserve=%s)", (preserveStreamingLayout) => { + const source = "- A useful title\n https://example.com/title"; + const { container, rerender } = render( + + {source} + , + ); + expect(container.querySelector("li")).toHaveClass("list-none"); + + for (const suffix of [" and more text.", "\n https://example.org/second", "\n - Nested `code` stays visible."]) { + rerender( + + {source + suffix} + , + ); + expect(container.querySelector("li.list-none")).toBeNull(); + expect(container.querySelector("a")).toHaveTextContent(/^https:\/\/example.com\/title$/); + expect(container).toHaveTextContent("A useful title"); + } + expect(container.querySelector("ul ul code")).toHaveTextContent("code"); + }); + + it.each([false, true])("keeps the list intact as streaming finishes (preserve=%s)", (preserveStreamingLayout) => { + const { container, rerender } = render( + + {""} + , + ); + for (const end of [reportedMessage.indexOf(" - 新 head"), reportedMessage.indexOf(" - 仅保留"), reportedMessage.length]) { + rerender( + + {reportedMessage.slice(0, end)} + , + ); + expect(container.querySelector("a[href$='/6269']")).toHaveTextContent(/^#6269$/); + expect(container.querySelector("li.list-none")).toBeNull(); + } + expectReportedList(container); + rerender( + + {reportedMessage} + , + ); + expectReportedList(container); + }); + }); + it("renders media attachments without an extra preview/code wrapper", () => { render(![Diagram](/api/media/sig/payload));