Refactor link cleaning and transformation functions

This commit is contained in:
Marty Oehme 2020-10-30 18:20:04 +01:00
parent cac9720d35
commit aa3e1eb754
Signed by: Marty
GPG key ID: B7538B8F50A1C800
3 changed files with 119 additions and 44 deletions

View file

@ -25,6 +25,7 @@ start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the fi
* [ ] options * [ ] options
* [x] zettel anchor separator * [x] zettel anchor separator
* [x] zettel extension * [x] zettel extension
* [ ] link style (wiki/markdown) custom?
* [ ] recursive lookup for zettel * [ ] recursive lookup for zettel
* [ ] zettel anchor regex * [ ] zettel anchor regex
* [ ] backlinks (via rg for filename anchor?) * [ ] backlinks (via rg for filename anchor?)

View file

@ -3,17 +3,52 @@ local L = {}
local o = require 'zettelkasten.options' local o = require 'zettelkasten.options'
local a = require 'zettelkasten.anchor' local a = require 'zettelkasten.anchor'
-- Returns a link to a markdown file with the date replaced with a zettel anchor, -- TODO split up into clean/anchor/style functions, make private
-- and the text cleaned up to be useful in a link. -- Returns a link to a markdown file with the anchor replaced with a zettel anchor,
function L.create_link_text(text, date) -- Returns the text passed in with the anchor passed in prepended,
function L.prepend_anchor(anchor, text)
if not text or text == "" then return anchor end
text = anchor .. o.anchor().separator .. text
return text
end
-- Returns the text cleaned up to be more useful in a link.
-- Spaces are replaced by dashes and everything is lowercased.
function L.clean(text)
text = text or "" text = text or ""
if text == "" then return text:lower():gsub(" ", "-")
text = "" .. a.create_anchor(date) end
else
text = text:lower():gsub(" ", "-") -- Returns the untouched text with the extension set in options appended
text = "" .. a.create_anchor(date) .. o.anchor().separator .. text -- at the end.
end function L.append_extension(text) return text .. o.zettel().extension end
return text .. o.zettel().extension
local function check_link_empty(link)
if not link or link == "" then error("Link is not allowed to be empty.") end
end
local function 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)
check_link_empty(link)
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)
check_link_empty(link)
local pipe = ""
text = trimmed(text)
if text and text ~= "" then pipe = "|" .. text end
return "[[" .. link .. pipe .. "]]"
end end
return L return L

View file

@ -1,52 +1,91 @@
ZK = require 'zettelkasten.link' link = require 'zettelkasten.link'
Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45} Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
before_each(function() _G.vim = {g = {}, b = {}} end) before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end) after_each(function() _G.vim = nil end)
describe("link creation", function() describe("prepend_anchor", function()
it( it("should append text to link", function()
"should return a markdown link with only zettel anchor on no text passed in", assert.same("1910291645_isappended",
function() link.prepend_anchor("1910291645", "isappended"))
assert.same("1910291645.md", ZK.create_link_text(nil, Test_date)) end)
end) it("should not add a separator if no text appended", function()
assert.same("1910291645", link.prepend_anchor("1910291645", ""))
it("should text to link", function()
assert.same("1910291645_isappended.md",
ZK.create_link_text("isappended", Test_date))
end) end)
it("should return lowercased link text", function() it("should return solely the anchor if no text is passed in", function()
assert.same("1910291645_yesiamshouting.md", assert.same("1910291645", link.prepend_anchor("1910291645", nil))
ZK.create_link_text("YESIAMSHOUTING", Test_date))
end) end)
it("should return solely the anchor if empty text is passed in", function()
it("should return spaces in text replaced with dashes", function() assert.same("1910291645", link.prepend_anchor("1910291645", ""))
assert.same("1910291645_yes-indeed-a-space.md",
ZK.create_link_text("yes indeed a space", Test_date))
end) end)
it("should add contents of g:zettel_anchor_separator variable to link", it("should add contents of g:zettel_anchor_separator variable to link",
function() function()
vim.g.zettel_anchor_separator = "SEP" vim.g.zettel_anchor_separator = "SEP"
assert.same("1910291645SEParated.md", assert.same("1910291645SEParated",
ZK.create_link_text("arated", Test_date)) link.prepend_anchor("1910291645", "arated"))
end) end)
it("should add contents of b:zettel_anchor_separator variable to link", it("should add contents of b:zettel_anchor_separator variable to link",
function() function()
vim.b.zettel_anchor_separator = "---" vim.b.zettel_anchor_separator = "---"
assert.same("1910291645---arated.md", assert.same("1910291645---arated",
ZK.create_link_text("arated", Test_date)) link.prepend_anchor("1910291645", "arated"))
end) end)
end)
it("should append the filetype set in g:zettel_extension", function()
vim.g.zettel_extension = ".something" describe("clean", function()
assert.same("1910291645_theworld.something", it("should return lowercased link text", function()
ZK.create_link_text("theworld", Test_date)) assert.same("yesiamshouting", link.clean("YESIAMSHOUTING"))
end) end)
it("should append the filetype set in b:zettel_extension", function()
vim.b.zettel_extension = ".somethingelse" it("should return spaces in text replaced with dashes", function()
assert.same("1910291645_theworld.somethingelse", assert.same("yes-indeed-a-space", link.clean("yes indeed a space"))
ZK.create_link_text("theworld", Test_date)) end)
end)
describe("append_extension", function()
it("should append the contents set in global zettel extension option",
function()
vim.g.zettel_extension = ".extension"
assert.same("myfile.extension", link.append_extension("myfile"))
end)
it("should append the contents set in global zettel extension option",
function()
vim.b.zettel_extension = ".bufext"
assert.same("myfile.bufext", link.append_extension("myfile"))
end)
end)
describe("style_markdown", function()
it("should correctly apply transformations to link and text", function()
assert.same("[My AWESOME Link](1910291645_my-awesome-link.md)",
link.style_markdown("1910291645_my-awesome-link.md",
"My AWESOME Link"))
end)
it("should trim whitespace for the text area", function()
assert.same("[](1910291645_my-awesome-link.md)",
link.style_markdown("1910291645_my-awesome-link.md", " "))
assert.same("[hi](1910291645_my-awesome-link.md)", link.style_markdown(
"1910291645_my-awesome-link.md", " hi "))
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)
describe("style_wiki", 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.style_wiki("1910291645", "My AWESOME Link"))
end)
it("should trim whitespace for the text area", function()
assert.same("[[1910291645]]", link.style_wiki("1910291645", " "))
assert.same("[[1910291645|hi]]",
link.style_wiki("1910291645", " hi "))
end) end)
end) end)