From 9705f5795b40171b875f770abdbe0ee059f489b4 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 27 Jan 2022 10:54:30 +0100 Subject: [PATCH 1/4] Add UUID prefix to new notes --- README.md | 5 +++++ doc/telekasten.txt | 18 ++++++++++++++++++ lua/telekasten.lua | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/README.md b/README.md index c628c1e..3a13ec9 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,11 @@ require('telekasten').setup({ -- 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", + -- following a link to a non-existing note will create it follow_creates_nonexisting = true, dailies_create_nonexisting = true, diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 3f34ff4..93bb6f4 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -67,6 +67,11 @@ telekasten.setup({opts}) -- markdown file extension extension = ".md", + -- prefix file with uuid + prefix_title_by_uuid = true, + -- file uuid type ("rand" or input for os.date such as "%Y%m%d%H%M") + uuid_type = "%Y%m%d%H%M", + templates = '/path/to/directory', -- path to templates extension = '.file extension', -- file extension of note files @@ -182,6 +187,19 @@ telekasten.setup({opts}) Default: '.md' + *telekasten.settings.prefix_title_by_uuid* + prefix_title_by_uuid: ~ + Adds an Universal Unique Identifier before the file name. + + Default: 'false' + + *telekasten.settings.uuid_type* + uuid_type: ~ + Type of UUID. Could be 'rand' for a random 6 character string, or a + time format to input in os.date() such as %Y%m%d%H%M. + + Default: '%Y%m%d%H%M' + *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 fbfc50b..315f228 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -52,6 +52,11 @@ 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", + -- following a link to a non-existing note will create it follow_creates_nonexisting = true, dailies_create_nonexisting = true, @@ -143,6 +148,32 @@ 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 append_uuid(opts, title) + 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 + + if opts.prefix_title_by_uuid then + local uuid = "" + if opts.uuid_type ~= "rand" then + uuid = os.date(opts.uuid_type) + else + uuid = random_variable(6) + end + title = uuid .. "-" .. title + end + return title +end + local function print_error(s) vim.cmd("echohl ErrorMsg") vim.cmd("echomsg " .. '"' .. s .. '"') @@ -1793,6 +1824,8 @@ local function on_create_with_template(opts, title) return end + title = append_uuid(opts, title) + opts = opts or {} opts.insert_after_inserting = opts.insert_after_inserting or M.Cfg.insert_after_inserting @@ -1874,6 +1907,8 @@ local function on_create(opts, title) return end + title = append_uuid(opts, title) + local pinfo = Pinfo:new({ title = title, opts }) local fname = pinfo.filepath From 2e4d28234427925911c73a1c7947cb4217c15ae7 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 27 Jan 2022 11:01:44 +0100 Subject: [PATCH 2/4] make luacheck happy --- lua/telekasten.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 315f228..2a14a60 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -163,7 +163,7 @@ local function append_uuid(opts, title) opts.uuid_type = opts.uuid_type or M.Cfg.uuid_type if opts.prefix_title_by_uuid then - local uuid = "" + local uuid if opts.uuid_type ~= "rand" then uuid = os.date(opts.uuid_type) else From c1008bff220f00aa57b126bd539d71200a1a1fc2 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 27 Jan 2022 11:31:17 +0100 Subject: [PATCH 3/4] make uuid more useful --- README.md | 1 + doc/telekasten.txt | 1 + lua/telekasten.lua | 36 ++++++++++++++++++++---------------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3a13ec9..0515372 100644 --- a/README.md +++ b/README.md @@ -721,6 +721,7 @@ Currently, the following substitutions will be made during new note creation: | specifier in template | expands to | example | | --- | --- | --- | | `{{title}}` | the title of the note | My new note | +| `{{uuid}}` | UUID for the note | 202201271129 | | `{{date}}` | date in iso format | 2021-11-21 | | `{{prevday}}` | previous day's date in iso format | 2021-11-20 | | `{{nextday}}` | next day's date in iso format | 2021-11-22 | diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 93bb6f4..f77b620 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -759,6 +759,7 @@ The following substitutions will be made during new note creation: | in template | expands to | example | +-----------------+-----------------------+-----------------------------+ | `{{title}}` | the title of the note | My new note | +| `{{uuid}}` | UUID of the note | 202201271129 | | `{{date}}` | date in iso format | 2021-11-21 | | `{{prevday}}` | previous day, iso | 2021-11-20 | | `{{nextday}}` | next day, iso | 2021-11-22 | diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 2a14a60..6181693 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -157,21 +157,20 @@ local function random_variable(length) return res end -local function append_uuid(opts, title) +local function getuuid(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 - local uuid if opts.uuid_type ~= "rand" then uuid = os.date(opts.uuid_type) else uuid = random_variable(6) end - title = uuid .. "-" .. title end - return title + return uuid end local function print_error(s) @@ -495,7 +494,7 @@ 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 @@ -524,6 +523,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) @@ -534,6 +534,7 @@ end local function create_note_from_template( title, + uuid, filepath, templatefn, calendar_info @@ -549,7 +550,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() @@ -1194,7 +1195,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 @@ -1243,7 +1244,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 @@ -1599,6 +1600,7 @@ local function GotoDate(opts) then create_note_from_template( word, + _, fname, M.note_type_templates.daily, opts.dates @@ -1824,8 +1826,6 @@ local function on_create_with_template(opts, title) return end - title = append_uuid(opts, title) - opts = opts or {} opts.insert_after_inserting = opts.insert_after_inserting or M.Cfg.insert_after_inserting @@ -1834,7 +1834,8 @@ 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 = getuuid(opts) + local pinfo = Pinfo:new({ title = uuid .. "-" .. title, opts }) local fname = pinfo.filepath if pinfo.fexists == true then -- open the new note @@ -1855,6 +1856,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 @@ -1907,15 +1909,15 @@ local function on_create(opts, title) return end - title = append_uuid(opts, title) - - local pinfo = Pinfo:new({ title = title, opts }) + local uuid = getuuid(opts) + local pinfo = Pinfo:new({ 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 @@ -1927,7 +1929,7 @@ local function on_create(opts, title) find_files_sorted({ prompt_title = "Created note...", cwd = pinfo.root_dir, - default_text = title, + default_text = uuid .. "-" .. title, find_command = M.Cfg.find_command, attach_mappings = function(_, map) actions.select_default:replace(picker_actions.select_default) @@ -2061,8 +2063,10 @@ local function FollowLink(opts) end if #pinfo.filepath > 0 then + local uuid = getuuid() create_note_from_template( title, + uuid, pinfo.filepath, pinfo.template, pinfo.calendar_info @@ -2428,7 +2432,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 From 767e6dc0c44b0fa4c498aee8a5403238f0fec37a Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 27 Jan 2022 12:04:39 +0100 Subject: [PATCH 4/4] 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,