From 767e6dc0c44b0fa4c498aee8a5403238f0fec37a Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 27 Jan 2022 12:04:39 +0100 Subject: [PATCH] make uuid more robust --- README.md | 2 ++ doc/telekasten.txt | 11 ++++++++++- lua/telekasten.lua | 34 +++++++++++++++++++++++++++------- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0515372..7a40053 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,8 @@ require('telekasten').setup({ prefix_title_by_uuid = false, -- file uuid type ("rand" or input for os.date()") uuid_type = "%Y%m%d%H%M", + -- UUID separator + uuid_sep = "-", -- following a link to a non-existing note will create it follow_creates_nonexisting = true, diff --git a/doc/telekasten.txt b/doc/telekasten.txt index f77b620..d5bac30 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -68,9 +68,12 @@ telekasten.setup({opts}) extension = ".md", -- prefix file with uuid - prefix_title_by_uuid = true, + prefix_title_by_uuid = false, -- file uuid type ("rand" or input for os.date such as "%Y%m%d%H%M") uuid_type = "%Y%m%d%H%M", + -- UUID separator + uuid_sep = "-", + templates = '/path/to/directory', -- path to templates extension = '.file extension', -- file extension of note files @@ -200,6 +203,12 @@ telekasten.setup({opts}) Default: '%Y%m%d%H%M' + *telekasten.settings.uuid_sep* + uuid_sep: ~ + Separator between UUID and title in filaneme. + + Default: '-' + *telekasten.settings.image_subdir* image_subdir: ~ Path to the directory where pasted images should go to. Accepts diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 6181693..1bfe487 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -56,6 +56,8 @@ M.Cfg = { prefix_title_by_uuid = false, -- file uuid type ("rand" or input for os.date()") uuid_type = "%Y%m%d%H%M", + -- UUID separator + uuid_sep = "-", -- following a link to a non-existing note will create it follow_creates_nonexisting = true, @@ -157,7 +159,7 @@ local function random_variable(length) return res end -local function getuuid(opts) +local function get_uuid(opts) opts.prefix_title_by_uuid = opts.prefix_title_by_uuid or M.Cfg.prefix_title_by_uuid opts.uuid_type = opts.uuid_type or M.Cfg.uuid_type @@ -173,6 +175,15 @@ local function getuuid(opts) return uuid end +local function concat_uuid_title(uuid, title) + local sep = M.Cfg.uuid_sep or "-" + if uuid == nil then + return title + else + return uuid .. sep .. title + end +end + local function print_error(s) vim.cmd("echohl ErrorMsg") vim.cmd("echomsg " .. '"' .. s .. '"') @@ -498,6 +509,9 @@ local function linesubst(line, title, dates, uuid) if dates == nil then dates = calculate_dates() end + if uuid == nil then + uuid = "" + end local substs = { hdate = dates.hdate, @@ -1834,8 +1848,11 @@ local function on_create_with_template(opts, title) opts.new_note_location = opts.new_note_location or M.Cfg.new_note_location opts.template_handling = opts.template_handling or M.Cfg.template_handling - local uuid = getuuid(opts) - local pinfo = Pinfo:new({ title = uuid .. "-" .. title, opts }) + local uuid = get_uuid(opts) + local pinfo = Pinfo:new({ + title = concat_uuid_title(uuid, title), + opts, + }) local fname = pinfo.filepath if pinfo.fexists == true then -- open the new note @@ -1909,8 +1926,11 @@ local function on_create(opts, title) return end - local uuid = getuuid(opts) - local pinfo = Pinfo:new({ title = uuid .. "-" .. title, opts }) + local uuid = get_uuid(opts) + local pinfo = Pinfo:new({ + title = concat_uuid_title(uuid, title), + opts, + }) local fname = pinfo.filepath if pinfo.fexists ~= true then @@ -1929,7 +1949,7 @@ local function on_create(opts, title) find_files_sorted({ prompt_title = "Created note...", cwd = pinfo.root_dir, - default_text = uuid .. "-" .. title, + default_text = concat_uuid_title(uuid, title), find_command = M.Cfg.find_command, attach_mappings = function(_, map) actions.select_default:replace(picker_actions.select_default) @@ -2063,7 +2083,7 @@ local function FollowLink(opts) end if #pinfo.filepath > 0 then - local uuid = getuuid() + local uuid = get_uuid(opts) create_note_from_template( title, uuid,