From a6dbd2c38af470499e364f29425eb82a0df51b6c Mon Sep 17 00:00:00 2001 From: Svyat Sobol Date: Wed, 5 Aug 2020 19:35:11 +0300 Subject: [PATCH] fix: Fix findUriByRef logic to use relative paths --- src/commands/openDocumentByReference.ts | 10 ++++------ src/utils/utils.ts | 12 ++++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/commands/openDocumentByReference.ts b/src/commands/openDocumentByReference.ts index e60fd5ad..41ec5354 100644 --- a/src/commands/openDocumentByReference.ts +++ b/src/commands/openDocumentByReference.ts @@ -3,11 +3,11 @@ import fs from 'fs'; import path from 'path'; import { - containsImageExt, getWorkspaceCache, findUriByRef, ensureDirectoryExists, parseRef, + getWorkspaceFolder, } from '../utils'; const openDocumentByReference = async ({ reference }: { reference: string }) => { @@ -17,14 +17,12 @@ const openDocumentByReference = async ({ reference }: { reference: string }) => if (uri) { await vscode.commands.executeCommand('vscode.open', uri); - } else if (!containsImageExt(reference)) { - // TODO: Open document regardless of extension - const workspaceFolder = - vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]; + } else { + const workspaceFolder = getWorkspaceFolder()!; if (workspaceFolder) { const paths = ref.split('/'); const pathsWithExt = [...paths.slice(0, -1), `${paths.slice(-1)}.md`]; - const filePath = path.join(workspaceFolder.uri.fsPath, ...pathsWithExt); + const filePath = path.join(workspaceFolder, ...pathsWithExt); // don't override file content if it already exists if (!fs.existsSync(filePath)) { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index f9902d90..a66a627a 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -358,24 +358,28 @@ export const findAllUrisWithUnknownExts = async (uris: vscode.Uri[]) => { export const extractExt = (value: string) => path.parse(value).ext.replace(/^\./, ''); -export const findUriByRef = (uris: vscode.Uri[], ref: string): vscode.Uri | undefined => - uris.find((uri) => { +export const findUriByRef = (uris: vscode.Uri[], ref: string): vscode.Uri | undefined => { + return uris.find((uri) => { + const relativeFsPath = + path.sep + path.relative(getWorkspaceFolder()!.toLowerCase(), uri.fsPath.toLowerCase()); + if (containsImageExt(ref) || containsOtherKnownExts(ref) || containsUnknownExt(ref)) { if (isLongRef(ref)) { - return normalizeSlashes(uri.fsPath.toLowerCase()).endsWith(ref.toLowerCase()); + return normalizeSlashes(relativeFsPath).endsWith(ref.toLowerCase()); } return path.basename(uri.fsPath).toLowerCase() === ref.toLowerCase(); } if (isLongRef(ref)) { - return normalizeSlashes(uri.fsPath.toLowerCase()).endsWith(`${ref.toLowerCase()}.md`); + return normalizeSlashes(relativeFsPath).endsWith(`${ref.toLowerCase()}.md`); } const name = path.parse(uri.fsPath).name.toLowerCase(); return containsMarkdownExt(path.basename(uri.fsPath)) && name === ref.toLowerCase(); }); +}; export const ensureDirectoryExists = (filePath: string) => { const dirname = path.dirname(filePath);