Skip to content

Commit

Permalink
Ensure fields transfer to new note when cloning from template
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Mar 27, 2024
1 parent df0c5cc commit f2eeb0a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Ensure fields transferred to new note when cloning from template.

## [v3.7.5](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.5) - 2024-03-22

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ Client.parse_title_id_path = function(self, title, id, dir)
if not base_dir:is_absolute() then
base_dir = self.dir / base_dir
else
base_dir = base_dir:resolve { strict = true }
base_dir = base_dir:resolve()
end
else
local bufpath = Path.buffer(0):resolve()
Expand Down
33 changes: 25 additions & 8 deletions lua/obsidian/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,41 @@ M.clone_template = function(opts)
assert(note_path:parent()):mkdir { parents = true, exist_ok = true }

local template_path = resolve_template(opts.template_name, opts.client)
local template_file = io.open(tostring(template_path), "r")

local template_file, read_err = io.open(tostring(template_path), "r")
if not template_file then
error(string.format("Unable to read template at '%s'", template_path))
error(string.format("Unable to read template at '%s': %s", template_path, tostring(read_err)))
end

local note_file = io.open(tostring(note_path), "wb")
local note_file, write_err = io.open(tostring(note_path), "w")
if not note_file then
error(string.format("Unable to write note at '%s'", note_path))
error(string.format("Unable to write note at '%s': %s", note_path, tostring(write_err)))
end

for line in template_file:lines "L" do
note_file:write(M.substitute_template_variables(line, opts.client, opts.note))
line = M.substitute_template_variables(line, opts.client, opts.note)
note_file:write(line)
end

template_file:close()
note_file:close()
assert(template_file:close())
assert(note_file:close())

local new_note = Note.from_file(note_path)
vim.print(new_note)

-- Transfer fields from `opts.note`.
new_note.id = opts.note.id
if new_note.title == nil then
new_note.title = opts.note.title
end
for _, alias in ipairs(opts.note.aliases) do
new_note:add_alias(alias)
end
for _, tag in ipairs(opts.note.tags) do
new_note:add_tag(tag)
end

return Note.from_file(note_path)
return new_note
end

---Insert a template at the given location.
Expand Down

0 comments on commit f2eeb0a

Please sign in to comment.