Add Datestamp and Link creation functionality
This commit is contained in:
parent
86ab71b1d6
commit
c7236ad295
4 changed files with 104 additions and 14 deletions
|
@ -1,5 +1,9 @@
|
|||
# Zettelkasten.nvim
|
||||
|
||||
To develop / debug:
|
||||
|
||||
start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the files in project dir as if they were on path
|
||||
|
||||
## TODO
|
||||
|
||||
* [ ] go to zettel
|
||||
|
|
|
@ -1,26 +1,63 @@
|
|||
local ZK = {}
|
||||
|
||||
local zettel_extension, anchor_separator
|
||||
|
||||
local api
|
||||
if vim ~= nil then
|
||||
api = vim.api
|
||||
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"
|
||||
end
|
||||
package.loaded['zettelkasten'] = nil
|
||||
|
||||
function ZK.init()
|
||||
print(
|
||||
vim.fn.nvim_win_get_width(0),
|
||||
vim.fn.nvim_win_get_height(0)
|
||||
)
|
||||
local anchor_separator = "_"
|
||||
|
||||
function ZK.init(vimapi)
|
||||
vim = vimapi or vim
|
||||
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"
|
||||
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.zettel_link()
|
||||
ZK._test()
|
||||
function ZK.follow_link()
|
||||
assert(false, "NOT IMPLEMENTED")
|
||||
return ''
|
||||
end
|
||||
|
||||
function ZK._test()
|
||||
print('follow_create_zettel')
|
||||
-- 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) .. anchor_separator .. text
|
||||
end
|
||||
return text .. (zettel_extension or ".md")
|
||||
end
|
||||
|
||||
return {
|
||||
init = ZK.init,
|
||||
zettel_link = ZK.zettel_link
|
||||
zettel_link_create = ZK.zettel_link_create,
|
||||
create_anchor = ZK.create_anchor,
|
||||
create_link = ZK.create_link,
|
||||
}
|
||||
|
|
49
lua/zettelkasten/zettelkasten_spec.lua
Normal file
49
lua/zettelkasten/zettelkasten_spec.lua
Normal file
|
@ -0,0 +1,49 @@
|
|||
ZK = require'init'
|
||||
Test_date={ year=2019, month=10, day=29, hour=16, min=45 }
|
||||
|
||||
describe("Zettelkasten", function()
|
||||
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 place the contents of g:zettel_anchor_separator variable in link", function()
|
||||
vim = { g = { zettel_anchor_separator = "SEP" }, b = {}}
|
||||
ZK.init(vim)
|
||||
assert.same("1910291645SEParated.md", ZK.create_link("arated", Test_date))
|
||||
end)
|
||||
|
||||
it("should append the filetype set in g:zettel_extension", function()
|
||||
vim = { g = { zettel_extension = ".something" }, b = {}}
|
||||
ZK.init(vim)
|
||||
assert.same("1910291645_theworld.something", ZK.create_link("theworld", Test_date))
|
||||
end)
|
||||
|
||||
|
||||
end)
|
||||
end)
|
|
@ -1,15 +1,15 @@
|
|||
" for DEBUGGING ONLY: reloads the whole lua plugin
|
||||
fun! ZKReload()
|
||||
" for DEBUGGING
|
||||
lua for k in pairs(package.loaded) do if k:match("^zettelkasten") then package.loaded[k] = nil end end
|
||||
echom 'zettelkasten reloaded'
|
||||
lua require 'zettelkasten'.init()
|
||||
endfun
|
||||
nnoremap <leader>R :call ZKReload()<cr>
|
||||
|
||||
augroup Zettelkasten
|
||||
autocmd!
|
||||
augroup END
|
||||
|
||||
" nnoremap <Plug>Zettel_Link :call zettelkasten#zettel_link()<cr>
|
||||
nnoremap <Plug>Zettel_Link :lua require 'zettelkasten'.zettel_link()<cr>
|
||||
nnoremap <Plug>zettel_link_create :lua require 'zettelkasten'.zettel_link_create()<cr>
|
||||
|
||||
nmap <leader>p <Plug>Zettel_Link
|
||||
nmap <leader>n <Plug>zettel_link_create
|
||||
|
|
Loading…
Reference in a new issue