fix: note title ending removed when it matches parts of extension (#119)

* fix: note title ending removed when it matches parts of extension

By using [] we provide a character set rather than a direct match with ().
Lua patterns don't allow N-captures with (), so we continue executing
the pattern until there are no more substitutions being executed.

fixes #118

* fix: refactor extension stripping from title

Don't worry about n-capture, once is enough.
Turned it into a fn so it can be used elsewhere.

refs #118
This commit is contained in:
chrsm
2022-05-01 00:05:01 -07:00
committed by GitHub
parent 2573b4a6d1
commit c0925f0d30

View File

@@ -341,6 +341,12 @@ local function save_all_tk_buffers()
end end
end 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
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
-- image stuff -- image stuff
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
@@ -2009,7 +2015,7 @@ local function CreateNoteSelectTemplate(opts)
-- vim.ui.input causes ppl problems - see issue #4 -- vim.ui.input causes ppl problems - see issue #4
-- vim.ui.input({ prompt = "Title: " }, on_create_with_template) -- vim.ui.input({ prompt = "Title: " }, on_create_with_template)
local title = vim.fn.input("Title: ") local title = vim.fn.input("Title: ")
title = title:gsub("[" .. M.Cfg.extension .. "]+$", "") title = strip_extension(title, M.Cfg.extension)
if #title > 0 then if #title > 0 then
on_create_with_template(opts, title) on_create_with_template(opts, title)
end end
@@ -2086,7 +2092,7 @@ local function CreateNote(opts)
end end
local title = vim.fn.input("Title: ") local title = vim.fn.input("Title: ")
title = title:gsub("[" .. M.Cfg.extension .. "]+$", "") title = strip_extension(title, M.Cfg.extension)
if #title > 0 then if #title > 0 then
on_create(opts, title) on_create(opts, title)
end end