Add zettel opening by anchor

This commit is contained in:
Marty Oehme 2020-11-06 17:28:05 +01:00
parent 382b0bef72
commit 93a1dfbae4
Signed by: Marty
GPG key ID: B7538B8F50A1C800
5 changed files with 37 additions and 14 deletions

View file

@ -2,6 +2,7 @@ local A = {}
local o = require 'zettelkasten.options' local o = require 'zettelkasten.options'
local link = require 'zettelkasten.link' local link = require 'zettelkasten.link'
local list = require 'zettelkasten.list'
local BIGNUMBER = 10000000 local BIGNUMBER = 10000000
@ -9,8 +10,8 @@ local BIGNUMBER = 10000000
-- Requires a link object passed in. -- Requires a link object passed in.
function A.open(link) function A.open(link)
if not link or not link.ref then return end if not link or not link.ref then return end
-- TODO follow: go to anchor, fall back to filename local fname = list.get_zettel(link.anchor) or link.ref
vim.api.nvim_command(string.format("edit %s", link.ref)) vim.api.nvim_command(string.format("edit %s", fname))
end end
-- Gets the input at the current buffer cursor and opens it -- Gets the input at the current buffer cursor and opens it

View file

@ -1,6 +1,8 @@
action = require 'zettelkasten.action' action = require 'zettelkasten.action'
before_each(function() _G.vim = {g = {}, b = {}} end) before_each(function()
_G.vim = {g = {}, b = {}, loop = {fs_scandir = function() end}}
end)
after_each(function() _G.vim = nil end) after_each(function() _G.vim = nil end)
describe("open", function() describe("open", function()
@ -15,6 +17,16 @@ describe("open", function()
vim.fn = {expand = function() end} vim.fn = {expand = function() end}
assert.is_not_error(action.open) assert.is_not_error(action.open)
end) end)
it("should use the anchor to open the corresponding zettel", function()
vim.api = {nvim_command = mock(function() end)}
ls = stub(require 'zettelkasten.list', "get_zettel")
action.open({
ref = "1910271456_link-to-my-file.md",
anchor = "1910271456"
})
assert.stub(ls).was_called_with("1910271456")
end)
end) end)
describe("open_selected", function() describe("open_selected", function()

View file

@ -94,7 +94,8 @@ end)
describe("extract_all", function() describe("extract_all", function()
it("should get all links input string", function() it("should get all links input string", function()
local input = "[Some text](2003042042_my-link.md) and another, [with more text](2001261123 another-link.md), and done. " local input =
"[Some text](2003042042_my-link.md) and another, [with more text](2001261123 another-link.md), and done. "
local expected = { local expected = {
{ {
endpos = 34, endpos = 34,

View file

@ -7,6 +7,11 @@ local function isDirectory(ftype)
return false return false
end end
local function cleanPath(path)
if path:match("^~") then path = os.getenv("HOME") .. path:sub(2) end
return path
end
-- TODO transform paths: -- TODO transform paths:
-- * to absolute value (e.g. ~ to home, scandir needs absolute) -- * to absolute value (e.g. ~ to home, scandir needs absolute)
-- * to ensure / at the end (or no /) gets taken into account -- * to ensure / at the end (or no /) gets taken into account
@ -16,6 +21,8 @@ function ls.get_anchors_and_paths(path, recursive)
local anchorreg = '^.*/?(' .. o.anchor().regex .. ')[^/]*%' .. local anchorreg = '^.*/?(' .. o.anchor().regex .. ')[^/]*%' ..
o.zettel().extension .. '$' o.zettel().extension .. '$'
path = cleanPath(path)
local handle = vim.loop.fs_scandir(path) local handle = vim.loop.fs_scandir(path)
while handle do while handle do
local name, ftype = vim.loop.fs_scandir_next(handle) local name, ftype = vim.loop.fs_scandir_next(handle)
@ -27,7 +34,7 @@ function ls.get_anchors_and_paths(path, recursive)
end end
local anchor = string.match(name, anchorreg) local anchor = string.match(name, anchorreg)
if anchor then zettel[tostring(anchor)] = name end if anchor then zettel[tostring(anchor)] = path .. "/" .. name end
end end
return zettel return zettel
end end

View file

@ -27,7 +27,9 @@ describe("get_anchors_and_paths", function()
local file_list = {"1910291645 this-is-a-testfile.md"} local file_list = {"1910291645 this-is-a-testfile.md"}
_G.vim = get_api_mock(file_list) _G.vim = get_api_mock(file_list)
local expected = {["1910291645"] = "1910291645 this-is-a-testfile.md"} local expected = {
["1910291645"] = "someDir/1910291645 this-is-a-testfile.md"
}
assert.same(expected, ls.get_anchors_and_paths("someDir")) assert.same(expected, ls.get_anchors_and_paths("someDir"))
end) end)
@ -40,8 +42,8 @@ describe("get_anchors_and_paths", function()
_G.vim = get_api_mock(file_list) _G.vim = get_api_mock(file_list)
local expected = { local expected = {
["1910291645"] = "1910291645 this-is-a-testfile.md", ["1910291645"] = "someDir/1910291645 this-is-a-testfile.md",
["2010261208"] = "2010261208 this-should-be-picked-up.md" ["2010261208"] = "someDir/2010261208 this-should-be-picked-up.md"
} }
assert.same(expected, ls.get_anchors_and_paths("someDir")) assert.same(expected, ls.get_anchors_and_paths("someDir"))
end) end)
@ -112,10 +114,10 @@ describe("get_anchors_and_paths", function()
} }
_G.vim = vim_api_mock _G.vim = vim_api_mock
local expected = { local expected = {
["1234567890"] = "1234567890 myfile.md", ["1234567890"] = "mydirectory/1234567890 myfile.md",
["2345678901"] = "2345678901 another.md", ["2345678901"] = "mydirectory/2345678901 another.md",
["2222222222"] = "2222222222 should-be-present.md", ["2222222222"] = "mydirectory/subdir/2222222222 should-be-present.md",
["3333333333"] = "3333333333 should-also-be-present.md" ["3333333333"] = "mydirectory/subdir/3333333333 should-also-be-present.md"
} }
assert.same(expected, ls.get_anchors_and_paths('mydirectory', true)) assert.same(expected, ls.get_anchors_and_paths('mydirectory', true))
end) end)
@ -126,8 +128,8 @@ describe("get_anchors_and_paths", function()
vim.g['zettel_extension'] = '.wiki' vim.g['zettel_extension'] = '.wiki'
local expected = { local expected = {
["1910291645"] = "1910291645 myfile.wiki", ["1910291645"] = "mydirectory/1910291645 myfile.wiki",
["2345678901"] = "2345678901 another.wiki" ["2345678901"] = "mydirectory/2345678901 another.wiki"
} }
assert.same(expected, assert.same(expected,