Skip to content

The agent that grows with you

fix(desktop): preview-file markdown links with spaces in the path (#102782)

Konstantin Khlopkov78c7b9fda5dc
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 3a30851b264..595bea81905 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -5895,6 +5895,25 @@ function expandUserPath(filePath) {
return value
}
+/** Markdown hrefs must percent-encode spaces (`/my%20notes/x.md`), but the
+ * on-disk path is the decoded form. When the raw path misses, retry decoded
+ * so chat links to paths with spaces still open in the preview rail. */
+function decodedPathIfMissing(resolvedPath) {
+ if (!/%[0-9a-fA-F]{2}/.test(resolvedPath)) {
+ return null
+ }
+
+ let decoded
+
+ try {
+ decoded = decodeURIComponent(resolvedPath)
+ } catch {
+ return null
+ }
+
+ return decoded !== resolvedPath ? decoded : null
+}
+
async function previewFileTarget(rawTarget, baseDir) {
const raw = String(rawTarget || '').trim()
const base = baseDir ? path.resolve(expandUserPath(baseDir)) : resolveHermesCwd()
@@ -5923,6 +5942,19 @@ async function previewFileTarget(rawTarget, baseDir) {
}
}
+ // Markdown hrefs must percent-encode spaces (`/my%20notes/x.md`), but the
+ // on-disk path is the decoded form. When the encoded path misses, retry the
+ // decoded form so chat links to paths with spaces still open in the preview
+ // rail (#102782) — before the directory/missing classification below, so a
+ // decoded target is typed by what it actually is.
+ if (!fileExists(resolved) && !directoryExists(resolved)) {
+ const decoded = decodedPathIfMissing(resolved)
+
+ if (decoded && (fileExists(decoded) || directoryExists(decoded))) {
+ resolved = decoded
+ }
+ }
+
// A directory is not a preview (#101683). Overloading it as
// `<dir>/index.html` made a directory without one classify as missing, and
// the renderer's blind fallback then fabricated a broken text-preview tab.
diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.filelinks.test.tsx b/apps/desktop/src/components/assistant-ui/markdown-text.filelinks.test.tsx
index 17009655de4..c714e47cc44 100644
--- a/apps/desktop/src/components/assistant-ui/markdown-text.filelinks.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/markdown-text.filelinks.test.tsx
@@ -35,6 +35,31 @@ describe('MarkdownLink filesystem hrefs', () => {
expect(screen.getAllByRole('button', { name: 'Open preview' })).toHaveLength(2)
})
+ it('routes angle-bracket file links whose path contains spaces', async () => {
+ // CommonMark requires `<...>` destinations for paths with spaces; the
+ // old FILE_LINK_RE charset `[^\)\s]*` couldn't match them, so the link
+ // fell through to harden and rendered as "label [blocked]".
+ render(
+ <MarkdownTextContent isRunning={false} text={'See [notes](<~/My Notes/todo with spaces.md>)'} />
+ )
+
+ await screen.findByText('todo with spaces.md')
+ expect(screen.getByRole('button', { name: 'Open preview' })).toBeTruthy()
+ expect(screen.queryByText(/blocked/)).toBeNull()
+ })
+
+ it('routes percent-encoded file links through the same preview pipeline', async () => {
+ // Markdown renderers emit the href percent-encoded; the renderer keeps
+ // the encoded form in the card label, and the electron side retries the
+ // decoded on-disk path when the file is opened.
+ render(
+ <MarkdownTextContent isRunning={false} text={'See [notes](~/My%20Notes/todo%20with%20spaces.md)'} />
+ )
+
+ await screen.findByText('todo%20with%20spaces.md')
+ expect(screen.getByRole('button', { name: 'Open preview' })).toBeTruthy()
+ })
+
it('renders a media player for a media-extension path link', async () => {
const { container } = render(<MarkdownTextContent isRunning={false} text="[clip](/tmp/demo.mp4)" />)
diff --git a/apps/desktop/src/lib/markdown-preprocess.ts b/apps/desktop/src/lib/markdown-preprocess.ts
index f3400ed841e..3417fed55dd 100644
--- a/apps/desktop/src/lib/markdown-preprocess.ts
+++ b/apps/desktop/src/lib/markdown-preprocess.ts
@@ -215,9 +215,12 @@ const FENCE_TOGGLE_RE = /^[ \t]*(?:```|~~~)/
// Markdown links whose target is a filesystem path on the agent's machine:
// `[report](/home/user/report.md)`, `[notes](file:///srv/notes.txt)`,
// `[todo](~/todo.md)`, `[log](C:\logs\run.txt)`. Negative lookbehind keeps
-// image syntax (`![alt](path)`) on its existing inline pipeline. The target
-// char class excludes `)`/whitespace, matching how LLMs actually emit these.
-const FILE_LINK_RE = /(?<!!)\[(?<label>[^\]\n]+)\]\((?<target><?(?:file:\/\/|\/|~\/|[a-z]:[\\/])[^)\s]*>?)\)/gi
+// image syntax (`![alt](path)`) on its existing inline pipeline. Plain
+// targets exclude `)`/whitespace, matching how LLMs actually emit these;
+// CommonMark angle-bracket destinations (`[notes](<~/My Notes/todo.md>`) are
+// matched separately so paths with spaces route to the preview pipeline too
+// (#102782) — `routeFileLinksToPreview` strips the surrounding `<>`.
+const FILE_LINK_RE = /(?<!!)\[(?<label>[^\]\n]+)\]\((?<target>(?:<(?:file:\/\/|\/|~\/|[a-z]:[\\/])[^>]*>)|(?:file:\/\/|\/|~\/|[a-z]:[\\/])[^)\s]*)\)/gi
// A transcript directive on its own line: `::name{...}`. Attribute values are
// prose the model wrote (a task brief, a question) and read as markdown to the