Skip to content

Commit 0938de2

Browse files
Ensure absolute paths used in create-note command are relative to workspace
1 parent a120f36 commit 0938de2

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

packages/foam-vscode/src/dated-notes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Unix: \${FOAM_DATE_SECONDS_UNIX}`,
242242
const monthName = foamDate.toLocaleString('default', { month: 'long' });
243243
const day = foamDate.getDate();
244244
return {
245-
filepath: \`/\${foamDate.getFullYear()}-\${String(foamDate.getMonth() + 1).padStart(2, '0')}-\${String(day).padStart(2, '0')}.md\`,
245+
filepath: \`\${foamDate.getFullYear()}-\${String(foamDate.getMonth() + 1).padStart(2, '0')}-\${String(day).padStart(2, '0')}.md\`,
246246
content: \`# JS Template: \${monthName} \${day}\n\nGenerated by JavaScript template.\`
247247
};
248248
};`,

packages/foam-vscode/src/features/commands/create-note.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,27 @@ describe('create-note command', () => {
229229
expect(error.message).toContain(`Failed to load template`); // eslint-disable-line jest/no-conditional-expect
230230
}
231231
});
232+
233+
it('creates a note with absolute path within the workspace', async () => {
234+
await commands.executeCommand('foam-vscode.create-note', {
235+
notePath: '/note-in-workspace.md',
236+
text: 'hello workspace',
237+
});
238+
expect(window.activeTextEditor.document.getText()).toEqual(
239+
'hello workspace'
240+
);
241+
expectSameUri(
242+
window.activeTextEditor.document.uri,
243+
fromVsCodeUri(workspace.workspaceFolders?.[0].uri).joinPath(
244+
'note-in-workspace.md'
245+
)
246+
);
247+
await deleteFile(
248+
fromVsCodeUri(workspace.workspaceFolders?.[0].uri).joinPath(
249+
'note-in-workspace.md'
250+
)
251+
);
252+
});
232253
});
233254

234255
describe('factories', () => {

packages/foam-vscode/src/services/templates.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ export const NoteFactory = {
319319
}
320320
}
321321

322+
newFilePath = asAbsoluteWorkspaceUri(newFilePath, true);
322323
const expandedText = await resolver.resolveText(text);
323324
const selectedContent = findSelectionContent();
324325
await createDocAndFocus(

0 commit comments

Comments
 (0)