From e2a97570e066668fa19c45d3a029da7e6204e500 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 30 Apr 2021 16:02:13 +0200 Subject: [PATCH] Add zettel opening by base name matching --- lua/zettelkasten/files.lua | 6 ++++-- lua/zettelkasten/files_spec.lua | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) 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)