Skip to content

Commit 725c2e4

Browse files
fix(web): read minimap previews through the markdown parser
The regex cleanup also rewrote code spans and fenced code, split table cells on escaped pipes, and stripped literal "[x]" outside task lists. Parse the assistant text with remark and GFM, the same syntax the chat renders, and read its text nodes instead.
1 parent f9d5307 commit 725c2e4

4 files changed

Lines changed: 59 additions & 22 deletions

File tree

‎apps/web/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@
5959
"rehype-sanitize": "^6.0.0",
6060
"remark-breaks": "^4.0.0",
6161
"remark-gfm": "^4.0.1",
62+
"remark-parse": "^11.0.0",
6263
"tailwind-merge": "^3.4.0",
64+
"unified": "^11.0.5",
6365
"zustand": "^5.0.11"
6466
},
6567
"devDependencies": {
@@ -82,7 +84,6 @@
8284
"jsdom": "^30.0.1",
8385
"react-test-renderer": "19.2.6",
8486
"tailwindcss": "^4.0.0",
85-
"unified": "^11.0.5",
8687
"vite": "catalog:",
8788
"vite-plus": "catalog:"
8889
}

‎apps/web/src/components/chat/timelineMinimapItems.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ describe("timeline minimap previews", () => {
9494
});
9595
});
9696

97+
it("keeps code, escaped table pipes, and literal checkboxes as written", () => {
98+
const preview = (markdown: string) =>
99+
resolveTimelineMinimapPreview(
100+
deriveTimelineMinimapItems(
101+
rows([
102+
["user", "Q"],
103+
["assistant", markdown],
104+
]),
105+
)[0]!,
106+
)?.assistantText;
107+
108+
expect(preview("Match `**/*.ts` files\n\n```sh\n# install deps\n- **not bold**\n```")).toBe(
109+
"Match **/*.ts files # install deps - **not bold**",
110+
);
111+
expect(preview("| Operator | Meaning |\n| --- | --- |\n| `a \\| b` | either |")).toBe(
112+
"Operator Meaning a | b either",
113+
);
114+
expect(preview("[x] deployment complete")).toBe("[x] deployment complete");
115+
});
116+
97117
it("shows fresh streaming text without changing the jump target", () => {
98118
const first = deriveTimelineMinimapItems(
99119
rows([

‎apps/web/src/components/chat/timelineMinimapItems.ts‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import type { Nodes } from "mdast";
2+
import remarkGfm from "remark-gfm";
3+
import remarkParse from "remark-parse";
4+
import { unified } from "unified";
5+
16
import type { MessagesTimelineRow } from "./MessagesTimeline.logic";
27

38
export interface TimelineMinimapItem {
@@ -48,23 +53,31 @@ function resolveFinalAssistantTextForTurn(
4853
return finalAssistantText;
4954
}
5055

51-
/** Drops markdown syntax so table, list, and link sources read as plain preview text. */
52-
function stripMarkdownForPreview(text: string) {
53-
return (
54-
text
55-
// Code fence lines, then table delimiter rows and thematic breaks.
56-
.replace(/^[ \t]*(?:```|~~~).*$/gm, "")
57-
.replace(/^[ \t]*\|?[ \t]*:?-{3,}:?[ \t]*(?:\|[ \t]*:?-{3,}:?[ \t]*)*\|?[ \t]*$/gm, "")
58-
// Table rows keep their cell text.
59-
.replace(/^[ \t]*\|(.*?)\|?[ \t]*$/gm, (_, cells: string) => cells.replace(/\s*\|\s*/g, " "))
60-
// Blockquote, heading, list, and task markers at the start of a line.
61-
.replace(/^[ \t]*(?:>[ \t]*)*(?:(?:#{1,6}|[-*+]|\d+[.)])[ \t]+)?(?:\[[ xX]\][ \t]+)?/gm, "")
62-
// Links and images keep their label.
63-
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
64-
.replace(/`+([^`]+)`+/g, "$1")
65-
// Paired bold and strikethrough only; single `*` and `_` are common in identifiers.
66-
.replace(/(\*\*|__|~~)(\S(?:.*?\S)?)\1/g, "$2")
67-
);
56+
const markdownPreviewParser = unified().use(remarkParse).use(remarkGfm).freeze();
57+
58+
const INLINE_PARENT_TYPES = new Set<Nodes["type"]>([
59+
"paragraph",
60+
"heading",
61+
"tableCell",
62+
"emphasis",
63+
"strong",
64+
"delete",
65+
"link",
66+
"linkReference",
67+
]);
68+
69+
function markdownNodeText(node: Nodes): string {
70+
if (node.type === "html") return "";
71+
if ("value" in node) return node.value;
72+
if (node.type === "image" || node.type === "imageReference") return node.alt ?? "";
73+
if (!("children" in node)) return "";
74+
const separator = INLINE_PARENT_TYPES.has(node.type) ? "" : " ";
75+
return node.children.map((child: Nodes) => markdownNodeText(child)).join(separator);
76+
}
77+
78+
/** Reads assistant markdown as rendered text so previews never show table pipes or link syntax. */
79+
function markdownPreviewText(markdown: string) {
80+
return markdownNodeText(markdownPreviewParser.parse(markdown));
6881
}
6982

7083
function compactMinimapPreview(text: string | null | undefined) {
@@ -81,7 +94,7 @@ export function resolveTimelineMinimapPreview(
8194
...item,
8295
userText: compactMinimapPreview(item.userText),
8396
assistantText: compactMinimapPreview(
84-
item.assistantText === null ? null : stripMarkdownForPreview(item.assistantText),
97+
item.assistantText === null ? null : markdownPreviewText(item.assistantText),
8598
),
8699
};
87100
}

‎pnpm-lock.yaml‎

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)