diff --git a/CHANGELOG.md b/CHANGELOG.md index b585ed0e2..cc61fd758 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 `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 diff --git a/lua/obsidian/templates.lua b/lua/obsidian/templates.lua index ac8a83f27..2d8ff1d4b 100644 --- a/lua/obsidian/templates.lua +++ b/lua/obsidian/templates.lua @@ -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 diff --git a/test/obsidian/templates_spec.lua b/test/obsidian/templates_spec.lua index 411d7485f..c0ef3df0a 100644 --- a/test/obsidian/templates_spec.lua +++ b/test/obsidian/templates_spec.lua @@ -19,8 +19,14 @@ 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"), @@ -28,18 +34,43 @@ describe("templates.substitute_template_variables()", function() ) 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)