diff --git a/lua/zettelkasten/link.lua b/lua/zettelkasten/link.lua index b7ae5f9..d78886e 100644 --- a/lua/zettelkasten/link.lua +++ b/lua/zettelkasten/link.lua @@ -51,4 +51,21 @@ function L.style_wiki(link, text) return "[[" .. link .. pipe .. "]]" end +-- Returns the correctly formatted link to a zettel with the anchor passed in. +-- Takes an optional link text which will be added to the link. +-- Takes an optional style according to which the link will be transformed. +function L.create(anchor, text, style) + local link = L.clean(text) + style = style or o.zettel().link_style + + if style == "markdown" then + link = L.append_extension(L.prepend_anchor(anchor, link)) + return L.style_markdown(link, text) + + elseif style == "wiki" then + return L.style_wiki(anchor, text) + end + error("Link creation failed.") +end + return L diff --git a/lua/zettelkasten/link_spec.lua b/lua/zettelkasten/link_spec.lua index 66a26e3..58bab50 100644 --- a/lua/zettelkasten/link_spec.lua +++ b/lua/zettelkasten/link_spec.lua @@ -89,3 +89,20 @@ describe("style_wiki", function() link.style_wiki("1910291645", " hi ")) end) end) + +describe("create_link", function() + it("should create a working link using set options in vim", function() + vim.g.zettel_extension = ".md" + vim.g.zettel_anchor_separator = "_" + vim.g.zettel_link_style = "markdown" + assert.same("[My FILE NAME](1910291645_my-file-name.md)", + link.create("1910291645", "My FILE NAME")) + end) + it("should create a working link if style is manually set", function() + vim.g.zettel_extension = ".md" + vim.g.zettel_anchor_separator = "_" + vim.g.zettel_link_style = "markdown" + assert.same("[[1910291645|My FILE NAME]]", + link.create("1910291645", "My FILE NAME", "wiki")) + end) +end) diff --git a/lua/zettelkasten/options.lua b/lua/zettelkasten/options.lua index f748d74..f47d9a8 100644 --- a/lua/zettelkasten/options.lua +++ b/lua/zettelkasten/options.lua @@ -2,6 +2,7 @@ local Opt = {} -- setting defaults local ZETTEL_EXTENSION = ".md" +local ZETTEL_LINK_STYLE = "markdown" local ANCHOR_SEPARATOR = "_" -- 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` @@ -11,6 +12,12 @@ function Opt.zettel() options.extension = vim.g["zettel_extension"] or vim.b["zettel_extension"] or ZETTEL_EXTENSION + options.link_style = vim.g["zettel_link_style"] or + vim.b["zettel_link_style"] or ZETTEL_LINK_STYLE + + if options.link_style ~= "wiki" and options.link_style ~= "markdown" then + error("zettel_link_style can only be set to markdown or wiki.") + end return options end diff --git a/lua/zettelkasten/options_spec.lua b/lua/zettelkasten/options_spec.lua index 0ea22bd..f1dda71 100644 --- a/lua/zettelkasten/options_spec.lua +++ b/lua/zettelkasten/options_spec.lua @@ -28,4 +28,19 @@ describe("zettel options", function() end) it("should return the default anchor separator if not set in vim", function() assert.same("_", opt.anchor().separator) end) + + it("should return the global link style if set in vim", function() + _G.vim.g.zettel_link_style = "wiki" + assert.same("wiki", opt.zettel().link_style) + end) + it("should return the buffer link style if set in vim", function() + _G.vim.b.zettel_link_style = "wiki" + assert.same("wiki", opt.zettel().link_style) + end) + it("should return the default link style if not set in vim", + function() assert.same("markdown", opt.zettel().link_style) end) + it("should error on entries other than markdown/wiki", function() + _G.vim.g.zettel_link_style = "idontbelong" + assert.is_error(function() opt.zettel() end) + end) end)