Refactor anchor and link functions into files

This commit is contained in:
Marty Oehme 2020-10-30 16:30:37 +01:00
parent ece30350c2
commit cac9720d35
Signed by: Marty
GPG key ID: B7538B8F50A1C800
7 changed files with 117 additions and 112 deletions

View file

@ -10,20 +10,22 @@ start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the fi
* [x] create anchor
* [ ] *unique* anchor creation
* [ ] create link (md / wiki)
* [ ] link creation (to existing note)
* [ ] list existing
* [ ] create link (md / wiki)
* [ ] link following (to existing anchor)
* [ ] note search (title / full-text)
* [ ] note listing (anchors / titles, no anchor)
* [ ] list anchors
* [ ] list filenames
* [ ] link following (to existing anchor)
* [ ] link creation (to existing note)
* [ ] list existing
* [ ] create link (md / wiki)
* [ ] link switching (to another existing note)
* [ ] note search (title / full-text)
* [ ] jump to zettel (open existing anchor)
* [ ] select by anchor
* [ ] select by (fuzzy) title match
* [ ] options
* [x] zettel anchor separator
* [ ] zettel extension
* [x] zettel extension
* [ ] recursive lookup for zettel
* [ ] zettel anchor regex
* [ ] backlinks (via rg for filename anchor?)
* [ ] keep tree of notes cached?

View file

@ -0,0 +1,18 @@
local A = {}
-- Return a valid zettelkasten anchor,
-- composed of yymmddHHMM.
--
-- date can be passed in as a table containing a year, a month, a day, an hour,
-- and a minute key. Returns nil if the date passed in is invalid.
-- If no date is passed in, returns Zettel anchor for current moment.
function A.create_anchor(date)
local timestamp
if pcall(function() timestamp = os.time(date) end) then
return os.date('%y%m%d%H%M', timestamp)
else
return nil
end
end
return A

View file

@ -0,0 +1,18 @@
local A = require 'zettelkasten.anchor'
Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("anchor creation", function()
it("should return zettel anchor from time passed in",
function() assert.same("1910291645", A.create_anchor(Test_date)) end)
it(
"should return zettel anchor from current moment if no argument passed in",
function() assert.same(os.date('%y%m%d%H%M'), A.create_anchor()) end)
it("should return nil if argument passed in is invalid", function()
assert.is_nil(A.create_anchor("My grandmother is lovely."))
end)
end)

View file

@ -2,43 +2,7 @@ local ZK = {}
local ls = require 'zettelkasten.list'
local o = require 'zettelkasten.options'
-- entrypoint for pressing the zettel key when the cursor
-- is either on an existing link (it will then
-- follow it) or over text (it will then turn it into a
-- zettel link)
function ZK.follow_link()
assert(false, "NOT IMPLEMENTED")
return ''
end
-- Return a valid zettelkasten anchor,
-- composed of yymmddHHMM.
--
-- date can be passed in as a table containing a year, a month, a day, an hour,
-- and a minute key. Returns nil if the date passed in is invalid.
-- If no date is passed in, returns Zettel anchor for current moment.
function ZK.create_anchor(date)
local timestamp
if pcall(function() timestamp = os.time(date) end) then
return os.date('%y%m%d%H%M', timestamp)
else
return nil
end
end
-- Returns a link to a markdown file with the date replaced with a zettel anchor,
-- and the text cleaned up to be useful in a link.
function ZK.create_link(text, date)
text = text or ""
if text == "" then
text = "" .. ZK.create_anchor(date)
else
text = text:lower():gsub(" ", "-")
text = "" .. ZK.create_anchor(date) .. o.anchor().separator .. text
end
return text .. o.zettel().extension
end
local a = require 'zettelkasten.anchor'
-- Returns all zettel in path as a
-- { "anchor" = "path/to/zettel/anchor filename.md" }
@ -48,9 +12,7 @@ function ZK.get_zettel_list(path, recursive)
return ls.get_anchors_and_paths(path, recursive or false, ZK.options)
end
return {
zettel_link_create = ZK.zettel_link_create,
create_anchor = ZK.create_anchor,
create_link = ZK.create_link,
get_zettel_list = ZK.get_zettel_list
}

View file

@ -1,71 +1,5 @@
ZK = require 'zettelkasten.init'
Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
describe("Zettelkasten", function()
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("anchor creation", function()
it("should return zettel anchor from time passed in", function()
assert.same("1910291645", ZK.create_anchor(Test_date))
end)
it(
"should return zettel anchor from current moment if no argument passed in",
function()
assert.same(os.date('%y%m%d%H%M'), ZK.create_anchor())
end)
it("should return nil if argument passed in is invalid", function()
assert.is_nil(ZK.create_anchor("My grandmother is lovely."))
end)
end)
describe("link creation", function()
it(
"should return a markdown link with only zettel anchor on no text passed in",
function()
assert.same("1910291645.md", ZK.create_link(nil, Test_date))
end)
it("should text to link", function()
assert.same("1910291645_isappended.md",
ZK.create_link("isappended", Test_date))
end)
it("should return lowercased link text", function()
assert.same("1910291645_yesiamshouting.md",
ZK.create_link("YESIAMSHOUTING", Test_date))
end)
it("should return spaces in text replaced with dashes", function()
assert.same("1910291645_yes-indeed-a-space.md",
ZK.create_link("yes indeed a space", Test_date))
end)
it("should add contents of g:zettel_anchor_separator variable to link",
function()
vim.g.zettel_anchor_separator = "SEP"
assert.same("1910291645SEParated.md",
ZK.create_link("arated", Test_date))
end)
it("should add contents of b:zettel_anchor_separator variable to link",
function()
vim.b.zettel_anchor_separator = "---"
assert.same("1910291645---arated.md",
ZK.create_link("arated", Test_date))
end)
it("should append the filetype set in g:zettel_extension", function()
vim.g.zettel_extension = ".something"
assert.same("1910291645_theworld.something",
ZK.create_link("theworld", Test_date))
end)
it("should append the filetype set in b:zettel_extension", function()
vim.b.zettel_extension = ".somethingelse"
assert.same("1910291645_theworld.somethingelse",
ZK.create_link("theworld", Test_date))
end)
end)
end)

19
lua/zettelkasten/link.lua Normal file
View file

@ -0,0 +1,19 @@
local L = {}
local o = require 'zettelkasten.options'
local a = require 'zettelkasten.anchor'
-- Returns a link to a markdown file with the date replaced with a zettel anchor,
-- and the text cleaned up to be useful in a link.
function L.create_link_text(text, date)
text = text or ""
if text == "" then
text = "" .. a.create_anchor(date)
else
text = text:lower():gsub(" ", "-")
text = "" .. a.create_anchor(date) .. o.anchor().separator .. text
end
return text .. o.zettel().extension
end
return L

View file

@ -0,0 +1,52 @@
ZK = require 'zettelkasten.link'
Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("link creation", function()
it(
"should return a markdown link with only zettel anchor on no text passed in",
function()
assert.same("1910291645.md", ZK.create_link_text(nil, Test_date))
end)
it("should text to link", function()
assert.same("1910291645_isappended.md",
ZK.create_link_text("isappended", Test_date))
end)
it("should return lowercased link text", function()
assert.same("1910291645_yesiamshouting.md",
ZK.create_link_text("YESIAMSHOUTING", Test_date))
end)
it("should return spaces in text replaced with dashes", function()
assert.same("1910291645_yes-indeed-a-space.md",
ZK.create_link_text("yes indeed a space", Test_date))
end)
it("should add contents of g:zettel_anchor_separator variable to link",
function()
vim.g.zettel_anchor_separator = "SEP"
assert.same("1910291645SEParated.md",
ZK.create_link_text("arated", Test_date))
end)
it("should add contents of b:zettel_anchor_separator variable to link",
function()
vim.b.zettel_anchor_separator = "---"
assert.same("1910291645---arated.md",
ZK.create_link_text("arated", Test_date))
end)
it("should append the filetype set in g:zettel_extension", function()
vim.g.zettel_extension = ".something"
assert.same("1910291645_theworld.something",
ZK.create_link_text("theworld", Test_date))
end)
it("should append the filetype set in b:zettel_extension", function()
vim.b.zettel_extension = ".somethingelse"
assert.same("1910291645_theworld.somethingelse",
ZK.create_link_text("theworld", Test_date))
end)
end)