Skip to content

Commit

Permalink
fix: Fix findUriByRef for long links with dots (#346, #339)
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Apr 27, 2021
1 parent 864ce71 commit 6ec7cca
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
84 changes: 84 additions & 0 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,60 @@ describe('getReferenceAtPosition()', () => {
}
`);
});

it('should get reference at position for a short link with dots', async () => {
expect(
getReferenceAtPosition(
await workspace.openTextDocument({
language: 'markdown',
content: '![[test-v1.1.0]]',
}),
new Position(0, 4),
),
).toMatchInlineSnapshot(`
Object {
"label": "",
"range": Array [
Object {
"character": 1,
"line": 0,
},
Object {
"character": 16,
"line": 0,
},
],
"ref": "test-v1.1.0",
}
`);
});

it('should get reference at position for a long link with dots', async () => {
expect(
getReferenceAtPosition(
await workspace.openTextDocument({
language: 'markdown',
content: '![[test/test-v1.1.0]]',
}),
new Position(0, 4),
),
).toMatchInlineSnapshot(`
Object {
"label": "",
"range": Array [
Object {
"character": 1,
"line": 0,
},
Object {
"character": 21,
"line": 0,
},
],
"ref": "test/test-v1.1.0",
}
`);
});
});

describe('escapeForRegExp()', () => {
Expand Down Expand Up @@ -1492,6 +1546,36 @@ describe('findUriByRef()', () => {
toPlainObject(await findUriByRef([Uri.file('/Users/Memo/Diary/.md')], '.md')),
).toBeUndefined();
});

it('should find uri by ref for a short link with dots', async () => {
expect(
toPlainObject(
await findUriByRef([Uri.file('/Users/Memo/Diary/test/test-v1.1.0.md')], 'test-v1.1.0'),
),
).toEqual(
expect.objectContaining({
$mid: 1,
fsPath: expect.toEndWith('test-v1.1.0.md'),
path: expect.toEndWith('test-v1.1.0.md'),
scheme: 'file',
}),
);
});

it('should find uri by ref for a long link with dots', async () => {
expect(
toPlainObject(
await findUriByRef([Uri.file('/Users/Memo/Diary/test/test-v1.1.0.md')], 'test/test-v1.1.0'),
),
).toEqual(
expect.objectContaining({
$mid: 1,
fsPath: expect.toEndWith('test-v1.1.0.md'),
path: expect.toEndWith('test-v1.1.0.md'),
scheme: 'file',
}),
);
});
});

describe('ensureDirectoryExists()', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ export const findUriByRef = (uris: vscode.Uri[], ref: string): vscode.Uri | unde

if (containsImageExt(ref) || containsOtherKnownExts(ref) || containsUnknownExt(ref)) {
if (isLongRef(ref)) {
return normalizeSlashes(relativeFsPath).endsWith(ref.toLowerCase());
return (
normalizeSlashes(relativeFsPath).endsWith(ref.toLowerCase()) ||
normalizeSlashes(relativeFsPath).endsWith(`${ref.toLowerCase()}.md`)
);
}

const basenameLowerCased = path.basename(uri.fsPath).toLowerCase();
Expand Down

0 comments on commit 6ec7cca

Please sign in to comment.