diff --git a/lua/zettelkasten/files.lua b/lua/zettelkasten/files.lua index c1f7aef..793f8ea 100644 --- a/lua/zettelkasten/files.lua +++ b/lua/zettelkasten/files.lua @@ -76,10 +76,12 @@ function ls.get_zettel_by_anchor(anchor, all) end function ls.get_zettel_by_ref(ref, files) + local name_only_match for full_path, bname in pairs(files) do - if bname == ref then return full_path end + if full_path == ref then return full_path end + if bname == ref then name_only_match = full_path end end - return "" + return name_only_match end diff --git a/lua/zettelkasten/files_spec.lua b/lua/zettelkasten/files_spec.lua index 626dd47..abbd7f5 100644 --- a/lua/zettelkasten/files_spec.lua +++ b/lua/zettelkasten/files_spec.lua @@ -171,12 +171,29 @@ describe("get_zettel_by_anchor", function() end) describe("get_zettel_by_ref", function() - it("should match a full file path for non-zettel files", function() + + it("should return a full file path for file path linked", function() + local file_list = { + ["link/to/my/file.md"] = "file.md", + ["link/to/my/target-file.md"] = "target-file.md", + } + assert.same("link/to/my/target-file.md", ls.get_zettel_by_ref("link/to/my/target-file.md", file_list)) + end) + + it("should return path to matching base name if only that is linked", function() local file_list = { ["link/to/my/file.md"] = "file.md", ["link/to/my/target-file.md"] = "target-file.md", } assert.same("link/to/my/target-file.md", ls.get_zettel_by_ref("target-file.md", file_list)) - end) + + it("should not return anything if no match exists", function() + local file_list = { + ["link/to/my/file.md"] = "file.md", + ["link/to/my/no-target-file.md"] = "no-target-file.md", + } + assert.same(nil, ls.get_zettel_by_ref("target-file.md", file_list)) + end) + end)