fix: ignore malformed markdown file links (#3695)

This commit is contained in:
Neil
2026-05-30 06:27:43 -07:00
committed by GitHub
parent b21365e858
commit 7405f5dc4b
2 changed files with 14 additions and 2 deletions
@@ -123,6 +123,10 @@ describe('resolveMarkdownLinkTarget', () => {
})
})
it('returns null for malformed percent-encoded file URL paths', () => {
expect(resolveMarkdownLinkTarget('file:///repo/docs/%zz.md', SOURCE, ROOT)).toBeNull()
})
it('returns null for empty href', () => {
expect(resolveMarkdownLinkTarget('', SOURCE, ROOT)).toBeNull()
expect(resolveMarkdownLinkTarget(undefined, SOURCE, ROOT)).toBeNull()
@@ -51,8 +51,13 @@ function toFileUrl(filePath: string): string {
return `file:///${segments.join('/')}`
}
function fileUrlToAbsolutePath(url: URL): string {
let absolutePath = decodeURIComponent(url.pathname)
function fileUrlToAbsolutePath(url: URL): string | null {
let absolutePath: string
try {
absolutePath = decodeURIComponent(url.pathname)
} catch {
return null
}
// Windows: "/C:/foo" → "C:/foo"
if (/^\/[A-Za-z]:\//.test(absolutePath)) {
absolutePath = absolutePath.slice(1)
@@ -175,6 +180,9 @@ export function resolveMarkdownLinkTarget(
}
const rawAbsolutePath = fileUrlToAbsolutePath(resolved)
if (rawAbsolutePath === null) {
return null
}
// Why: hash-based line anchor takes precedence; fall back to trailing
// `:line:col` syntax only if no hash anchor was found.