Skip to content

Commit

Permalink
fix: Fix findUriByRef logic to use relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Aug 5, 2020
1 parent 12ea62f commit a6dbd2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/commands/openDocumentByReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -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)) {
Expand Down
12 changes: 8 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a6dbd2c

Please sign in to comment.