Refactor link creation into parser style_func

This commit is contained in:
Marty Oehme 2020-11-04 22:16:21 +01:00
parent 2a3e213ac8
commit e95bcf53b4
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
2 changed files with 7 additions and 50 deletions

View File

@ -7,19 +7,20 @@ local parsers = {
markdown = {
ref = "%[.-%]%((.-)%)",
text = "%[(.-)%]%(.-%)",
style_func = function(link, text, extension)
style_func = function(anchor, text, extension)
local link = (a.prepend(anchor, L.urlify(L.trimmed(text))))
return "[" .. L.trimmed(text) .. "](" .. link .. extension .. ")"
end
},
wiki = {
ref = "%[%[(.-)|?.-%]%]",
text = "%[%[.-|?(.-)%]%]",
style_func = function(link, text)
local pipe = ""
style_func = function(anchor, text)
local pipetxt = ""
text = L.trimmed(text)
if text and text ~= "" then pipe = "|" .. text end
return "[[" .. link .. pipe .. "]]"
if text and text ~= "" then pipetxt = "|" .. text end
return "[[" .. anchor .. pipetxt .. "]]"
end
}
}
@ -35,12 +36,6 @@ end
-- at the end.
function L.append_extension(text) return text .. o.zettel().extension end
local function must_have(content)
if not content or content == "" then
error("Link is not allowed to be empty.")
end
end
-- Returns text with surrounding whitespace trimmed. Returns empty string
-- if only whitespace.
function L.trimmed(text)
@ -48,25 +43,6 @@ function L.trimmed(text)
return text:match '^()%s*$' and '' or text:match '^%s*(.*%S)'
end
-- Returns a markdown-compatible transformation of the link and text combination
-- passed in.
function L.style_markdown(link, text)
must_have(link)
return "[" .. L.trimmed(text) .. "](" .. link .. ")"
end
-- Returns a wikilink-compatible transformation of the link and text combination
-- passed in, adding the text as a pipe if it exists.
function L.style_wiki(link, text)
must_have(link)
local pipe = ""
text = L.trimmed(text)
if text and text ~= "" then pipe = "|" .. text end
return "[[" .. link .. pipe .. "]]"
end
-- 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.
@ -74,14 +50,7 @@ end
function L.create(anchor, text, style)
style = style or o.zettel().link_style
if style == "markdown" then
local link = (a.prepend(anchor, L.urlify(L.trimmed(text))))
return parsers.markdown.style_func(link, text, o.zettel().extension)
elseif style == "wiki" then
return parsers.wiki.style_func(anchor, text)
end
error("Link creation failed.")
return parsers[style].style_func(anchor, text, o.zettel().extension)
end
-- Returns a correctly formatted link to a new zettel (without anchor).

View File

@ -49,10 +49,6 @@ describe("create", function()
assert.same("[[1910291645]]", link.create("1910291645"))
end)
describe("wiki link styling", function()
it("should error if no link provided", function()
assert.is_error(function() link.style_wiki("", "mytext") end)
assert.is_error(function() link.style_wiki(nil, "mytext") end)
end)
it("should correctly apply transformations to link and text", function()
assert.same("[[1910291645|My AWESOME Link]]",
link.create("1910291645", "My AWESOME Link", "wiki"))
@ -76,14 +72,6 @@ describe("create", function()
assert.same("[hi](1910291645_hi.md)",
link.create("1910291645", " hi ", "markdown"))
end)
it("should error if no link provided", function()
assert.is_error(function()
link.style_markdown("", "mytext")
end)
assert.is_error(function()
link.style_markdown(nil, "mytext")
end)
end)
end)
end)