Skip to content

Commit

Permalink
Enhance folder reference detection in user input
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Brian Joseph Petro committed Dec 4, 2024
1 parent c5a09ab commit 51ecf38
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
17 changes: 12 additions & 5 deletions smart-chats/utils/folder_references.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 15 additions & 1 deletion smart-chats/utils/folder_references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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);
});
});

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

0 comments on commit 51ecf38

Please sign in to comment.