From 51ecf385484e6db1e1117e90a671194c642f245d Mon Sep 17 00:00:00 2001 From: Brian Joseph Petro Date: Wed, 4 Dec 2024 10:10:54 -0500 Subject: [PATCH] Enhance folder reference detection in user input - Added checks for wiki-style links ([[...]]), ensuring folder references are not falsely detected within them. - Improved handling of parentheses to correctly identify folder paths that may be wrapped in parentheses. - Updated tests to validate new behavior, including cases for links containing folder references and ensuring accurate detection of folder paths. This update improves the robustness of the folder reference detection logic, enhancing user input handling. --- smart-chats/utils/folder_references.js | 17 ++++++++++++----- smart-chats/utils/folder_references.test.js | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/smart-chats/utils/folder_references.js b/smart-chats/utils/folder_references.js index e2f2e856..6236ec0b 100644 --- a/smart-chats/utils/folder_references.js +++ b/smart-chats/utils/folder_references.js @@ -4,16 +4,23 @@ export function contains_folder_reference(user_input) { if (first_slash === -1) return false; const last_slash = user_input.lastIndexOf("/"); if (last_slash - first_slash <= 1) return false; // if slashes are the same or JavaScript-style comment + + // Check for parentheses const first_open_parentheses = user_input.indexOf("("); const first_close_parentheses = user_input.indexOf(")"); if (first_open_parentheses > first_slash && first_close_parentheses < last_slash) return true; // folder path contains parentheses - // returns false if slash is wrapped in parentheses + // Check for wiki-style links [[...]] + const first_wiki_open = user_input.indexOf("[["); + const first_wiki_close = user_input.indexOf("]]"); + if (first_wiki_open !== -1 && first_wiki_close !== -1) { + if (first_slash > first_wiki_open && last_slash < first_wiki_close) return false; + } + + // Check for regular parentheses if (first_open_parentheses !== -1 && first_close_parentheses !== -1) { - const start = user_input.indexOf("("); - const end = user_input.indexOf(")"); - // remove content in parentheses - const without_content_in_parentheses = user_input.slice(0, start) + user_input.slice(end + 1); + if (first_slash > first_open_parentheses && last_slash < first_close_parentheses) return false; + const without_content_in_parentheses = user_input.slice(0, first_open_parentheses) + user_input.slice(first_close_parentheses + 1); if (without_content_in_parentheses.indexOf("/") !== -1) return false; if (without_content_in_parentheses.indexOf("/") === without_content_in_parentheses.lastIndexOf("/")) return false; } diff --git a/smart-chats/utils/folder_references.test.js b/smart-chats/utils/folder_references.test.js index 33b8bda5..60fe3311 100644 --- a/smart-chats/utils/folder_references.test.js +++ b/smart-chats/utils/folder_references.test.js @@ -26,6 +26,12 @@ test('returns true if folder contains parentheses', t => { const userInput = 'This is a /folder (reference)/'; t.true(contains_folder_reference(userInput)); }); +// should return false if contained in a link like [[folder/subfolder/this.md]] +test('returns false if contained in a link like [[folder/subfolder/this.md]]', t => { + const folders = ['/folder/subfolder/']; + const user_input = 'This is a [[folder/subfolder/this.md]] link'; + t.false(contains_folder_reference(user_input), 'should return false if contained in a link like [[folder/subfolder/this.md]]'); +}); // extract_folder_references test('returns an array of folder references found in the user input', t => { @@ -42,4 +48,12 @@ test('returns an empty array if no folder references are found in the user input const expected = []; const result = extract_folder_references(folders, user_input); t.deepEqual(result, expected); -}); \ No newline at end of file +}); + +test('returns empty array if folder is in a link like [[folder/subfolder/this.md]]', t => { + const folders = ['/folder/subfolder/']; + const user_input = 'This is a [[folder/subfolder/this.md]] link'; + const expected = []; + const result = extract_folder_references(folders, user_input); + t.deepEqual(result, expected); +});