Using URI as much as possible in note creation to minimize platform specific handling

This commit is contained in:
Riccardo Ferretti
2025-07-24 17:41:25 +02:00
parent 27665154db
commit c028689012
2 changed files with 13 additions and 15 deletions

View File

@@ -42,7 +42,7 @@ describe('create-note command', () => {
]);
const target = getUriInWorkspace();
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.path,
notePath: target,
templatePath: templateA.uri.path,
text: 'hello',
});
@@ -55,7 +55,7 @@ describe('create-note command', () => {
it('focuses on the newly created note', async () => {
const target = getUriInWorkspace();
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.path,
notePath: target,
text: 'hello',
});
expect(window.activeTextEditor.document.getText()).toEqual('hello');
@@ -66,7 +66,7 @@ describe('create-note command', () => {
it('supports variables', async () => {
const target = getUriInWorkspace();
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.path,
notePath: target,
text: 'hello ${FOAM_TITLE}', // eslint-disable-line no-template-curly-in-string
variables: { FOAM_TITLE: 'world' },
});
@@ -78,7 +78,7 @@ describe('create-note command', () => {
it('supports date variables', async () => {
const target = getUriInWorkspace();
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.path,
notePath: target,
text: 'hello ${FOAM_DATE_YEAR}', // eslint-disable-line no-template-curly-in-string
date: '2021-10-01',
});
@@ -93,7 +93,7 @@ describe('create-note command', () => {
expect(content).toEqual('hello');
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.uri.path,
notePath: target.uri,
text: 'test overwrite',
onFileExists: 'overwrite',
});
@@ -104,7 +104,7 @@ describe('create-note command', () => {
await closeEditors();
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.uri.path,
notePath: target.uri,
text: 'test open',
onFileExists: 'open',
});
@@ -115,7 +115,7 @@ describe('create-note command', () => {
await closeEditors();
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.uri.path,
notePath: target.uri,
text: 'test cancel',
onFileExists: 'cancel',
});
@@ -126,7 +126,7 @@ describe('create-note command', () => {
.mockImplementationOnce(jest.fn(() => Promise.resolve(undefined)));
await closeEditors();
await commands.executeCommand('foam-vscode.create-note', {
notePath: target.uri.path,
notePath: target.uri,
text: 'test ask',
onFileExists: 'ask',
});

View File

@@ -40,7 +40,7 @@ interface CreateNoteArgs {
* The path of the note to create.
* If relative it will be resolved against the workspace root.
*/
notePath?: string;
notePath?: string | URI;
/**
* The path of the template to use.
*/
@@ -141,7 +141,10 @@ export async function createNote(args: CreateNoteArgs, foam: Foam) {
// If notePath is provided, add it to template metadata to avoid unnecessary title resolution
if (args.notePath && template.type === 'markdown') {
template.metadata = template.metadata || new Map();
template.metadata.set('filepath', args.notePath);
template.metadata.set(
'filepath',
args.notePath instanceof URI ? args.notePath.toFsPath() : args.notePath
);
}
// Create resolver with all variables upfront
@@ -155,11 +158,6 @@ export async function createNote(args: CreateNoteArgs, foam: Foam) {
resolver.define('FOAM_TITLE', args.title);
}
// Add other parameters as variables
if (args.notePath) {
resolver.define('notePath', args.notePath);
}
// Process template using the new engine with unified resolver
const engine = new NoteCreationEngine(
foam,