fix: resolve windows markdown drive links

This commit is contained in:
Neil
2026-05-31 09:56:18 -07:00
committed by GitHub
parent 20d4bbd791
commit 3e77178144
4 changed files with 37 additions and 0 deletions
@@ -42,6 +42,19 @@ describe('resolveMarkdownLinkTarget', () => {
expect(r?.kind).toBe('markdown')
})
it('classifies Windows drive-letter absolute .md links inside worktree as markdown', () => {
const r = resolveMarkdownLinkTarget(
'C:\\repo\\docs\\guide.md',
'C:\\repo\\docs\\note.md',
'C:\\repo'
)
expect(r).toEqual({
kind: 'markdown',
absolutePath: 'C:/repo/docs/guide.md',
relativePath: 'docs/guide.md'
})
})
it('extracts line from #L10', () => {
const r = resolveMarkdownLinkTarget('./guide.md#L10', SOURCE, ROOT)
expect(r).toMatchObject({ kind: 'markdown', line: 10, column: undefined })
@@ -1,4 +1,5 @@
import { filesystemPathToFileUri, fileUriToFilesystemPath } from '../../../../shared/file-uri-path'
import { isWindowsAbsolutePathLike } from '../../../../shared/cross-platform-path'
// Pure classifier for markdown link targets. Called by the link-activation
// dispatcher (activateMarkdownLink slice action) from three call sites —
@@ -108,6 +109,11 @@ function extractHashLineCol(hash: string): { line?: number; column?: number } {
function resolveRelativeToSource(rawHref: string, sourceFilePath: string): URL | null {
try {
if (isWindowsAbsolutePathLike(rawHref)) {
// Why: URL treats `C:\...` as a custom `c:` scheme unless the Windows
// absolute path is first converted to the file URL form used downstream.
return new URL(filesystemPathToFileUri(rawHref))
}
return new URL(rawHref, toFileUrl(sourceFilePath))
} catch {
return null
@@ -48,6 +48,12 @@ describe('getMarkdownPreviewImageSrc', () => {
)
})
it('resolves Windows drive-letter absolute image paths', () => {
expect(
getMarkdownPreviewImageSrc('C:\\repo\\assets\\diagram.png', '/repo/docs/README.md')
).toBe('file:///C:/repo/assets/diagram.png')
})
it('resolves relative paths for Windows UNC markdown files', () => {
expect(
getMarkdownPreviewImageSrc('./diagram.png', '\\\\server\\share\\repo\\docs\\README.md')
@@ -113,6 +119,12 @@ describe('resolveImageAbsolutePath', () => {
)
})
it('resolves Windows drive-letter absolute image paths to filesystem paths', () => {
expect(resolveImageAbsolutePath('C:\\repo\\assets\\diagram.png', '/repo/docs/README.md')).toBe(
'C:/repo/assets/diagram.png'
)
})
it('resolves Windows UNC paths without dropping the server name', () => {
expect(
resolveImageAbsolutePath('./diagram.png', '\\\\server\\share\\repo\\docs\\README.md')
@@ -1,4 +1,5 @@
import { filesystemPathToFileUri, fileUriToFilesystemPath } from '../../../../shared/file-uri-path'
import { isWindowsAbsolutePathLike } from '../../../../shared/cross-platform-path'
function toFileUrl(filePath: string): string {
return filesystemPathToFileUri(filePath)
@@ -10,6 +11,11 @@ export function resolveMarkdownPreviewHref(rawUrl: string, filePath: string): UR
}
try {
if (isWindowsAbsolutePathLike(rawUrl)) {
// Why: URL treats `C:\...` as a custom `c:` scheme unless we first
// normalize the drive path into the file URL form markdown previews use.
return new URL(filesystemPathToFileUri(rawUrl))
}
return new URL(rawUrl, toFileUrl(filePath))
} catch {
return null