make uuid more robust

This commit is contained in:
lambtho12
2022-01-27 12:04:39 +01:00
parent c1008bff22
commit 767e6dc0c4
3 changed files with 39 additions and 8 deletions

View File

@@ -242,6 +242,8 @@ require('telekasten').setup({
prefix_title_by_uuid = false, prefix_title_by_uuid = false,
-- file uuid type ("rand" or input for os.date()") -- file uuid type ("rand" or input for os.date()")
uuid_type = "%Y%m%d%H%M", uuid_type = "%Y%m%d%H%M",
-- UUID separator
uuid_sep = "-",
-- following a link to a non-existing note will create it -- following a link to a non-existing note will create it
follow_creates_nonexisting = true, follow_creates_nonexisting = true,

View File

@@ -68,9 +68,12 @@ telekasten.setup({opts})
extension = ".md", extension = ".md",
-- prefix file with uuid -- 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") -- file uuid type ("rand" or input for os.date such as "%Y%m%d%H%M")
uuid_type = "%Y%m%d%H%M", uuid_type = "%Y%m%d%H%M",
-- UUID separator
uuid_sep = "-",
templates = '/path/to/directory', -- path to templates templates = '/path/to/directory', -- path to templates
extension = '.file extension', -- file extension of note files extension = '.file extension', -- file extension of note files
@@ -200,6 +203,12 @@ telekasten.setup({opts})
Default: '%Y%m%d%H%M' Default: '%Y%m%d%H%M'
*telekasten.settings.uuid_sep*
uuid_sep: ~
Separator between UUID and title in filaneme.
Default: '-'
*telekasten.settings.image_subdir* *telekasten.settings.image_subdir*
image_subdir: ~ image_subdir: ~
Path to the directory where pasted images should go to. Accepts Path to the directory where pasted images should go to. Accepts

View File

@@ -56,6 +56,8 @@ M.Cfg = {
prefix_title_by_uuid = false, prefix_title_by_uuid = false,
-- file uuid type ("rand" or input for os.date()") -- file uuid type ("rand" or input for os.date()")
uuid_type = "%Y%m%d%H%M", uuid_type = "%Y%m%d%H%M",
-- UUID separator
uuid_sep = "-",
-- following a link to a non-existing note will create it -- following a link to a non-existing note will create it
follow_creates_nonexisting = true, follow_creates_nonexisting = true,
@@ -157,7 +159,7 @@ local function random_variable(length)
return res return res
end end
local function getuuid(opts) local function get_uuid(opts)
opts.prefix_title_by_uuid = opts.prefix_title_by_uuid opts.prefix_title_by_uuid = opts.prefix_title_by_uuid
or M.Cfg.prefix_title_by_uuid or M.Cfg.prefix_title_by_uuid
opts.uuid_type = opts.uuid_type or M.Cfg.uuid_type opts.uuid_type = opts.uuid_type or M.Cfg.uuid_type
@@ -173,6 +175,15 @@ local function getuuid(opts)
return uuid return uuid
end 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) local function print_error(s)
vim.cmd("echohl ErrorMsg") vim.cmd("echohl ErrorMsg")
vim.cmd("echomsg " .. '"' .. s .. '"') vim.cmd("echomsg " .. '"' .. s .. '"')
@@ -498,6 +509,9 @@ local function linesubst(line, title, dates, uuid)
if dates == nil then if dates == nil then
dates = calculate_dates() dates = calculate_dates()
end end
if uuid == nil then
uuid = ""
end
local substs = { local substs = {
hdate = dates.hdate, 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.new_note_location = opts.new_note_location or M.Cfg.new_note_location
opts.template_handling = opts.template_handling or M.Cfg.template_handling opts.template_handling = opts.template_handling or M.Cfg.template_handling
local uuid = getuuid(opts) local uuid = get_uuid(opts)
local pinfo = Pinfo:new({ title = uuid .. "-" .. title, opts }) local pinfo = Pinfo:new({
title = concat_uuid_title(uuid, title),
opts,
})
local fname = pinfo.filepath local fname = pinfo.filepath
if pinfo.fexists == true then if pinfo.fexists == true then
-- open the new note -- open the new note
@@ -1909,8 +1926,11 @@ local function on_create(opts, title)
return return
end end
local uuid = getuuid(opts) local uuid = get_uuid(opts)
local pinfo = Pinfo:new({ title = uuid .. "-" .. title, opts }) local pinfo = Pinfo:new({
title = concat_uuid_title(uuid, title),
opts,
})
local fname = pinfo.filepath local fname = pinfo.filepath
if pinfo.fexists ~= true then if pinfo.fexists ~= true then
@@ -1929,7 +1949,7 @@ local function on_create(opts, title)
find_files_sorted({ find_files_sorted({
prompt_title = "Created note...", prompt_title = "Created note...",
cwd = pinfo.root_dir, cwd = pinfo.root_dir,
default_text = uuid .. "-" .. title, default_text = concat_uuid_title(uuid, title),
find_command = M.Cfg.find_command, find_command = M.Cfg.find_command,
attach_mappings = function(_, map) attach_mappings = function(_, map)
actions.select_default:replace(picker_actions.select_default) actions.select_default:replace(picker_actions.select_default)
@@ -2063,7 +2083,7 @@ local function FollowLink(opts)
end end
if #pinfo.filepath > 0 then if #pinfo.filepath > 0 then
local uuid = getuuid() local uuid = get_uuid(opts)
create_note_from_template( create_note_from_template(
title, title,
uuid, uuid,