diff --git a/CHANGELOG.md b/CHANGELOG.md index fc23ca715..ed0cfee56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Made workspace detection more robust. - Fixed regression where frontmatter is updated in template files. - Fixed finding backlinks with URL-encoded path references. +- Fixed using templates with frontmatter when `disable_frontmatter` is set to true. Previously the frontmatter would be removed when the template was inserted, now it will be kept unchanged. ## [v3.7.12](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.12) - 2024-05-02 diff --git a/lua/obsidian/note.lua b/lua/obsidian/note.lua index 26e166f27..59db369ad 100644 --- a/lua/obsidian/note.lua +++ b/lua/obsidian/note.lua @@ -683,10 +683,12 @@ Note.save = function(self, opts) local save_path = Path.new(assert(opts.path or self.path)):resolve() assert(save_path:parent()):mkdir { parents = true, exist_ok = true } - -- Read contents from existing file or buffer, if there is one, skipping frontmatter. + -- Read contents from existing file or buffer, if there is one. -- TODO: check for open buffer? ---@type string[] local content = {} + ---@type string[] + local existing_frontmatter = {} if self.path ~= nil and self.path:is_file() then with(open(tostring(self.path)), function(reader) local in_frontmatter, at_boundary = false, false -- luacheck: ignore (false positive) @@ -703,6 +705,8 @@ Note.save = function(self, opts) if not in_frontmatter and not at_boundary then table.insert(content, line) + else + table.insert(existing_frontmatter, line) end end end) @@ -722,7 +726,8 @@ Note.save = function(self, opts) -- Replace frontmatter. new_lines = vim.tbl_flatten { self:frontmatter_lines(false, opts.frontmatter), content } else - new_lines = content + -- Use existing frontmatter. + new_lines = vim.tbl_flatten { existing_frontmatter, content } end -- Write new lines.