From c7236ad29536465f7e98854a9cbb281e513e83b9 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 26 Oct 2020 19:32:27 +0100 Subject: [PATCH] Add Datestamp and Link creation functionality --- README.md | 4 ++ lua/zettelkasten/init.lua | 57 +++++++++++++++++++++----- lua/zettelkasten/zettelkasten_spec.lua | 49 ++++++++++++++++++++++ plugin/zettelkasten.vim | 8 ++-- 4 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 lua/zettelkasten/zettelkasten_spec.lua diff --git a/README.md b/README.md index a9d499e..032baab 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/zettelkasten/init.lua b/lua/zettelkasten/init.lua index 7991d67..1b03ae2 100644 --- a/lua/zettelkasten/init.lua +++ b/lua/zettelkasten/init.lua @@ -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, } diff --git a/lua/zettelkasten/zettelkasten_spec.lua b/lua/zettelkasten/zettelkasten_spec.lua new file mode 100644 index 0000000..312dfbc --- /dev/null +++ b/lua/zettelkasten/zettelkasten_spec.lua @@ -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) diff --git a/plugin/zettelkasten.vim b/plugin/zettelkasten.vim index ae53d16..349fa24 100644 --- a/plugin/zettelkasten.vim +++ b/plugin/zettelkasten.vim @@ -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 R :call ZKReload() augroup Zettelkasten autocmd! augroup END " nnoremap Zettel_Link :call zettelkasten#zettel_link() -nnoremap Zettel_Link :lua require 'zettelkasten'.zettel_link() +nnoremap zettel_link_create :lua require 'zettelkasten'.zettel_link_create() -nmap p Zettel_Link +nmap n zettel_link_create