Skip to content

Commit

Permalink
Fixed using templates with frontmatter with disable_frontmatter=true
Browse files Browse the repository at this point in the history
Closes #588.
  • Loading branch information
epwalsh committed May 17, 2024
1 parent 6943cb4 commit 7b59d90
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions lua/obsidian/note.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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.
Expand Down

0 comments on commit 7b59d90

Please sign in to comment.