Handle arbitrary leading and trailing spaces in variables (#282)

This commit is contained in:
Logan Yang 2024-02-06 12:20:11 -08:00 committed by GitHub
parent f7410774c8
commit e78eee1829
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 2 deletions

View file

@ -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;

View file

@ -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');
});
});
});