Add opening zettel by reference if no anchor found

Opens zettel by linked value, most often a direct path link, but will
also look through the root dir to find a(n exactly) matching basename to
open.

If nothing is found in the root dir will open a new file to write with
the corresponding name. Careful, if the reference is a full path
definition, and the file does not exist, it will still open the
corresponding file at the correct location but when attempting to save
will generally complain if parts of the path are missing. They have to
be created manually or in some other place, this is outside the scope of
this plugin.
This commit is contained in:
Marty Oehme 2021-04-30 16:28:22 +02:00
parent 43673aaf35
commit fdcb0f2a93
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
4 changed files with 9 additions and 2 deletions

View File

@ -81,6 +81,7 @@ next up:
* i.e. if 2030101200 exists move to 2030101159, 2030101158, ...
* if moving backwards, we do not take away id space from *future* note creation
* if moving forwards, every zettel created within a minute would delay next zettel creation *another* minute
* to decide: should zettel creation create a zettel in current working dir or at zettel root dir? or set by option?
* [ ] (CODE) switch -- comments to --- doc comments for function descriptions etc

View File

@ -10,7 +10,8 @@ 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_by_anchor(zlink.anchor) or zlink.ref
local fname = list.get_zettel_by_anchor(zlink.anchor) or
list.get_zettel_by_ref(zlink.ref) or zlink.ref
vim.api.nvim_command(string.format("edit %s", fname))
end

View File

@ -17,7 +17,8 @@ describe("open", function()
vim.fn = {expand = function() end}
assert.is_not_error(action.open)
end)
it("should use the anchor to open the corresponding zettel", function()
it("should first use the anchor to open the corresponding zettel",
function()
vim.api = {nvim_command = mock(function() end)}
local ls = stub(require 'zettelkasten.files', "get_zettel_by_anchor")

View File

@ -73,6 +73,10 @@ function ls.get_zettel_by_anchor(anchor, files)
end
-- Returns the path to the zettel defined by a reference link.
-- Will prefer a fully matching path to only matching basename
-- of a file, but if only basename is found will return first
-- matching one.
--
-- Takes a set of files as optional variable in.
-- If no set provided will use the (recursive) results
-- of zettel_root directory.