Skip to content

Commit

Permalink
fix: update parser to accept pkg: links
Browse files Browse the repository at this point in the history
findDocumentLinks2 returns proper URLs without pkg:, but I made
some assumptions when comparing the import string to the file
URL.
  • Loading branch information
wkillerud committed Feb 16, 2024
1 parent 86e7be3 commit c6a5e1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 3 additions & 0 deletions server/src/parser/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export function getLanguageService(): LanguageService {
};
}
},
getContent(uri, encoding) {
return fs.readFile(URI.parse(uri), encoding);
},
};

ls = getSCSSLanguageService({ fileSystemProvider, clientCapabilities });
Expand Down
10 changes: 6 additions & 4 deletions server/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c6a5e1d

Please sign in to comment.