diff --git a/lua/zettelkasten/action.lua b/lua/zettelkasten/action.lua index 353ee17..1d9d276 100644 --- a/lua/zettelkasten/action.lua +++ b/lua/zettelkasten/action.lua @@ -2,6 +2,7 @@ local A = {} local o = require 'zettelkasten.options' local link = require 'zettelkasten.link' +local list = require 'zettelkasten.list' local BIGNUMBER = 10000000 @@ -9,8 +10,8 @@ local BIGNUMBER = 10000000 -- Requires a link object passed in. function A.open(link) if not link or not link.ref then return end - -- TODO follow: go to anchor, fall back to filename - vim.api.nvim_command(string.format("edit %s", link.ref)) + local fname = list.get_zettel(link.anchor) or link.ref + vim.api.nvim_command(string.format("edit %s", fname)) end -- Gets the input at the current buffer cursor and opens it diff --git a/lua/zettelkasten/action_spec.lua b/lua/zettelkasten/action_spec.lua index 1c956f4..d04bf23 100644 --- a/lua/zettelkasten/action_spec.lua +++ b/lua/zettelkasten/action_spec.lua @@ -1,6 +1,8 @@ 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) describe("open", function() @@ -15,6 +17,16 @@ 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() + 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) describe("open_selected", function() diff --git a/lua/zettelkasten/link_spec.lua b/lua/zettelkasten/link_spec.lua index f28033c..73e3d6c 100644 --- a/lua/zettelkasten/link_spec.lua +++ b/lua/zettelkasten/link_spec.lua @@ -94,7 +94,8 @@ end) describe("extract_all", 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 = { { endpos = 34, diff --git a/lua/zettelkasten/list.lua b/lua/zettelkasten/list.lua index 6525498..a6bae7a 100644 --- a/lua/zettelkasten/list.lua +++ b/lua/zettelkasten/list.lua @@ -7,6 +7,11 @@ local function isDirectory(ftype) return false end +local function cleanPath(path) + if path:match("^~") then path = os.getenv("HOME") .. path:sub(2) end + return path +end + -- TODO transform paths: -- * to absolute value (e.g. ~ to home, scandir needs absolute) -- * 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 .. ')[^/]*%' .. o.zettel().extension .. '$' + path = cleanPath(path) + local handle = vim.loop.fs_scandir(path) while handle do local name, ftype = vim.loop.fs_scandir_next(handle) @@ -27,7 +34,7 @@ function ls.get_anchors_and_paths(path, recursive) end local anchor = string.match(name, anchorreg) - if anchor then zettel[tostring(anchor)] = name end + if anchor then zettel[tostring(anchor)] = path .. "/" .. name end end return zettel end diff --git a/lua/zettelkasten/list_spec.lua b/lua/zettelkasten/list_spec.lua index e97c42d..07e3299 100644 --- a/lua/zettelkasten/list_spec.lua +++ b/lua/zettelkasten/list_spec.lua @@ -27,7 +27,9 @@ describe("get_anchors_and_paths", function() local file_list = {"1910291645 this-is-a-testfile.md"} _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")) end) @@ -40,8 +42,8 @@ describe("get_anchors_and_paths", function() _G.vim = get_api_mock(file_list) local expected = { - ["1910291645"] = "1910291645 this-is-a-testfile.md", - ["2010261208"] = "2010261208 this-should-be-picked-up.md" + ["1910291645"] = "someDir/1910291645 this-is-a-testfile.md", + ["2010261208"] = "someDir/2010261208 this-should-be-picked-up.md" } assert.same(expected, ls.get_anchors_and_paths("someDir")) end) @@ -112,10 +114,10 @@ describe("get_anchors_and_paths", function() } _G.vim = vim_api_mock local expected = { - ["1234567890"] = "1234567890 myfile.md", - ["2345678901"] = "2345678901 another.md", - ["2222222222"] = "2222222222 should-be-present.md", - ["3333333333"] = "3333333333 should-also-be-present.md" + ["1234567890"] = "mydirectory/1234567890 myfile.md", + ["2345678901"] = "mydirectory/2345678901 another.md", + ["2222222222"] = "mydirectory/subdir/2222222222 should-be-present.md", + ["3333333333"] = "mydirectory/subdir/3333333333 should-also-be-present.md" } assert.same(expected, ls.get_anchors_and_paths('mydirectory', true)) end) @@ -126,8 +128,8 @@ describe("get_anchors_and_paths", function() vim.g['zettel_extension'] = '.wiki' local expected = { - ["1910291645"] = "1910291645 myfile.wiki", - ["2345678901"] = "2345678901 another.wiki" + ["1910291645"] = "mydirectory/1910291645 myfile.wiki", + ["2345678901"] = "mydirectory/2345678901 another.wiki" } assert.same(expected,