Add link formatting function

Function takes an anchor (and optional link text and formatting style)
and transforms the input into a link to an anchor id.

Currently works for markdown and wikilink style link transforms.
This commit is contained in:
Marty Oehme 2020-10-30 18:44:50 +01:00
parent aa3e1eb754
commit 78a1948545
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
4 changed files with 56 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)