diff --git a/lua/zettelkasten/link.lua b/lua/zettelkasten/link.lua index 9dcf77f..08f4fc0 100644 --- a/lua/zettelkasten/link.lua +++ b/lua/zettelkasten/link.lua @@ -23,6 +23,7 @@ end -- Returns text with surrounding whitespace trimmed. Returns empty string -- if only whitespace. local function trimmed(text) + if not text then return end return text:match '^()%s*$' and '' or text:match '^%s*(.*%S)' end @@ -45,7 +46,8 @@ function L.style_wiki(link, text) return "[[" .. link .. pipe .. "]]" end --- Returns a correctly formatted link to a new zettel. +-- Returns a correctly formatted link to a zettel. +-- Requires an anchor to be 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) diff --git a/lua/zettelkasten/link_spec.lua b/lua/zettelkasten/link_spec.lua index 36c3fb0..7a236a1 100644 --- a/lua/zettelkasten/link_spec.lua +++ b/lua/zettelkasten/link_spec.lua @@ -61,7 +61,7 @@ describe("style_wiki", function() end) end) -describe("create_link", function() +describe("create", function() it("should create a working link using set options in vim", function() vim.g.zettel_extension = ".md" vim.g.zettel_anchor_separator = "_" @@ -76,4 +76,10 @@ describe("create_link", function() assert.same("[[1910291645|My FILE NAME]]", link.create("1910291645", "My FILE NAME", "wiki")) end) + it("should handle empty text", function() + vim.g.zettel_extension = ".wiki" + vim.g.zettel_anchor_separator = "_" + vim.g.zettel_link_style = "wiki" + assert.same("[[1910291645]]", link.create("1910291645")) + end) end)