From c0925f0d301901d6c82c0ff6c8cc82a091463c8b Mon Sep 17 00:00:00 2001 From: chrsm <1082451+chrsm@users.noreply.github.com> Date: Sun, 1 May 2022 00:05:01 -0700 Subject: [PATCH] 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 --- lua/telekasten.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 6446e5d..ab42de2 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -341,6 +341,12 @@ local function save_all_tk_buffers() 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 -- ---------------------------------------------------------------------------- @@ -2009,7 +2015,7 @@ local function CreateNoteSelectTemplate(opts) -- vim.ui.input causes ppl problems - see issue #4 -- vim.ui.input({ prompt = "Title: " }, on_create_with_template) local title = vim.fn.input("Title: ") - title = title:gsub("[" .. M.Cfg.extension .. "]+$", "") + title = strip_extension(title, M.Cfg.extension) if #title > 0 then on_create_with_template(opts, title) end @@ -2086,7 +2092,7 @@ local function CreateNote(opts) end local title = vim.fn.input("Title: ") - title = title:gsub("[" .. M.Cfg.extension .. "]+$", "") + title = strip_extension(title, M.Cfg.extension) if #title > 0 then on_create(opts, title) end