diff --git a/README.md b/README.md index 48892af..a5490f0 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,13 @@ 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", + -- UUID separator + uuid_sep = "-", + -- following a link to a non-existing note will create it follow_creates_nonexisting = true, dailies_create_nonexisting = true, @@ -721,6 +728,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 8cb9c60..4478bc5 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -71,6 +71,14 @@ telekasten.setup({opts}) -- markdown file extension extension = ".md", + -- prefix file with uuid + 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 +208,25 @@ 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.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 @@ -759,6 +786,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 9b4d4f2..b199b00 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -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