From 526573f571108d751ada40508c401c8b6f927ea0 Mon Sep 17 00:00:00 2001 From: Logan Yang Date: Wed, 14 Aug 2024 13:41:53 -0700 Subject: [PATCH] Fix find notes by path bug (#508) --- src/utils.ts | 32 ++++++++++++++++++++++++-------- tests/utils.test.ts | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index f0631152..9ab74e4a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -56,15 +56,30 @@ export const getNotesFromPath = async ( return files; } - // Split the path to get the last folder name - const pathSegments = path.split("/"); - const lastSegment = pathSegments[pathSegments.length - 1].toLowerCase(); + // Normalize the input path + const normalizedPath = path.toLowerCase().replace(/^\/|\/$/g, ""); return files.filter((file) => { - // Split the file path and get the last directory name - return ( - isFolderMatch(file.path, lastSegment) || file.basename === lastSegment - ); + // Normalize the file path + const normalizedFilePath = file.path.toLowerCase(); + const filePathParts = normalizedFilePath.split("/"); + const pathParts = normalizedPath.split("/"); + + // Check if the file path contains all parts of the input path in order + let filePathIndex = 0; + for (const pathPart of pathParts) { + while (filePathIndex < filePathParts.length) { + if (filePathParts[filePathIndex] === pathPart) { + break; + } + filePathIndex++; + } + if (filePathIndex >= filePathParts.length) { + return false; + } + } + + return true; }); }; @@ -512,7 +527,8 @@ export function extractNoteTitles(query: string): string[] { } /** - * Process the variable name to generate a note path if it's enclosed in double brackets, otherwise return the variable name as is. + * Process the variable name to generate a note path if it's enclosed in double brackets, + * otherwise return the variable name as is. * * @param {string} variableName - The name of the variable to process * @return {string} The processed note path or the variable name itself diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 68df6822..0f9f9442 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -100,6 +100,25 @@ describe("getNotesFromPath", () => { expect(files).toEqual([]); }); + it("should return only files from the specified subfolder path", async () => { + const vault = new Obsidian.Vault(); + // Mock the getMarkdownFiles method to return our test structure + vault.getMarkdownFiles = jest + .fn() + .mockReturnValue([ + { path: "folder/subfolder 1/eng/1.md" }, + { path: "folder/subfolder 2/eng/3.md" }, + { path: "folder/subfolder 1/eng/2.md" }, + { path: "folder/other/note.md" }, + ]); + + const files = await getNotesFromPath(vault, "folder/subfolder 1/eng"); + expect(files).toEqual([ + { path: "folder/subfolder 1/eng/1.md" }, + { path: "folder/subfolder 1/eng/2.md" }, + ]); + }); + describe("processVariableNameForNotePath", () => { it("should return the note md filename", () => { const variableName = processVariableNameForNotePath("[[test]]");