zettelkasten.nvim/lua/zettelkasten/init.lua

71 lines
2.2 KiB
Lua
Raw Normal View History

2020-10-26 14:55:40 +00:00
local ZK = {}
2020-10-29 18:00:23 +00:00
local ls = require 'zettelkasten.list'
function ZK.init(vimapi)
2020-10-29 18:00:23 +00:00
vim = vimapi or vim
ZK.options = {
anchor_separator = vim.g["zettel_anchor_separator"] or
vim.b["zettel_anchor_separator"] or "_",
zettel_extension = vim.g["zettel_extension"] or
vim.b["zettel_extension"] or ".md"
-- zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes",
-- TODO zettel_anchor_pattern = regex? -> needs custom creation function in `create_anchor`
}
2020-10-26 14:55:40 +00:00
end
-- 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()
2020-10-29 18:00:23 +00:00
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)
2020-10-29 18:00:23 +00:00
local timestamp
if pcall(function() timestamp = os.time(date) end) then
return os.date('%y%m%d%H%M', timestamp)
else
return nil
end
2020-10-26 14:55:40 +00:00
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)
2020-10-29 18:00:23 +00:00
text = text or ""
local o = ZK.options
if text == "" then
text = "" .. ZK.create_anchor(date)
else
text = text:lower():gsub(" ", "-")
text = "" .. ZK.create_anchor(date) .. (o.anchor_separator or "_") ..
text
end
return text .. (o.zettel_extension or ".md")
2020-10-26 14:55:40 +00:00
end
-- Returns all zettel in path as a
-- { "anchor" = "path/to/zettel/anchor filename.md" }
-- table.
-- Recurses into subdirectories if recursive argument is true.
function ZK.get_zettel_list(path, recursive)
2020-10-29 18:00:23 +00:00
return ls.get_anchors_and_paths(path, recursive or false)
end
2020-10-26 14:55:40 +00:00
return {
2020-10-29 18:00:23 +00:00
init = ZK.init,
zettel_link_create = ZK.zettel_link_create,
create_anchor = ZK.create_anchor,
create_link = ZK.create_link,
get_zettel_list = ZK.get_zettel_list
2020-10-26 14:55:40 +00:00
}