Skip to content

Commit a6dbd2c

Browse files
committed
fix: Fix findUriByRef logic to use relative paths
1 parent 12ea62f commit a6dbd2c

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/commands/openDocumentByReference.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import fs from 'fs';
33
import path from 'path';
44

55
import {
6-
containsImageExt,
76
getWorkspaceCache,
87
findUriByRef,
98
ensureDirectoryExists,
109
parseRef,
10+
getWorkspaceFolder,
1111
} from '../utils';
1212

1313
const openDocumentByReference = async ({ reference }: { reference: string }) => {
@@ -17,14 +17,12 @@ const openDocumentByReference = async ({ reference }: { reference: string }) =>
1717

1818
if (uri) {
1919
await vscode.commands.executeCommand('vscode.open', uri);
20-
} else if (!containsImageExt(reference)) {
21-
// TODO: Open document regardless of extension
22-
const workspaceFolder =
23-
vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0];
20+
} else {
21+
const workspaceFolder = getWorkspaceFolder()!;
2422
if (workspaceFolder) {
2523
const paths = ref.split('/');
2624
const pathsWithExt = [...paths.slice(0, -1), `${paths.slice(-1)}.md`];
27-
const filePath = path.join(workspaceFolder.uri.fsPath, ...pathsWithExt);
25+
const filePath = path.join(workspaceFolder, ...pathsWithExt);
2826

2927
// don't override file content if it already exists
3028
if (!fs.existsSync(filePath)) {

src/utils/utils.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -358,24 +358,28 @@ export const findAllUrisWithUnknownExts = async (uris: vscode.Uri[]) => {
358358

359359
export const extractExt = (value: string) => path.parse(value).ext.replace(/^\./, '');
360360

361-
export const findUriByRef = (uris: vscode.Uri[], ref: string): vscode.Uri | undefined =>
362-
uris.find((uri) => {
361+
export const findUriByRef = (uris: vscode.Uri[], ref: string): vscode.Uri | undefined => {
362+
return uris.find((uri) => {
363+
const relativeFsPath =
364+
path.sep + path.relative(getWorkspaceFolder()!.toLowerCase(), uri.fsPath.toLowerCase());
365+
363366
if (containsImageExt(ref) || containsOtherKnownExts(ref) || containsUnknownExt(ref)) {
364367
if (isLongRef(ref)) {
365-
return normalizeSlashes(uri.fsPath.toLowerCase()).endsWith(ref.toLowerCase());
368+
return normalizeSlashes(relativeFsPath).endsWith(ref.toLowerCase());
366369
}
367370

368371
return path.basename(uri.fsPath).toLowerCase() === ref.toLowerCase();
369372
}
370373

371374
if (isLongRef(ref)) {
372-
return normalizeSlashes(uri.fsPath.toLowerCase()).endsWith(`${ref.toLowerCase()}.md`);
375+
return normalizeSlashes(relativeFsPath).endsWith(`${ref.toLowerCase()}.md`);
373376
}
374377

375378
const name = path.parse(uri.fsPath).name.toLowerCase();
376379

377380
return containsMarkdownExt(path.basename(uri.fsPath)) && name === ref.toLowerCase();
378381
});
382+
};
379383

380384
export const ensureDirectoryExists = (filePath: string) => {
381385
const dirname = path.dirname(filePath);

0 commit comments

Comments
 (0)