Skip to content

Commit

Permalink
feat(templates): provide note to custom variable substitution functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cristobalgvera committed Sep 11, 2024
1 parent 14e0427 commit 122c58a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 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 `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.
- Added the related `Note` to the custom variable substitution functions.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion lua/obsidian/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ M.substitute_template_variables = function(text, client, note)
if type(subst) == "string" then
value = subst
else
value = subst()
value = subst(note)
-- cache the result
methods[key] = value
end
Expand Down
59 changes: 45 additions & 14 deletions test/obsidian/templates_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,58 @@ local tmp_client = function()
end

describe("templates.substitute_template_variables()", function()
---@type obsidian.Client
local client

before_each(function()
client = tmp_client()
end)

it("should substitute built-in variables", function()
local client = tmp_client()
local text = "today is {{date}} and the title of the note is {{title}}"
assert.equal(
string.format("today is %s and the title of the note is %s", os.date "%Y-%m-%d", "FOO"),
templates.substitute_template_variables(text, client, Note.new("FOO", { "FOO" }, {}))
)
end)

it("should substitute custom variables", function()
local client = tmp_client()
client.opts.templates.substitutions = {
weekday = function()
return "Monday"
end,
}
local text = "today is {{weekday}}"
assert.equal("today is Monday", templates.substitute_template_variables(text, client, Note.new("foo", {}, {})))

-- Make sure the client opts has not been modified.
assert.equal(1, vim.tbl_count(client.opts.templates.substitutions))
assert.equal("function", type(client.opts.templates.substitutions.weekday))
describe("when substituting custom variable", function()
it("should substitute using a string", function()
client.opts.templates.substitutions = {
weekday = "Monday",
}
local text = "today is {{weekday}}"
assert.equal("today is Monday", templates.substitute_template_variables(text, client, Note.new("foo", {}, {})))

-- Make sure the client opts has not been modified.
assert.equal(1, vim.tbl_count(client.opts.templates.substitutions))
assert.equal("string", type(client.opts.templates.substitutions.weekday))
end)

it("should substitute using a function", function()
client.opts.templates.substitutions = {
weekday = function()
return "Monday"
end,
}
local text = "today is {{weekday}}"
assert.equal("today is Monday", templates.substitute_template_variables(text, client, Note.new("foo", {}, {})))

-- Make sure the client opts has not been modified.
assert.equal(1, vim.tbl_count(client.opts.templates.substitutions))
assert.equal("function", type(client.opts.templates.substitutions.weekday))
end)

it("should substitute using values from the note", function()
client.opts.templates.substitutions = {
---@param note obsidian.Note
id_uppercase = function(note)
return string.upper(note.id)
end,
}

local text = "formatted id: {{id_uppercase}}"
assert.equal("formatted id: FOO", templates.substitute_template_variables(text, client, Note.new("foo", {}, {})))
end)
end)
end)

0 comments on commit 122c58a

Please sign in to comment.