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.
This commit is contained in:
parent
9dcd2556ba
commit
325be3c500
4 changed files with 36 additions and 15 deletions
|
@ -10,7 +10,7 @@ local BIGNUMBER = 10000000
|
||||||
-- Requires a link object passed in.
|
-- Requires a link object passed in.
|
||||||
function A.open(zlink)
|
function A.open(zlink)
|
||||||
if not zlink or not zlink.ref then return end
|
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))
|
vim.api.nvim_command(string.format("edit %s", fname))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ describe("open", function()
|
||||||
end)
|
end)
|
||||||
it("should use the anchor to open the corresponding zettel", function()
|
it("should use the anchor to open the corresponding zettel", function()
|
||||||
vim.api = {nvim_command = mock(function() end)}
|
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({
|
action.open({
|
||||||
ref = "1910271456_link-to-my-file.md",
|
ref = "1910271456_link-to-my-file.md",
|
||||||
|
|
|
@ -61,9 +61,11 @@ 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.
|
||||||
-- Take a list of zettel as an optional variable, without which
|
-- Takes a set of zettel as an optional variable in the form
|
||||||
-- it will use the (recursive) results of the zettel_root directory.
|
-- { [anchorID] = "full/path/to/file.md" }
|
||||||
function ls.get_zettel(anchor, all)
|
-- 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
|
if not all then
|
||||||
local files = ls.get_all_files(o.zettel().rootdir, true)
|
local files = ls.get_all_files(o.zettel().rootdir, true)
|
||||||
all = ls.get_anchors_and_paths(files)
|
all = ls.get_anchors_and_paths(files)
|
||||||
|
@ -73,4 +75,12 @@ function ls.get_zettel(anchor, all)
|
||||||
return all[anchor]
|
return all[anchor]
|
||||||
end
|
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
|
return ls
|
||||||
|
|
|
@ -25,8 +25,7 @@ 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"] =
|
file_list["someDir/1910291645 this-is-a-testfile.md"] = "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 = {
|
||||||
|
@ -41,7 +40,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)
|
||||||
|
|
||||||
|
@ -65,7 +64,8 @@ describe("get_anchors_and_paths", function()
|
||||||
["2345678901"] = "mydirectory/2345678901 another.wiki"
|
["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)
|
||||||
end)
|
end)
|
||||||
|
@ -105,7 +105,8 @@ 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", function()
|
it("should add all files found in subdirectories when recursing",
|
||||||
|
function()
|
||||||
local outer_files = {
|
local outer_files = {
|
||||||
"subdir", "1234567890 myfile.md", "2345678901 another.md"
|
"subdir", "1234567890 myfile.md", "2345678901 another.md"
|
||||||
}
|
}
|
||||||
|
@ -145,7 +146,7 @@ describe("get_all_files", function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("get_zettel", 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",
|
["1910291645"] = "1910291645 myfile.md",
|
||||||
|
@ -154,18 +155,28 @@ describe("get_zettel", function()
|
||||||
_G.vim = simple_api_mock(file_list)
|
_G.vim = simple_api_mock(file_list)
|
||||||
|
|
||||||
assert.same("1910291645 myfile.md",
|
assert.same("1910291645 myfile.md",
|
||||||
ls.get_zettel("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("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")
|
||||||
local expected = require'zettelkasten.options'.zettel().rootdir
|
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)
|
assert.stub(fc).was_called_with(expected, true)
|
||||||
end)
|
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)
|
end)
|
||||||
|
|
Loading…
Reference in a new issue