2020-10-30 15:30:37 +00:00
|
|
|
local L = {}
|
|
|
|
|
|
|
|
local o = require 'zettelkasten.options'
|
|
|
|
local a = require 'zettelkasten.anchor'
|
|
|
|
|
2020-10-30 17:20:04 +00:00
|
|
|
-- Returns the text cleaned up to be more useful in a link.
|
|
|
|
-- Spaces are replaced by dashes and everything is lowercased.
|
2020-10-31 10:01:53 +00:00
|
|
|
function L.urlify(text)
|
2020-10-30 15:30:37 +00:00
|
|
|
text = text or ""
|
2020-10-30 17:20:04 +00:00
|
|
|
return text:lower():gsub(" ", "-")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns the untouched text with the extension set in options appended
|
|
|
|
-- at the end.
|
|
|
|
function L.append_extension(text) return text .. o.zettel().extension end
|
|
|
|
|
2020-10-31 10:01:53 +00:00
|
|
|
local function must_have(content)
|
|
|
|
if not content or content == "" then
|
|
|
|
error("Link is not allowed to be empty.")
|
|
|
|
end
|
2020-10-30 17:20:04 +00:00
|
|
|
end
|
|
|
|
|
2020-10-31 10:01:53 +00:00
|
|
|
-- Returns text with surrounding whitespace trimmed. Returns empty string
|
|
|
|
-- if only whitespace.
|
2020-10-30 17:20:04 +00:00
|
|
|
local function trimmed(text)
|
2020-10-31 10:12:37 +00:00
|
|
|
if not text then return end
|
2020-10-30 17:20:04 +00:00
|
|
|
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)
|
2020-10-31 10:01:53 +00:00
|
|
|
must_have(link)
|
2020-10-30 17:20:04 +00:00
|
|
|
|
|
|
|
return "[" .. 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)
|
2020-10-31 10:01:53 +00:00
|
|
|
must_have(link)
|
2020-10-30 17:20:04 +00:00
|
|
|
local pipe = ""
|
|
|
|
text = trimmed(text)
|
|
|
|
|
|
|
|
if text and text ~= "" then pipe = "|" .. text end
|
|
|
|
return "[[" .. link .. pipe .. "]]"
|
2020-10-30 15:30:37 +00:00
|
|
|
end
|
|
|
|
|
2020-10-31 10:12:37 +00:00
|
|
|
-- Returns a correctly formatted link to a zettel.
|
|
|
|
-- Requires an anchor to be passed in.
|
2020-10-30 17:44:50 +00:00
|
|
|
-- 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)
|
|
|
|
style = style or o.zettel().link_style
|
|
|
|
|
|
|
|
if style == "markdown" then
|
2020-10-31 10:01:53 +00:00
|
|
|
local link = (a.prepend(anchor, L.urlify(text)))
|
|
|
|
return L.style_markdown(L.append_extension(link), text)
|
2020-10-30 17:44:50 +00:00
|
|
|
|
|
|
|
elseif style == "wiki" then
|
|
|
|
return L.style_wiki(anchor, text)
|
|
|
|
end
|
|
|
|
error("Link creation failed.")
|
|
|
|
end
|
|
|
|
|
2020-10-31 10:37:55 +00:00
|
|
|
-- Returns a correctly formatted link to a new zettel (without anchor).
|
|
|
|
-- 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.new(text, style)
|
|
|
|
local anchor = a.create()
|
|
|
|
return L.create(anchor, text, style)
|
|
|
|
end
|
|
|
|
|
2020-10-30 15:30:37 +00:00
|
|
|
return L
|