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).
This commit is contained in:
Marty Oehme 2021-04-30 16:16:13 +02:00
parent 4aa9d98818
commit 43673aaf35
Signed by: Marty
GPG key ID: B7538B8F50A1C800
2 changed files with 28 additions and 27 deletions

View file

@ -61,18 +61,15 @@ function ls.get_all_files(path, recursive)
end end
-- Returns the path to the zettel defined by the anchor argument. -- Returns the path to the zettel defined by the anchor argument.
-- Takes a set of zettel as an optional variable in the form -- Takes a set of files as optional variable.
-- { [anchorID] = "full/path/to/file.md" } -- If no set provided will use the (recursive) results
-- If no set provided, it will use the (recursive) results -- of zettel_root directory.
-- of the zettel_root directory. function ls.get_zettel_by_anchor(anchor, files)
function ls.get_zettel_by_anchor(anchor, all) files = files or ls.get_all_files(o.zettel().rootdir, true)
if not all then local zettels = ls.get_anchors_and_paths(files)
local files = ls.get_all_files(o.zettel().rootdir, true) if not zettels then return end
all = ls.get_anchors_and_paths(files)
end
if not all then return end
return all[anchor] return zettels[anchor]
end end
-- Returns the path to the zettel defined by a reference link. -- Returns the path to the zettel defined by a reference link.

View file

@ -25,7 +25,8 @@ describe("get_anchors_and_paths", function()
it("should return anchor-keyed table pointing to filename of zettel", it("should return anchor-keyed table pointing to filename of zettel",
function() function()
local file_list = {} 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) _G.vim = get_api_mock(file_list)
local expected = { 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/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", ["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", ["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) _G.vim = get_api_mock(file_list)
@ -64,8 +65,7 @@ describe("get_anchors_and_paths", function()
["2345678901"] = "mydirectory/2345678901 another.wiki" ["2345678901"] = "mydirectory/2345678901 another.wiki"
} }
assert.same(expected, assert.same(expected, ls.get_anchors_and_paths(file_list, false, vim.g))
ls.get_anchors_and_paths(file_list, false, vim.g))
end) end)
end) end)
@ -105,8 +105,7 @@ describe("get_all_files", function()
"path/to/startingdir/more-notes-here") "path/to/startingdir/more-notes-here")
end) end)
it("should add all files found in subdirectories when recursing", it("should add all files found in subdirectories when recursing", function()
function()
local outer_files = { local outer_files = {
"subdir", "1234567890 myfile.md", "2345678901 another.md" "subdir", "1234567890 myfile.md", "2345678901 another.md"
} }
@ -149,17 +148,19 @@ end)
describe("get_zettel_by_anchor", function() describe("get_zettel_by_anchor", function()
it("should return the correct zettel by id", function() it("should return the correct zettel by id", function()
local file_list = { local file_list = {
["1910291645"] = "1910291645 myfile.md", ["aDir/1910291645 myfile.md"] = "1910291645 myfile.md",
["2345678901"] = "2345678901 another.md" ["dir/2345678901 another.md"] = "2345678901 another.md"
} }
_G.vim = simple_api_mock(file_list) _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)) ls.get_zettel_by_anchor("1910291645", file_list))
end) end)
it("should return nil and not break on no all list passed in", function() it("should return nil and not break on no all list passed in", function()
stub(ls, "get_anchors_and_paths") 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) end)
it("should default to the zettel root dir if no list passed in", function() it("should default to the zettel root dir if no list passed in", function()
local fc = stub(ls, "get_all_files") 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() it("should return a full file path for file path linked", function()
local file_list = { local file_list = {
["link/to/my/file.md"] = "file.md", ["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) 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 = { local file_list = {
["link/to/my/file.md"] = "file.md", ["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) end)
it("should not return anything if no match exists", function() it("should not return anything if no match exists", function()
local file_list = { local file_list = {
["link/to/my/file.md"] = "file.md", ["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)) assert.same(nil, ls.get_zettel_by_ref("target-file.md", file_list))
end) end)