diff --git a/lua/zettelkasten/action.lua b/lua/zettelkasten/action.lua new file mode 100644 index 0000000..441ae12 --- /dev/null +++ b/lua/zettelkasten/action.lua @@ -0,0 +1,79 @@ +local A = {} + +-- Return only the link reference portion of a markdown/wiki style link +function A.extract_link(input) + if not input then return end + return input:match("%[%[(.+)|?.*%]%]") or input:match("%[.*%]%((.+)%)") +end + +-- Extracts a file name from a link and opens the corresponding file +-- in the current buffer. +-- Takes an optional input parameter +function A.open(input) + local fname = A.extract_link(input) + if not fname then return end + -- TODO follow: go to anchor, fall back to filename + vim.api.nvim_command(string.format("edit %s", fname)) +end + +-- Gets the input at the current buffer cursor and opens it +-- in the current buffer. +function A.open_selected() + -- TODO decide between currentword/restofline option + A.open(A.get_link_under_cursor()) +end + +-- Returns the word currently under cursor, the vim equivalent of yiW. +-- Takes an optional boolean flag to set the word being caught +-- to the vim equivalent of doing yiw, a more exclusive version. +function A.get_link_under_cursor(small) + local c = "" + if small then c = "" end + local word = vim.fn.expand(c) + return word +end +-- -- Returns the content of the line from the cursor onwards. +-- function A.get_next_link_on_line() +-- local line = vim.api.nvim_get_current_line() +-- local linenr = vim.api.nvim_win_get_cursor(0)[1] +-- print(line, linenr) +-- return "" +-- end + +return {open = A.open, open_selected = A.open_selected} + +-- local function get_selection() +-- s_start = vim.fn.line("'<") - 1 +-- s_end = vim.fn.line("'>") +-- return vim.api.nvim_buf_get_lines(0, s_start, s_end, true) +-- end + +-- -- UGLY HACKS ABOUND +-- function ZK.create_zettel() +-- -- get line and its number +-- local selection +-- local line = vim.api.nvim_get_current_line() +-- local linenr = vim.api.nvim_win_get_cursor(0)[1] + +-- -- get words under cursor / selected +-- local mode = vim.api.nvim_get_mode()['mode'] +-- if mode == "n" then +-- print(vim.fn.line("'<'") - 1) +-- selection = vim.fn.expand("") +-- -- NOT WORKING YET +-- elseif mode == "v" then +-- selection = get_selection() +-- else +-- return +-- end + +-- -- get valid link +-- local link = l.create(nil, selection) + +-- -- create new line with selection replaced in middle +-- local st, en = line:find(selection, 0, true) +-- local repl_line = line:sub(1, st - 1) .. link .. line:sub(en + 1) + +-- -- replace existing line in favor of new one +-- vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, true, {repl_line}) +-- end diff --git a/lua/zettelkasten/action_spec.lua b/lua/zettelkasten/action_spec.lua new file mode 100644 index 0000000..49fcbae --- /dev/null +++ b/lua/zettelkasten/action_spec.lua @@ -0,0 +1,32 @@ +action = require 'zettelkasten.action' + +before_each(function() _G.vim = {g = {}, b = {}} end) +after_each(function() _G.vim = nil end) + +describe("open", function() + it("should open file in editor if it is a valid link", function() + vim.api = {nvim_command = mock(function() end)} + + action.open("[some text](1910271456_link-to-my-file.md)") + assert.spy(vim.api.nvim_command).was_called_with( + "edit 1910271456_link-to-my-file.md") + end) + it("should not fail when no input is available", function() + vim.fn = {expand = function() end} + assert.is_not_error(action.open) + end) +end) +describe("open_selected", function() + it("should open the next link found if no argument passed in", function() + vim.api = {nvim_command = mock(function() end)} + vim.fn = { + expand = function(sure) + return "[" .. sure .. "](1910271456_link-to-my-file.md)" + end + } + + action.open_selected() + assert.spy(vim.api.nvim_command).was_called_with( + "edit 1910271456_link-to-my-file.md") + end) +end) diff --git a/lua/zettelkasten/init.lua b/lua/zettelkasten/init.lua index be8d3fd..91b4b7c 100644 --- a/lua/zettelkasten/init.lua +++ b/lua/zettelkasten/init.lua @@ -16,6 +16,4 @@ end -- composed of yymmddHHMM. function ZK.create_anchor() return a.create() end -return { - get_zettel_list = ZK.get_zettel_list -} +return {get_zettel_list = ZK.get_zettel_list, create_anchor = ZK.create_anchor}