Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt for undefined template fields #666

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- 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