From 65e3b0bc1cf62ace66b5ff2cbadad24cc3548c4c Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 6 Nov 2020 15:33:39 +0100 Subject: [PATCH] Add anchor regex ceontent as changeable option --- lua/zettelkasten/list.lua | 6 +++-- lua/zettelkasten/options.lua | 6 ++++- lua/zettelkasten/options_spec.lua | 39 ++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lua/zettelkasten/list.lua b/lua/zettelkasten/list.lua index 6ee46f8..e19cea8 100644 --- a/lua/zettelkasten/list.lua +++ b/lua/zettelkasten/list.lua @@ -15,7 +15,7 @@ function ls.get_anchors_and_paths(path, recursive, options) -- TODO check for duplicates and warn user local zettel = {} -- TODO let user set as option, at least remove magic var - local anchorreg = '^.*/?([%d][%d][%d][%d][%d][%d][%d][%d][%d][%d])[^/]*%' .. + local anchorreg = '^.*/?(' .. o.anchor().regex .. ')[^/]*%' .. o.zettel().extension .. '$' local handle = vim.loop.fs_scandir(path) @@ -39,7 +39,9 @@ end -- it will use the (recursive) results of the zettel_root directory. function ls.get_zettel(anchor, all) -- TODO why is there 'somepath' here? - if not all then all = ls.get_anchors_and_paths('/home/marty/documents/notes') end + if not all then + all = ls.get_anchors_and_paths('/home/marty/documents/notes') + end return all[anchor] end diff --git a/lua/zettelkasten/options.lua b/lua/zettelkasten/options.lua index c643e42..87f06e1 100644 --- a/lua/zettelkasten/options.lua +++ b/lua/zettelkasten/options.lua @@ -15,7 +15,11 @@ local zettel_defaults = { } } local anchor_defaults = { - separator = {vimname = "zettel_anchor_separator", default = "_"} + separator = {vimname = "zettel_anchor_separator", default = "_"}, + regex = { + vimname = "zettel_anchor_regex", + default = '[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]' + } } -- remaining options diff --git a/lua/zettelkasten/options_spec.lua b/lua/zettelkasten/options_spec.lua index 2a97239..c299996 100644 --- a/lua/zettelkasten/options_spec.lua +++ b/lua/zettelkasten/options_spec.lua @@ -15,19 +15,6 @@ describe("zettel options", function() end) it("should return the default zettel extension if not set in vim", function() assert.same(".md", opt.zettel().extension) end) -end) - -describe("zettel options", function() - it("should return the global anchor separator if set in vim", function() - _G.vim.g.zettel_anchor_separator = "SEPARATE" - assert.same("SEPARATE", opt.anchor().separator) - end) - it("should return the buffer anchor separator if set in vim", function() - _G.vim.b.zettel_anchor_separator = "--" - assert.same("--", opt.anchor().separator) - end) - it("should return the default anchor separator if not set in vim", - function() assert.same("_", opt.anchor().separator) end) it("should return the global link style if set in vim", function() _G.vim.g.zettel_link_style = "wiki" @@ -59,3 +46,29 @@ describe("zettel options", function() assert.is_error(function() opt.zettel() end) end) end) + +describe("anchor options", function() + it("should return the global anchor separator if set in vim", function() + _G.vim.g.zettel_anchor_separator = "SEPARATE" + assert.same("SEPARATE", opt.anchor().separator) + end) + it("should return the buffer anchor separator if set in vim", function() + _G.vim.b.zettel_anchor_separator = "--" + assert.same("--", opt.anchor().separator) + end) + it("should return the default anchor separator if not set in vim", + function() assert.same("_", opt.anchor().separator) end) + + it("should return the anchor regex if set in vim", function() + vim.g.zettel_anchor_regex = + "[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]?[%d]?" + -- allowing both 10- and 12-digit time-based zettel ids + assert.same('[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]?[%d]?', + opt.anchor().regex) + end) + it("should return the default anchor regex if not set in vim", function() + assert.same('[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]', + opt.anchor().regex) + end) + +end)