diff --git a/CHANGELOG.md b/CHANGELOG.md index ab937a779..ab53edec6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added: Support for UI highlights on calloutblocks - Added: Support for custom callout block defintions - Added `opts.follow_img_func` option for customizing how to handle image paths. +- Added better handling for undefined template fields, which will now be prompted for. ### Changed diff --git a/lua/obsidian/templates.lua b/lua/obsidian/templates.lua index 58f44461c..ac8a83f27 100644 --- a/lua/obsidian/templates.lua +++ b/lua/obsidian/templates.lua @@ -75,6 +75,7 @@ M.substitute_template_variables = function(text, client, note) methods["path"] = tostring(note.path) end + -- Replace known variables. for key, subst in pairs(methods) do for m_start, m_end in util.gfind(text, "{{" .. key .. "}}", nil, true) do ---@type string @@ -90,6 +91,15 @@ M.substitute_template_variables = function(text, client, note) end end + -- Find unknown variables and prompt for them. + for m_start, m_end in util.gfind(text, "{{[^}]+}}") do + local key = util.strip_whitespace(string.sub(text, m_start + 2, m_end - 2)) + local value = util.input(string.format("Enter value for '%s' ( to skip): ", key)) + if value and string.len(value) > 0 then + text = string.sub(text, 1, m_start - 1) .. value .. string.sub(text, m_end + 1) + end + end + return text end