Skip to content

Commit 4da7846

Browse files
authored
Prompt for undefined template fields (#666)
1 parent c87db61 commit 4da7846

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Added `opts.follow_img_func` option for customizing how to handle image paths.
13+
- Added better handling for undefined template fields, which will now be prompted for.
1314

1415
### Changed
1516

lua/obsidian/templates.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ M.substitute_template_variables = function(text, client, note)
7575
methods["path"] = tostring(note.path)
7676
end
7777

78+
-- Replace known variables.
7879
for key, subst in pairs(methods) do
7980
for m_start, m_end in util.gfind(text, "{{" .. key .. "}}", nil, true) do
8081
---@type string
@@ -90,6 +91,15 @@ M.substitute_template_variables = function(text, client, note)
9091
end
9192
end
9293

94+
-- Find unknown variables and prompt for them.
95+
for m_start, m_end in util.gfind(text, "{{[^}]+}}") do
96+
local key = util.strip_whitespace(string.sub(text, m_start + 2, m_end - 2))
97+
local value = util.input(string.format("Enter value for '%s' (<cr> to skip): ", key))
98+
if value and string.len(value) > 0 then
99+
text = string.sub(text, 1, m_start - 1) .. value .. string.sub(text, m_end + 1)
100+
end
101+
end
102+
93103
return text
94104
end
95105

0 commit comments

Comments
 (0)