Skip to content

Commit

Permalink
feat: update disable_frontmatter config
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Nov 30, 2023
1 parent 51d9495 commit 6b1444f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `disable_frontmatter` now can be a function to determine whether a note's frontmatter can be managed by obsidian.nvim or not.

### Added

- Added `Client:update_ui()` method.
Expand Down
24 changes: 22 additions & 2 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ Client.vault_relative_path = function(self, path)
end
end

---Determines whether a note's frontmatter is managed by obsidian.nvim.
---
---@param note obsidian.Note
---@return boolean
Client.should_save_frontmatter = function(self, note)
if not note:should_save_frontmatter() then
return false
end
if self.opts.disable_frontmatter == nil then
return true
end
if type(self.opts.disable_frontmatter) == "boolean" then
return self.opts.disable_frontmatter == false
end
if type(self.opts.disable_frontmatter) == "function" then
return not self.opts.disable_frontmatter(self:vault_relative_path(note.path))
end
return true
end

---@class obsidian.client.SearchOpts : obsidian.ABC
---@field sort boolean|?
---@field include_templates boolean|?
Expand Down Expand Up @@ -560,7 +580,7 @@ Client.new_note = function(self, title, id, dir, aliases)
if self.opts.note_frontmatter_func ~= nil then
frontmatter = self.opts.note_frontmatter_func(note)
end
note:save(nil, not self.opts.disable_frontmatter, frontmatter)
note:save(nil, not self:should_save_frontmatter(note), frontmatter)
log.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path))

return note
Expand Down Expand Up @@ -631,7 +651,7 @@ Client._daily = function(self, datetime)
if self.opts.note_frontmatter_func ~= nil then
frontmatter = self.opts.note_frontmatter_func(note)
end
note:save(nil, not self.opts.disable_frontmatter, frontmatter)
note:save(nil, not self:should_save_frontmatter(note), frontmatter)
end
log.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path))
end
Expand Down
2 changes: 1 addition & 1 deletion lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local config = {}
---@field note_id_func function|?
---@field follow_url_func function|?
---@field note_frontmatter_func function|?
---@field disable_frontmatter boolean|?
---@field disable_frontmatter (fun(fname: string?): boolean)|boolean|?
---@field backlinks obsidian.config.BacklinksOpts
---@field completion obsidian.config.CompletionOpts
---@field mappings obsidian.config.MappingOpts
Expand Down
2 changes: 1 addition & 1 deletion lua/obsidian/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ obsidian.setup = function(opts)

local bufnr = ev.buf
local note = obsidian.Note.from_buffer(bufnr, client.dir)
if not note:should_save_frontmatter() or client.opts.disable_frontmatter == true then
if not client:should_save_frontmatter(note) then
return
end

Expand Down

0 comments on commit 6b1444f

Please sign in to comment.