From 325be3c50006bca9de14fccee03c00e4b4a19511 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 30 Apr 2021 15:52:54 +0200 Subject: [PATCH] Split zettel getting into by anchor and by ref Prepare for creating a lookup of zettels through the root directory by file name as well as zettel id. --- lua/zettelkasten/action.lua | 2 +- lua/zettelkasten/action_spec.lua | 2 +- lua/zettelkasten/files.lua | 16 +++++++++++++--- lua/zettelkasten/files_spec.lua | 31 +++++++++++++++++++++---------- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lua/zettelkasten/action.lua b/lua/zettelkasten/action.lua index 215b68d..8b52683 100644 --- a/lua/zettelkasten/action.lua +++ b/lua/zettelkasten/action.lua @@ -10,7 +10,7 @@ local BIGNUMBER = 10000000 -- Requires a link object passed in. function A.open(zlink) if not zlink or not zlink.ref then return end - local fname = list.get_zettel(zlink.anchor) or zlink.ref + local fname = list.get_zettel_by_anchor(zlink.anchor) or zlink.ref vim.api.nvim_command(string.format("edit %s", fname)) end diff --git a/lua/zettelkasten/action_spec.lua b/lua/zettelkasten/action_spec.lua index 4a5a0c8..dc0077e 100644 --- a/lua/zettelkasten/action_spec.lua +++ b/lua/zettelkasten/action_spec.lua @@ -19,7 +19,7 @@ describe("open", function() end) it("should use the anchor to open the corresponding zettel", function() vim.api = {nvim_command = mock(function() end)} - local ls = stub(require 'zettelkasten.files', "get_zettel") + local ls = stub(require 'zettelkasten.files', "get_zettel_by_anchor") action.open({ ref = "1910271456_link-to-my-file.md", diff --git a/lua/zettelkasten/files.lua b/lua/zettelkasten/files.lua index ebbb78a..c1f7aef 100644 --- a/lua/zettelkasten/files.lua +++ b/lua/zettelkasten/files.lua @@ -61,9 +61,11 @@ function ls.get_all_files(path, recursive) end -- Returns the path to the zettel defined by the anchor argument. --- Take a list of zettel as an optional variable, without which --- it will use the (recursive) results of the zettel_root directory. -function ls.get_zettel(anchor, all) +-- 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) @@ -73,4 +75,12 @@ function ls.get_zettel(anchor, all) return all[anchor] end +function ls.get_zettel_by_ref(ref, files) + for full_path, bname in pairs(files) do + if bname == ref then return full_path end + end + return "" +end + + return ls diff --git a/lua/zettelkasten/files_spec.lua b/lua/zettelkasten/files_spec.lua index 770b3e1..626dd47 100644 --- a/lua/zettelkasten/files_spec.lua +++ b/lua/zettelkasten/files_spec.lua @@ -25,8 +25,7 @@ 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 = { @@ -41,7 +40,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) @@ -65,7 +64,8 @@ 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,7 +105,8 @@ 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" } @@ -145,7 +146,7 @@ describe("get_all_files", function() end) end) -describe("get_zettel", function() +describe("get_zettel_by_anchor", function() it("should return the correct zettel by id", function() local file_list = { ["1910291645"] = "1910291645 myfile.md", @@ -154,18 +155,28 @@ describe("get_zettel", function() _G.vim = simple_api_mock(file_list) assert.same("1910291645 myfile.md", - ls.get_zettel("1910291645", file_list)) + 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("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") local expected = require'zettelkasten.options'.zettel().rootdir - ls.get_zettel(expected) + ls.get_zettel_by_anchor(expected) assert.stub(fc).was_called_with(expected, true) end) - +end) + +describe("get_zettel_by_ref", function() + it("should match a full file path for non-zettel files", 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) end)