Skip to content

Commit

Permalink
Prompt for undefined template fields (epwalsh#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh authored and aquilesg committed Jul 31, 2024
1 parent c15d42c commit 533672d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions lua/obsidian/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' (<cr> 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

Expand Down

0 comments on commit 533672d

Please sign in to comment.