diff --git a/src/utils.ts b/src/utils.ts index b3bfe57f..e4b28af4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -252,10 +252,11 @@ export function extractChatHistory(memoryVariables: MemoryVariables): [string, s } export function processVariableName(variableName: string): string { + variableName = variableName.trim(); // Check if the variable name is enclosed in double brackets indicating it's a note if (variableName.startsWith('[[') && variableName.endsWith(']]')) { // It's a note, so we remove the brackets and append '.md' - return `${variableName.slice(2, -2)}.md`; + return `${variableName.slice(2, -2).trim()}.md`; } // It's a path, so we just return it as is return variableName; diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 128c8092..cc09f2f0 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -1,5 +1,5 @@ import * as Obsidian from 'obsidian'; -import { getNotesFromPath, isFolderMatch } from '../src/utils'; +import { getNotesFromPath, isFolderMatch, processVariableName } from '../src/utils'; describe('isFolderMatch', () => { @@ -94,4 +94,51 @@ describe('getNotesFromPath', () => { const files = await getNotesFromPath(vault, ''); expect(files).toEqual([]); }); + + describe('processVariableName', () => { + it('should return the note md filename', () => { + const variableName = processVariableName('[[test]]'); + expect(variableName).toEqual('test.md'); + }); + + it('should return the note md filename with extra spaces 1', () => { + const variableName = processVariableName(' [[ test]]'); + expect(variableName).toEqual('test.md'); + }); + + it('should return the note md filename with extra spaces 2', () => { + const variableName = processVariableName('[[ test ]] '); + expect(variableName).toEqual('test.md'); + }); + + it('should return the note md filename with extra spaces 2', () => { + const variableName = processVariableName(' [[ test note ]] '); + expect(variableName).toEqual('test note.md'); + }); + + it('should return the note md filename with extra spaces 2', () => { + const variableName = processVariableName(' [[ test_note note ]] '); + expect(variableName).toEqual('test_note note.md'); + }); + + it('should return folder path with leading slash', () => { + const variableName = processVariableName('/testfolder'); + expect(variableName).toEqual('/testfolder'); + }); + + it('should return folder path without slash', () => { + const variableName = processVariableName('testfolder'); + expect(variableName).toEqual('testfolder'); + }); + + it('should return folder path with trailing slash', () => { + const variableName = processVariableName('testfolder/'); + expect(variableName).toEqual('testfolder/'); + }); + + it('should return folder path with leading spaces', () => { + const variableName = processVariableName(' testfolder '); + expect(variableName).toEqual('testfolder'); + }); + }); });