From c6a5e1dc6a148e4391b1733a0bfbe02a85742c45 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Fri, 16 Feb 2024 22:20:19 +0100 Subject: [PATCH] fix: update parser to accept pkg: links findDocumentLinks2 returns proper URLs without pkg:, but I made some assumptions when comparing the import string to the file URL. --- server/src/parser/language-service.ts | 3 +++ server/src/parser/parser.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/server/src/parser/language-service.ts b/server/src/parser/language-service.ts index ea2d3929..0ea783e3 100644 --- a/server/src/parser/language-service.ts +++ b/server/src/parser/language-service.ts @@ -34,6 +34,9 @@ export function getLanguageService(): LanguageService { }; } }, + getContent(uri, encoding) { + return fs.readFile(URI.parse(uri), encoding); + }, }; ls = getSCSSLanguageService({ fileSystemProvider, clientCapabilities }); diff --git a/server/src/parser/parser.ts b/server/src/parser/parser.ts index 600929b2..05db82bd 100644 --- a/server/src/parser/parser.ts +++ b/server/src/parser/parser.ts @@ -362,19 +362,21 @@ function ensureIndex(target: string): string { return `${path}/${fileName}/index${extension}`; } -function urlMatches(url: string, linkTarget: string): boolean { - let safeUrl = url; +function urlMatches(importString: string, fileUrl: string): boolean { + let safeUrl = importString.replace("pkg:", ""); while (/^[./@~]/.exec(safeUrl)) { safeUrl = safeUrl.slice(1); } - let match = linkTarget.includes(safeUrl); + // With pkg: and subpatch exports, the URL doesn't necessarily match the import string, + // but it should contain all parts of it. + let match = safeUrl.split("/").every((part) => fileUrl.includes(part)); if (!match) { const lastSlash = safeUrl.lastIndexOf("/"); const toLastSlash = safeUrl.slice(0, Math.max(0, lastSlash)); const restOfUrl = safeUrl.slice(Math.max(0, lastSlash + 1)); const partial = `${toLastSlash}/_${restOfUrl}`; - match = linkTarget.includes(partial); + match = fileUrl.includes(partial); } return match;