diff --git a/lua/telekasten.lua b/lua/telekasten.lua index c085a10..dfc0c81 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -158,29 +158,6 @@ local function defaultConfig(home) } 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.uuid_type = opts.uuid_type or M.Cfg.uuid_type - - local uuid - if opts.uuid_type == "rand" then - uuid = random_variable(6) - elseif type(opts.uuid_type) == "function" then - uuid = opts.uuid_type() - else - uuid = os.date(opts.uuid_type) - end - return uuid -end - local function generate_note_filename(uuid, title) if M.Cfg.filename_space_subst ~= nil then title = title:gsub(" ", M.Cfg.filename_space_subst) @@ -1999,8 +1976,9 @@ local function on_create_with_template(opts, title) or M.Cfg.close_after_yanking 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_type = opts.uuid_type or M.Cfg.uuid_type - local uuid = get_uuid(opts) + local uuid = fileutils.new_uuid(uuid_type) local pinfo = Pinfo:new({ title = generate_note_filename(uuid, title), opts, @@ -2069,12 +2047,13 @@ local function on_create(opts, title) or M.Cfg.close_after_yanking 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_type = opts.uuid_type or M.Cfg.uuid_type if title == nil then return end - local uuid = get_uuid(opts) + local uuid = fileutils.new_uuid(uuid_type) local pinfo = Pinfo:new({ title = generate_note_filename(uuid, title), opts, @@ -2143,6 +2122,7 @@ local function FollowLink(opts) opts.template_handling = opts.template_handling or M.Cfg.template_handling opts.new_note_location = opts.new_note_location or M.Cfg.new_note_location + local uuid_type = opts.uuid_type or M.Cfg.uuid_type if not global_dir_check() then return @@ -2237,7 +2217,7 @@ local function FollowLink(opts) end if #pinfo.filepath > 0 then - local uuid = get_uuid(opts) + local uuid = fileutils.new_uuid(uuid_type) create_note_from_template( title, uuid, diff --git a/lua/telekasten/utils/files.lua b/lua/telekasten/utils/files.lua index 3c0f22c..2296719 100644 --- a/lua/telekasten/utils/files.lua +++ b/lua/telekasten/utils/files.lua @@ -15,6 +15,12 @@ function M.file_exists(fname) end end +-- Strips an extension from a file name, escaping "." properly, eg: +-- strip_extension("path/Filename.md", ".md") -> "path/Filename" +local function strip_extension(str, ext) + return str:gsub("(" .. ext:gsub("%.", "%%.") .. ")$", "") +end + -- Prompts the user for a note title function M.prompt_title(ext, defaultFile, callback) local canceledStr = "__INPUT_CANCELLED__" @@ -39,4 +45,25 @@ function M.prompt_title(ext, defaultFile, callback) 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 + +function M.new_uuid(uuid_style) + local uuid + if uuid_style == "rand" then + uuid = random_variable(6) + elseif type(uuid_style) == "function" then + uuid = uuid_style() + else + uuid = os.date(uuid_style) + end + return uuid +end + return M diff --git a/lua/telekasten/utils/init.lua b/lua/telekasten/utils/init.lua index 41e0e43..5186cb1 100644 --- a/lua/telekasten/utils/init.lua +++ b/lua/telekasten/utils/init.lua @@ -18,10 +18,4 @@ function M.strip(s, chars_to_remove) return s:gsub("[" .. M.escape(chars_to_remove) .. "]", "") end --- strip an extension from a file name, escaping "." properly, eg: --- strip_extension("path/Filename.md", ".md") -> "path/Filename" -local function strip_extension(str, ext) - return str:gsub("(" .. ext:gsub("%.", "%%.") .. ")$", "") -end - return M