TypeScript
fix(markdown): Windows paths in messages keep their backslashes (#21069)
Dara Adedeji834f0e3a1b3d
diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 166cbf0f850..66c04d58f96 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -1,6 +1,9 @@import { ThreadFindTimelineContext } from "./ThreadFindProvider";import { shallow } from "zustand/vanilla/shallow";-import { shouldPreserveAssistantLineBreaks } from "@t3tools/shared/markdownPipeline";+import {+ remarkKeepWindowsPaths,+ shouldPreserveAssistantLineBreaks,+} from "@t3tools/shared/markdownPipeline";import { MarkdownFindContext, findRevealRef } from "./markdownFindContext";import { useChatCanvas } from "./ChatCanvasContext";import { WorkLogBlock, WorkLogButton, WorkLogDetails, WorkLogList, WorkLogRow } from "./WorkLog";@@ -3850,6 +3853,7 @@ function LiveWorkEntryTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "<ReactMarkdownremarkPlugins={[remarkGfm,+ remarkKeepWindowsPaths,[remarkThoughtPreview, row.active ? "Thinking" : "Thought"],]}>@@ -5480,6 +5484,7 @@ function WorkEntryLogRow(props: WorkEntryRowProps) {<ReactMarkdownremarkPlugins={[remarkGfm,+ remarkKeepWindowsPaths,[remarkThoughtPreview,workEntry.toolLifecycleStatus === "inProgress" ? "Thinking" : "Thought",diff --git a/packages/shared/src/markdownPipeline.test.ts b/packages/shared/src/markdownPipeline.test.tsindex 1227e77b942..b546fcc57c6 100644--- a/packages/shared/src/markdownPipeline.test.ts+++ b/packages/shared/src/markdownPipeline.test.ts@@ -27,3 +27,31 @@ it("preserves in-app thread links through the shared Markdown sanitizer", () =>});expect(JSON.stringify(tree)).not.toContain("javascript:");});++it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {+ 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 text = (node: Node): string =>+ node.type === "text"+ ? (node.value ?? "")+ : node.tagName === "code"+ ? `<code>${(node.children ?? []).map(text).join("")}</code>`+ : (node.children ?? []).map(text).join("");+ const render = (markdown: string) => text(processor.runSync(processor.parse(markdown)) as Node);++ expect(+ render(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`),+ ).toBe(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`);+ // Paths render as written, so one an agent already escaped keeps its doubled backslashes.+ expect(render(String.raw`Escaped C:\\Users\\me`)).toBe(String.raw`Escaped C:\\Users\\me`);+ expect(render(String.raw`See C:\x and \*this\* and Note:\*that\*.`)).toBe(+ String.raw`See C:\x and *this* and Note:*that*.`,+ );+ expect(render(String.raw`${"`"}C:\me\.t3${"`"} and C:\me\.t3`)).toBe(+ String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,+ );+});diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.tsindex ac1850499f3..d8717c6273f 100644--- a/packages/shared/src/markdownPipeline.ts+++ b/packages/shared/src/markdownPipeline.ts@@ -158,7 +158,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [remarkGfm,- remarkKeepWindowsPathDestinations,+ remarkKeepWindowsPaths,remarkGithubAlerts,remarkNormalizeListItemIndentation,remarkCodexDirectives,@@ -168,7 +168,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [remarkGfm,- remarkKeepWindowsPathDestinations,+ remarkKeepWindowsPaths,remarkGithubAlerts,remarkNormalizeListItemIndentation,remarkCodexDirectives,@@ -255,17 +255,37 @@ function keepWindowsPathDestination(this: DestinationCompileContext, token: unknnode.url = isWindowsAbsolutePath(authored) && !authored.includes("&") ? authored : decoded;}+// A drive path that has not reached whitespace yet: `C:`, `C:\Users\me`, `C:/work`.+const UNFINISHED_DRIVE_PATH = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/;++interface EscapeCompileContext {+ readonly stack: ReadonlyArray<{+ readonly type: string;+ children?: Array<{ type: string; value?: string }>;+ }>;+}++function keepWindowsPathEscape(this: EscapeCompileContext) {+ const text = this.stack.at(-1)?.children?.at(-1);+ if (text?.type !== "text" || text.value === undefined) return;+ const before = text.value.slice(0, -1);+ if (UNFINISHED_DRIVE_PATH.test(before)) text.value = `${before}\\${text.value.slice(-1)}`;+}+/**- * CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape, even in a link- * destination. Every backslash in a Windows path is a separator, so link, image, and- * definition destinations that are Windows paths keep the text as written.+ * CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape. Every backslash in a+ * Windows path is a separator, so link, image, and definition destinations that are+ * Windows paths keep the text as written, and so do escapes inside drive paths in prose.+ * Prose paths end at whitespace: past that, nothing tells a path from a sentence, and+ * `C:\x and \*this\*` must still read `*this*`.*/-function remarkKeepWindowsPathDestinations(this: Processor) {+export function remarkKeepWindowsPaths(this: Processor) {const data = this.data();(data.fromMarkdownExtensions ??= []).push({exit: {resourceDestinationString: keepWindowsPathDestination,definitionDestinationString: keepWindowsPathDestination,+ characterEscape: keepWindowsPathEscape,},});}