Skip to content

Commit 834f0e3

Browse files
SunkenInTimeOhadCclaude
authored
fix(markdown): Windows paths in messages keep their backslashes (pingdotgg#21069)
Co-authored-by: OhadC <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d6841e9 commit 834f0e3

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ThreadFindTimelineContext } from "./ThreadFindProvider";
22
import { shallow } from "zustand/vanilla/shallow";
3-
import { shouldPreserveAssistantLineBreaks } from "@t3tools/shared/markdownPipeline";
3+
import {
4+
remarkKeepWindowsPaths,
5+
shouldPreserveAssistantLineBreaks,
6+
} from "@t3tools/shared/markdownPipeline";
47
import { MarkdownFindContext, findRevealRef } from "./markdownFindContext";
58
import { useChatCanvas } from "./ChatCanvasContext";
69
import { WorkLogBlock, WorkLogButton, WorkLogDetails, WorkLogList, WorkLogRow } from "./WorkLog";
@@ -3850,6 +3853,7 @@ function LiveWorkEntryTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "
38503853
<ReactMarkdown
38513854
remarkPlugins={[
38523855
remarkGfm,
3856+
remarkKeepWindowsPaths,
38533857
[remarkThoughtPreview, row.active ? "Thinking" : "Thought"],
38543858
]}
38553859
>
@@ -5480,6 +5484,7 @@ function WorkEntryLogRow(props: WorkEntryRowProps) {
54805484
<ReactMarkdown
54815485
remarkPlugins={[
54825486
remarkGfm,
5487+
remarkKeepWindowsPaths,
54835488
[
54845489
remarkThoughtPreview,
54855490
workEntry.toolLifecycleStatus === "inProgress" ? "Thinking" : "Thought",

‎packages/shared/src/markdownPipeline.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,31 @@ it("preserves in-app thread links through the shared Markdown sanitizer", () =>
2727
});
2828
expect(JSON.stringify(tree)).not.toContain("javascript:");
2929
});
30+
31+
it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
32+
const processor = unified()
33+
.use(remarkParse)
34+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
35+
.use(remarkRehype, { allowDangerousHtml: true })
36+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
37+
type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
38+
const text = (node: Node): string =>
39+
node.type === "text"
40+
? (node.value ?? "")
41+
: node.tagName === "code"
42+
? `<code>${(node.children ?? []).map(text).join("")}</code>`
43+
: (node.children ?? []).map(text).join("");
44+
const render = (markdown: string) => text(processor.runSync(processor.parse(markdown)) as Node);
45+
46+
expect(
47+
render(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`),
48+
).toBe(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`);
49+
// Paths render as written, so one an agent already escaped keeps its doubled backslashes.
50+
expect(render(String.raw`Escaped C:\\Users\\me`)).toBe(String.raw`Escaped C:\\Users\\me`);
51+
expect(render(String.raw`See C:\x and \*this\* and Note:\*that\*.`)).toBe(
52+
String.raw`See C:\x and *this* and Note:*that*.`,
53+
);
54+
expect(render(String.raw`${"`"}C:\me\.t3${"`"} and C:\me\.t3`)).toBe(
55+
String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,
56+
);
57+
});

‎packages/shared/src/markdownPipeline.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
158158

159159
export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
160160
remarkGfm,
161-
remarkKeepWindowsPathDestinations,
161+
remarkKeepWindowsPaths,
162162
remarkGithubAlerts,
163163
remarkNormalizeListItemIndentation,
164164
remarkCodexDirectives,
@@ -168,7 +168,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
168168

169169
export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [
170170
remarkGfm,
171-
remarkKeepWindowsPathDestinations,
171+
remarkKeepWindowsPaths,
172172
remarkGithubAlerts,
173173
remarkNormalizeListItemIndentation,
174174
remarkCodexDirectives,
@@ -255,17 +255,37 @@ function keepWindowsPathDestination(this: DestinationCompileContext, token: unkn
255255
node.url = isWindowsAbsolutePath(authored) && !authored.includes("&") ? authored : decoded;
256256
}
257257

258+
// A drive path that has not reached whitespace yet: `C:`, `C:\Users\me`, `C:/work`.
259+
const UNFINISHED_DRIVE_PATH = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/;
260+
261+
interface EscapeCompileContext {
262+
readonly stack: ReadonlyArray<{
263+
readonly type: string;
264+
children?: Array<{ type: string; value?: string }>;
265+
}>;
266+
}
267+
268+
function keepWindowsPathEscape(this: EscapeCompileContext) {
269+
const text = this.stack.at(-1)?.children?.at(-1);
270+
if (text?.type !== "text" || text.value === undefined) return;
271+
const before = text.value.slice(0, -1);
272+
if (UNFINISHED_DRIVE_PATH.test(before)) text.value = `${before}\\${text.value.slice(-1)}`;
273+
}
274+
258275
/**
259-
* CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape, even in a link
260-
* destination. Every backslash in a Windows path is a separator, so link, image, and
261-
* definition destinations that are Windows paths keep the text as written.
276+
* CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape. Every backslash in a
277+
* Windows path is a separator, so link, image, and definition destinations that are
278+
* Windows paths keep the text as written, and so do escapes inside drive paths in prose.
279+
* Prose paths end at whitespace: past that, nothing tells a path from a sentence, and
280+
* `C:\x and \*this\*` must still read `*this*`.
262281
*/
263-
function remarkKeepWindowsPathDestinations(this: Processor) {
282+
export function remarkKeepWindowsPaths(this: Processor) {
264283
const data = this.data();
265284
(data.fromMarkdownExtensions ??= []).push({
266285
exit: {
267286
resourceDestinationString: keepWindowsPathDestination,
268287
definitionDestinationString: keepWindowsPathDestination,
288+
characterEscape: keepWindowsPathEscape,
269289
},
270290
});
271291
}

0 commit comments

Comments
 (0)