Fix find notes by path bug (#508)

This commit is contained in:
Logan Yang 2024-08-14 13:41:53 -07:00 committed by GitHub
parent 9c0a56d70d
commit 526573f571
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 8 deletions

View file

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

View file

@ -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]]");