diff --git a/README.md b/README.md index 6b52243..a9ea1c0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ To develop / debug: start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the files in project dir as if they were on path +next up: +* link following option (under cursor, next on line) +* next link on line function (actions) +* helper function to decide which one to use from A.open + ## TODO: needed functionality * [ ] note creation (new anchor) @@ -27,11 +32,13 @@ start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the fi * [x] zettel extension * [x] link style (wiki/markdown) * [ ] custom link style? + * [ ] link detection/following (under word, next on line) * [ ] recursive dir lookup for zettel * [ ] zettel anchor regex ## TODO: nice-to-haves +* [ ] refactor parsers (md/wiki) to be tables of functions/regex in options, so e.g. valid link detection can call `options.parser.isValidLink(link)` or transformation `options.parser.styleLink(anchor, text)` * [ ] completion engine (e.g. for `completion-nvim`, look in completion_buffers/completion-tags for reference) * [ ] zettel caching for big directories * [ ] backlinks (via rg for filename anchor?) diff --git a/lua/zettelkasten/action.lua b/lua/zettelkasten/action.lua index 441ae12..0f3dbd4 100644 --- a/lua/zettelkasten/action.lua +++ b/lua/zettelkasten/action.lua @@ -1,11 +1,5 @@ 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 @@ -23,6 +17,12 @@ function A.open_selected() A.open(A.get_link_under_cursor()) end +-- 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 + -- 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. @@ -32,6 +32,7 @@ function A.get_link_under_cursor(small) 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() diff --git a/lua/zettelkasten/options.lua b/lua/zettelkasten/options.lua index bf80889..108edfc 100644 --- a/lua/zettelkasten/options.lua +++ b/lua/zettelkasten/options.lua @@ -1,14 +1,16 @@ local Opt = {} -- setting defaults -local ZETTEL_EXTENSION = ".md" -local ZETTEL_LINK_STYLE = "markdown" -local ANCHOR_SEPARATOR = "_" +local ZETTEL_EXTENSION = ".md" -- ending of zettel files +local ZETTEL_LINK_STYLE = "markdown" -- "wiki", sets the style of link to use +local ZETTEL_LINK_FOLLOWING = "cursor" -- "line", sets the distance ahead to look for zettel links +local ANCHOR_SEPARATOR = "_" -- separtor between anchor and link text in md links -- TODO zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes", -- TODO zettel_anchor_pattern = regex? -> needs custom creation function in `create_anchor` -- constricted option sets local ZETTEL_LINK_STYLE_OPTIONS = {markdown = true, wiki = true} +local ZETTEL_LINK_FOLLOWING_OPTIONS = { cursor = true, line = true } local function must_contain(set, value, name) if type(set) ~= "table" then return false end @@ -31,6 +33,9 @@ function Opt.zettel() must_contain(ZETTEL_LINK_STYLE_OPTIONS, options.link_style, "zettel_link_style") + options.link_following = vim.g["zettel_link_following"] or vim.b["zettel_link_following"] or ZETTEL_LINK_FOLLOWING + must_contain(ZETTEL_LINK_FOLLOWING_OPTIONS, options.link_following, "zettel_link_following") + return options end diff --git a/lua/zettelkasten/options_spec.lua b/lua/zettelkasten/options_spec.lua index f1dda71..2a97239 100644 --- a/lua/zettelkasten/options_spec.lua +++ b/lua/zettelkasten/options_spec.lua @@ -43,4 +43,19 @@ describe("zettel options", function() _G.vim.g.zettel_link_style = "idontbelong" assert.is_error(function() opt.zettel() end) end) + + it("should return the global link following if set in vim", function() + _G.vim.g.zettel_link_following = "line" + assert.same("line", opt.zettel().link_following) + end) + it("should return the buffer link following if set in vim", function() + _G.vim.b.zettel_link_following = "line" + assert.same("line", opt.zettel().link_following) + end) + it("should return the default link following if not set in vim", + function() assert.same("cursor", opt.zettel().link_following) end) + it("should error on entries other than markdown/wiki", function() + _G.vim.g.zettel_link_following = "idontbelong" + assert.is_error(function() opt.zettel() end) + end) end)