Merge pull request #84 from lambtho12/uuid_prefix

Add UUID prefix to new notes
This commit is contained in:
Rene Schallner
2022-04-06 20:13:55 +02:00
committed by GitHub
3 changed files with 103 additions and 8 deletions

View File

@@ -77,6 +77,13 @@ M.Cfg = {
-- markdown file extension
extension = ".md",
-- prefix file with uuid
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,
dailies_create_nonexisting = true,
@@ -168,6 +175,40 @@ local function file_exists(fname)
end
end
local function random_variable(length)
math.randomseed(os.clock() ^ 5)
local res = ""
for _ = 1, length do
res = res .. string.char(math.random(97, 122))
end
return res
end
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
local uuid
if opts.prefix_title_by_uuid then
if opts.uuid_type ~= "rand" then
uuid = os.date(opts.uuid_type)
else
uuid = random_variable(6)
end
end
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 .. '"')
@@ -558,10 +599,13 @@ local function calculate_dates(date)
return dates
end
local function linesubst(line, title, dates)
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,
@@ -587,6 +631,7 @@ local function linesubst(line, title, dates)
saturday = dates.saturday,
title = title,
uuid = uuid,
}
for k, v in pairs(substs) do
line = line:gsub("{{" .. k .. "}}", v)
@@ -597,6 +642,7 @@ end
local function create_note_from_template(
title,
uuid,
filepath,
templatefn,
calendar_info
@@ -612,7 +658,7 @@ local function create_note_from_template(
-- now write the output file, substituting vars line by line
local ofile = io.open(filepath, "a")
for _, line in pairs(lines) do
ofile:write(linesubst(line, title, calendar_info) .. "\n")
ofile:write(linesubst(line, title, calendar_info, uuid) .. "\n")
end
ofile:flush()
@@ -1257,7 +1303,7 @@ local function FindDailyNotes(opts)
or M.Cfg.dailies_create_nonexisting == true
)
then
create_note_from_template(today, fname, M.note_type_templates.daily)
create_note_from_template(today, _, fname, M.note_type_templates.daily)
opts.erase = true
opts.erase_file = fname
end
@@ -1306,7 +1352,7 @@ local function FindWeeklyNotes(opts)
or M.Cfg.weeklies_create_nonexisting == true
)
then
create_note_from_template(title, fname, M.note_type_templates.weekly)
create_note_from_template(title, _, fname, M.note_type_templates.weekly)
opts.erase = true
opts.erase_file = fname
end
@@ -1662,6 +1708,7 @@ local function GotoDate(opts)
then
create_note_from_template(
word,
_,
fname,
M.note_type_templates.daily,
opts.dates
@@ -1895,7 +1942,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 pinfo = Pinfo:new({ title = 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
@@ -1916,6 +1967,7 @@ local function on_create_with_template(opts, title)
-- TODO: pass in the calendar_info returned from the pinfo
create_note_from_template(
title,
uuid,
fname,
template,
pinfo.calendar_info
@@ -1968,13 +2020,18 @@ local function on_create(opts, title)
return
end
local pinfo = Pinfo:new({ title = 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
-- TODO: pass in the calendar_info returned in pinfo
create_note_from_template(
title,
uuid,
fname,
pinfo.template,
pinfo.calendar_info
@@ -1986,7 +2043,7 @@ local function on_create(opts, title)
find_files_sorted({
prompt_title = "Created note...",
cwd = pinfo.root_dir,
default_text = 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)
@@ -2120,8 +2177,10 @@ local function FollowLink(opts)
end
if #pinfo.filepath > 0 then
local uuid = get_uuid(opts)
create_note_from_template(
title,
uuid,
pinfo.filepath,
pinfo.template,
pinfo.calendar_info
@@ -2487,7 +2546,7 @@ local function GotoThisWeek(opts)
or M.Cfg.weeklies_create_nonexisting == true
)
then
create_note_from_template(title, fname, M.note_type_templates.weekly)
create_note_from_template(title, _, fname, M.note_type_templates.weekly)
opts.erase = true
opts.erase_file = fname
end