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 = { markdown = {
ref = "%[.-%]%((.-)%)", ref = "%[.-%]%((.-)%)",
text = "%[(.-)%]%(.-%)", 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 .. ")" return "[" .. L.trimmed(text) .. "](" .. link .. extension .. ")"
end end
}, },
wiki = { wiki = {
ref = "%[%[(.-)|?.-%]%]", ref = "%[%[(.-)|?.-%]%]",
text = "%[%[.-|?(.-)%]%]", text = "%[%[.-|?(.-)%]%]",
style_func = function(link, text) style_func = function(anchor, text)
local pipe = "" local pipetxt = ""
text = L.trimmed(text) text = L.trimmed(text)
if text and text ~= "" then pipe = "|" .. text end if text and text ~= "" then pipetxt = "|" .. text end
return "[[" .. link .. pipe .. "]]" return "[[" .. anchor .. pipetxt .. "]]"
end end
} }
} }
@ -35,12 +36,6 @@ end
-- at the end. -- at the end.
function L.append_extension(text) return text .. o.zettel().extension 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 -- Returns text with surrounding whitespace trimmed. Returns empty string
-- if only whitespace. -- if only whitespace.
function L.trimmed(text) function L.trimmed(text)
@ -48,25 +43,6 @@ function L.trimmed(text)
return text:match '^()%s*$' and '' or text:match '^%s*(.*%S)' return text:match '^()%s*$' and '' or text:match '^%s*(.*%S)'
end 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. -- Returns a correctly formatted link to a zettel.
-- Requires an anchor to be passed in. -- Requires an anchor to be passed in.
-- Takes an optional link text which will be added to the link. -- Takes an optional link text which will be added to the link.
@ -74,14 +50,7 @@ end
function L.create(anchor, text, style) function L.create(anchor, text, style)
style = style or o.zettel().link_style style = style or o.zettel().link_style
if style == "markdown" then return parsers[style].style_func(anchor, text, o.zettel().extension)
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.")
end end
-- Returns a correctly formatted link to a new zettel (without anchor). -- 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")) assert.same("[[1910291645]]", link.create("1910291645"))
end) end)
describe("wiki link styling", function() 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() it("should correctly apply transformations to link and text", function()
assert.same("[[1910291645|My AWESOME Link]]", assert.same("[[1910291645|My AWESOME Link]]",
link.create("1910291645", "My AWESOME Link", "wiki")) link.create("1910291645", "My AWESOME Link", "wiki"))
@ -76,14 +72,6 @@ describe("create", function()
assert.same("[hi](1910291645_hi.md)", assert.same("[hi](1910291645_hi.md)",
link.create("1910291645", " hi ", "markdown")) link.create("1910291645", " hi ", "markdown"))
end) 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)
end) end)