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

@@ -242,6 +242,13 @@ require('telekasten').setup({
-- markdown file extension -- markdown file extension
extension = ".md", 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 -- following a link to a non-existing note will create it
follow_creates_nonexisting = true, follow_creates_nonexisting = true,
dailies_create_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 | | specifier in template | expands to | example |
| --- | --- | --- | | --- | --- | --- |
| `{{title}}` | the title of the note | My new note | | `{{title}}` | the title of the note | My new note |
| `{{uuid}}` | UUID for the note | 202201271129 |
| `{{date}}` | date in iso format | 2021-11-21 | | `{{date}}` | date in iso format | 2021-11-21 |
| `{{prevday}}` | previous day's date in iso format | 2021-11-20 | | `{{prevday}}` | previous day's date in iso format | 2021-11-20 |
| `{{nextday}}` | next day's date in iso format | 2021-11-22 | | `{{nextday}}` | next day's date in iso format | 2021-11-22 |

View File

@@ -71,6 +71,14 @@ telekasten.setup({opts})
-- markdown file extension -- markdown file extension
extension = ".md", 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 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 +208,25 @@ telekasten.setup({opts})
Default: '.md' 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* *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
@@ -759,6 +786,7 @@ The following substitutions will be made during new note creation:
| in template | expands to | example | | in template | expands to | example |
+-----------------+-----------------------+-----------------------------+ +-----------------+-----------------------+-----------------------------+
| `{{title}}` | the title of the note | My new note | | `{{title}}` | the title of the note | My new note |
| `{{uuid}}` | UUID of the note | 202201271129 |
| `{{date}}` | date in iso format | 2021-11-21 | | `{{date}}` | date in iso format | 2021-11-21 |
| `{{prevday}}` | previous day, iso | 2021-11-20 | | `{{prevday}}` | previous day, iso | 2021-11-20 |
| `{{nextday}}` | next day, iso | 2021-11-22 | | `{{nextday}}` | next day, iso | 2021-11-22 |

View File

@@ -77,6 +77,13 @@ M.Cfg = {
-- markdown file extension -- markdown file extension
extension = ".md", 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 -- following a link to a non-existing note will create it
follow_creates_nonexisting = true, follow_creates_nonexisting = true,
dailies_create_nonexisting = true, dailies_create_nonexisting = true,
@@ -168,6 +175,40 @@ local function file_exists(fname)
end end
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) local function print_error(s)
vim.cmd("echohl ErrorMsg") vim.cmd("echohl ErrorMsg")
vim.cmd("echomsg " .. '"' .. s .. '"') vim.cmd("echomsg " .. '"' .. s .. '"')
@@ -558,10 +599,13 @@ local function calculate_dates(date)
return dates return dates
end end
local function linesubst(line, title, dates) 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,
@@ -587,6 +631,7 @@ local function linesubst(line, title, dates)
saturday = dates.saturday, saturday = dates.saturday,
title = title, title = title,
uuid = uuid,
} }
for k, v in pairs(substs) do for k, v in pairs(substs) do
line = line:gsub("{{" .. k .. "}}", v) line = line:gsub("{{" .. k .. "}}", v)
@@ -597,6 +642,7 @@ end
local function create_note_from_template( local function create_note_from_template(
title, title,
uuid,
filepath, filepath,
templatefn, templatefn,
calendar_info calendar_info
@@ -612,7 +658,7 @@ local function create_note_from_template(
-- now write the output file, substituting vars line by line -- now write the output file, substituting vars line by line
local ofile = io.open(filepath, "a") local ofile = io.open(filepath, "a")
for _, line in pairs(lines) do for _, line in pairs(lines) do
ofile:write(linesubst(line, title, calendar_info) .. "\n") ofile:write(linesubst(line, title, calendar_info, uuid) .. "\n")
end end
ofile:flush() ofile:flush()
@@ -1257,7 +1303,7 @@ local function FindDailyNotes(opts)
or M.Cfg.dailies_create_nonexisting == true or M.Cfg.dailies_create_nonexisting == true
) )
then 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 = true
opts.erase_file = fname opts.erase_file = fname
end end
@@ -1306,7 +1352,7 @@ local function FindWeeklyNotes(opts)
or M.Cfg.weeklies_create_nonexisting == true or M.Cfg.weeklies_create_nonexisting == true
) )
then 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 = true
opts.erase_file = fname opts.erase_file = fname
end end
@@ -1662,6 +1708,7 @@ local function GotoDate(opts)
then then
create_note_from_template( create_note_from_template(
word, word,
_,
fname, fname,
M.note_type_templates.daily, M.note_type_templates.daily,
opts.dates 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.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 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 local fname = pinfo.filepath
if pinfo.fexists == true then if pinfo.fexists == true then
-- open the new note -- 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 -- TODO: pass in the calendar_info returned from the pinfo
create_note_from_template( create_note_from_template(
title, title,
uuid,
fname, fname,
template, template,
pinfo.calendar_info pinfo.calendar_info
@@ -1968,13 +2020,18 @@ local function on_create(opts, title)
return return
end 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 local fname = pinfo.filepath
if pinfo.fexists ~= true then if pinfo.fexists ~= true then
-- TODO: pass in the calendar_info returned in pinfo -- TODO: pass in the calendar_info returned in pinfo
create_note_from_template( create_note_from_template(
title, title,
uuid,
fname, fname,
pinfo.template, pinfo.template,
pinfo.calendar_info pinfo.calendar_info
@@ -1986,7 +2043,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 = 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)
@@ -2120,8 +2177,10 @@ local function FollowLink(opts)
end end
if #pinfo.filepath > 0 then if #pinfo.filepath > 0 then
local uuid = get_uuid(opts)
create_note_from_template( create_note_from_template(
title, title,
uuid,
pinfo.filepath, pinfo.filepath,
pinfo.template, pinfo.template,
pinfo.calendar_info pinfo.calendar_info
@@ -2487,7 +2546,7 @@ local function GotoThisWeek(opts)
or M.Cfg.weeklies_create_nonexisting == true or M.Cfg.weeklies_create_nonexisting == true
) )
then 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 = true
opts.erase_file = fname opts.erase_file = fname
end end