From 43673aaf3533a85a47f6639cc26f50ed8d59b918 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 30 Apr 2021 16:16:13 +0200 Subject: [PATCH] Change zettel anchor getting function definition Unify the definitions of the two function to take a set of files instead of different arguments. The list of files has the form: files = { "full/path/name.md" = "name.md" } I.e. the complete path is the key and the file basename the value, leading to a set of files, even if basenames are the same. Usually it will be created by the get_all_files function of the same module, but it can be passed in manually as well (e.g. to avoid duplicate filesystem lookups, etc). --- lua/zettelkasten/files.lua | 19 ++++++++--------- lua/zettelkasten/files_spec.lua | 36 ++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lua/zettelkasten/files.lua b/lua/zettelkasten/files.lua index 4c23dc8..1e23065 100644 --- a/lua/zettelkasten/files.lua +++ b/lua/zettelkasten/files.lua @@ -61,18 +61,15 @@ function ls.get_all_files(path, recursive) end -- Returns the path to the zettel defined by the anchor argument. --- Takes a set of zettel as an optional variable in the form --- { [anchorID] = "full/path/to/file.md" } --- If no set provided, it will use the (recursive) results --- of the zettel_root directory. -function ls.get_zettel_by_anchor(anchor, all) - if not all then - local files = ls.get_all_files(o.zettel().rootdir, true) - all = ls.get_anchors_and_paths(files) - end - if not all then return end +-- Takes a set of files as optional variable. +-- If no set provided will use the (recursive) results +-- of zettel_root directory. +function ls.get_zettel_by_anchor(anchor, files) + files = files or ls.get_all_files(o.zettel().rootdir, true) + local zettels = ls.get_anchors_and_paths(files) + if not zettels then return end - return all[anchor] + return zettels[anchor] end -- Returns the path to the zettel defined by a reference link. diff --git a/lua/zettelkasten/files_spec.lua b/lua/zettelkasten/files_spec.lua index abbd7f5..0e23b7d 100644 --- a/lua/zettelkasten/files_spec.lua +++ b/lua/zettelkasten/files_spec.lua @@ -25,7 +25,8 @@ describe("get_anchors_and_paths", function() it("should return anchor-keyed table pointing to filename of zettel", function() local file_list = {} - file_list["someDir/1910291645 this-is-a-testfile.md"] = "1910291645 this-is-a-testfile.md" + file_list["someDir/1910291645 this-is-a-testfile.md"] = + "1910291645 this-is-a-testfile.md" _G.vim = get_api_mock(file_list) local expected = { @@ -40,7 +41,7 @@ describe("get_anchors_and_paths", function() ["someDir/1910291645 this-is-a-testfile.md"] = "1910291645 this-is-a-testfile.md", ["someDir/this-is-not-a-testfile.md"] = "this-is-not-a-testfile.md", ["1910271456 this-is-wrong-extension.txt"] = "1910271456 this-is-wrong-extension.txt", - ["1812 this-is-ignored.md"] = "1812 this-is-ignored.md", + ["1812 this-is-ignored.md"] = "1812 this-is-ignored.md" } _G.vim = get_api_mock(file_list) @@ -64,8 +65,7 @@ describe("get_anchors_and_paths", function() ["2345678901"] = "mydirectory/2345678901 another.wiki" } - assert.same(expected, - ls.get_anchors_and_paths(file_list, false, vim.g)) + assert.same(expected, ls.get_anchors_and_paths(file_list, false, vim.g)) end) end) @@ -105,8 +105,7 @@ describe("get_all_files", function() "path/to/startingdir/more-notes-here") end) - it("should add all files found in subdirectories when recursing", - function() + it("should add all files found in subdirectories when recursing", function() local outer_files = { "subdir", "1234567890 myfile.md", "2345678901 another.md" } @@ -149,17 +148,19 @@ end) describe("get_zettel_by_anchor", function() it("should return the correct zettel by id", function() local file_list = { - ["1910291645"] = "1910291645 myfile.md", - ["2345678901"] = "2345678901 another.md" + ["aDir/1910291645 myfile.md"] = "1910291645 myfile.md", + ["dir/2345678901 another.md"] = "2345678901 another.md" } _G.vim = simple_api_mock(file_list) - assert.same("1910291645 myfile.md", + assert.same("aDir/1910291645 myfile.md", ls.get_zettel_by_anchor("1910291645", file_list)) end) it("should return nil and not break on no all list passed in", function() stub(ls, "get_anchors_and_paths") - assert.is_not_error(function() ls.get_zettel_by_anchor("myanchor") end) + assert.is_not_error(function() + ls.get_zettel_by_anchor("myanchor") + end) end) it("should default to the zettel root dir if no list passed in", function() local fc = stub(ls, "get_all_files") @@ -175,23 +176,26 @@ describe("get_zettel_by_ref", 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", + ["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)) + 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() + 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", + ["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)) + 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", + ["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)