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

Fix bug where ObsidianNewFromTemplate` did not respect note_id_func #774

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed an edge case with collecting backlinks.
- Fixed typo in `ObsidianPasteImg`'s command description
- Fixed the case when `opts.attachments` is `nil`.
- Fixed bug where `ObsidianNewFromTemplate` did not respect `note_id_func` (#737)

## [v3.9.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.9.0) - 2024-07-11

Expand Down
1 change: 1 addition & 0 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,7 @@ Client.write_note_to_buffer = function(self, note, opts)

if opts.template and util.buffer_is_empty(opts.bufnr) then
note = insert_template {
note = note,
template_name = opts.template,
client = self,
location = util.get_active_window_cursor_location(),
Expand Down
8 changes: 5 additions & 3 deletions lua/obsidian/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ end

---Insert a template at the given location.
---
---@param opts { template_name: string|obsidian.Path, client: obsidian.Client, location: { [1]: integer, [2]: integer, [3]: integer, [4]: integer } } Options.
---@param opts { note: obsidian.Note|?, template_name: string|obsidian.Path, client: obsidian.Client, location: { [1]: integer, [2]: integer, [3]: integer, [4]: integer } } Options.
---
---@return obsidian.Note
M.insert_template = function(opts)
local buf, win, row, _ = unpack(opts.location)
local note = Note.from_buffer(buf)
if opts.note == nil then
opts.note = Note.from_buffer(buf)
end

local template_path = resolve_template(opts.template_name, opts.client)

Expand All @@ -165,7 +167,7 @@ M.insert_template = function(opts)
if template_file then
local lines = template_file:lines()
for line in lines do
local new_lines = M.substitute_template_variables(line, opts.client, note)
local new_lines = M.substitute_template_variables(line, opts.client, opts.note)
if string.find(new_lines, "[\r\n]") then
local line_start = 1
for line_end in util.gfind(new_lines, "[\r\n]") do
Expand Down