Skip to content

Commit 4cb7436

Browse files
committed
fix: Don't add extra md extension if link has an extension
1 parent e9476f1 commit 4cb7436

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed

src/commands/openDocumentByReference.spec.ts

+66-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { commands } from 'vscode';
21
import path from 'path';
2+
import { commands } from 'vscode';
33

4+
import openDocumentByReference from './openDocumentByReference';
45
import {
56
createFile,
67
rndName,
78
getWorkspaceFolder,
89
getOpenedFilenames,
910
getOpenedPaths,
1011
closeEditorsAndCleanWorkspace,
12+
toPlainObject,
1113
} from '../test/testUtils';
1214

1315
describe('openDocumentByReference command', () => {
@@ -21,31 +23,31 @@ describe('openDocumentByReference command', () => {
2123

2224
await createFile(filename);
2325

24-
await commands.executeCommand('_memo.openDocumentByReference', { reference: name });
26+
await openDocumentByReference({ reference: name });
2527

26-
expect(getOpenedFilenames()).toContain(filename);
28+
expect(getOpenedPaths()).toContain(`${path.join(getWorkspaceFolder()!, filename)}`);
2729
});
2830

2931
it('should create a new text document if does not exist yet', async () => {
3032
const name = rndName();
3133

3234
expect(getOpenedFilenames()).not.toContain(`${name}.md`);
3335

34-
await commands.executeCommand('_memo.openDocumentByReference', { reference: name });
36+
await openDocumentByReference({ reference: name });
3537

36-
expect(getOpenedFilenames()).toContain(`${name}.md`);
38+
expect(getOpenedPaths()).toContain(`${path.join(getWorkspaceFolder()!, `${name}.md`)}`);
3739
});
3840

3941
it('should open a text document from a reference with label', async () => {
4042
const name = rndName();
4143

4244
expect(getOpenedFilenames()).not.toContain(`${name}.md`);
4345

44-
await commands.executeCommand('_memo.openDocumentByReference', {
46+
await openDocumentByReference({
4547
reference: `${name}|Test Label`,
4648
});
4749

48-
expect(getOpenedFilenames()).toContain(`${name}.md`);
50+
expect(getOpenedPaths()).toContain(`${path.join(getWorkspaceFolder()!, `${name}.md`)}`);
4951
});
5052

5153
it('should not open a reference on inexact filename match', async () => {
@@ -54,7 +56,7 @@ describe('openDocumentByReference command', () => {
5456

5557
await createFile(filename);
5658

57-
await commands.executeCommand('_memo.openDocumentByReference', { reference: 'test' });
59+
await openDocumentByReference({ reference: 'test' });
5860

5961
expect(getOpenedFilenames()).not.toContain(filename);
6062
});
@@ -65,11 +67,11 @@ describe('openDocumentByReference command', () => {
6567

6668
await createFile(filename);
6769

68-
await commands.executeCommand('_memo.openDocumentByReference', {
70+
await openDocumentByReference({
6971
reference: name.toUpperCase(),
7072
});
7173

72-
expect(getOpenedFilenames()).toContain(filename);
74+
expect(getOpenedPaths()).toContain(`${path.join(getWorkspaceFolder()!, filename)}`);
7375
});
7476

7577
it('should open document by a long ref', async () => {
@@ -79,7 +81,7 @@ describe('openDocumentByReference command', () => {
7981
await createFile(filename);
8082
await createFile(`/folder1/${filename}`);
8183

82-
await commands.executeCommand('_memo.openDocumentByReference', {
84+
await openDocumentByReference({
8385
reference: `/folder1/${name}`,
8486
});
8587

@@ -92,7 +94,7 @@ describe('openDocumentByReference command', () => {
9294
await createFile(`/a/${name}.png`);
9395
await createFile(`/b/${name}.md`);
9496

95-
await commands.executeCommand('_memo.openDocumentByReference', {
97+
await openDocumentByReference({
9698
reference: name,
9799
});
98100

@@ -102,7 +104,7 @@ describe('openDocumentByReference command', () => {
102104
it('should create note automatically including folder if does not exist yet', async () => {
103105
const name = rndName();
104106

105-
await commands.executeCommand('_memo.openDocumentByReference', {
107+
await openDocumentByReference({
106108
reference: `folder1/folder2/${name}`,
107109
});
108110

@@ -114,12 +116,62 @@ describe('openDocumentByReference command', () => {
114116
it('should create note automatically even with leading slash in the reference', async () => {
115117
const name = rndName();
116118

117-
await commands.executeCommand('_memo.openDocumentByReference', {
119+
await openDocumentByReference({
118120
reference: `/folder1/${name}`,
119121
});
120122

121123
expect(getOpenedPaths()).toContain(
122124
`${path.join(getWorkspaceFolder()!, 'folder1', `${name}.md`)}`,
123125
);
124126
});
127+
128+
it('should open png ref with .png extension', async () => {
129+
const name = rndName();
130+
131+
const executeCommandSpy = jest.spyOn(commands, 'executeCommand');
132+
133+
await openDocumentByReference({
134+
reference: `${name}.png`,
135+
});
136+
137+
expect(
138+
toPlainObject(executeCommandSpy.mock.calls.filter(([command]) => command === 'vscode.open')),
139+
).toMatchObject([
140+
[
141+
'vscode.open',
142+
expect.objectContaining({
143+
$mid: 1,
144+
path: path.join(getWorkspaceFolder()!, `${name}.png`),
145+
scheme: 'file',
146+
}),
147+
],
148+
]);
149+
150+
executeCommandSpy.mockRestore();
151+
});
152+
153+
it('should open ref with explicit md extension', async () => {
154+
const name = rndName();
155+
156+
const executeCommandSpy = jest.spyOn(commands, 'executeCommand');
157+
158+
await openDocumentByReference({
159+
reference: `${name}.md`,
160+
});
161+
162+
expect(
163+
toPlainObject(executeCommandSpy.mock.calls.filter(([command]) => command === 'vscode.open')),
164+
).toMatchObject([
165+
[
166+
'vscode.open',
167+
expect.objectContaining({
168+
$mid: 1,
169+
path: path.join(getWorkspaceFolder()!, `${name}.md.md`),
170+
scheme: 'file',
171+
}),
172+
],
173+
]);
174+
175+
executeCommandSpy.mockRestore();
176+
});
125177
});

src/commands/openDocumentByReference.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ const openDocumentByReference = async ({ reference }: { reference: string }) =>
2121
const workspaceFolder = getWorkspaceFolder()!;
2222
if (workspaceFolder) {
2323
const paths = ref.split('/');
24-
const pathsWithExt = [...paths.slice(0, -1), `${paths.slice(-1)}.md`];
24+
const refExt = path.parse(ref).ext;
25+
const pathsWithExt = [
26+
...paths.slice(0, -1),
27+
`${paths.slice(-1)}${refExt !== '.md' && refExt !== '' ? '' : '.md'}`,
28+
];
2529
const filePath = path.join(workspaceFolder, ...pathsWithExt);
2630

2731
// don't override file content if it already exists

0 commit comments

Comments
 (0)