Skip to content

Commit e9b9fdd

Browse files
feat: add option for disabling frontmatter updates
1 parent 14e0427 commit e9b9fdd

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

lua/obsidian/client.lua

+34-6
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Client.templates_dir = function(self, workspace)
276276
return nil
277277
end
278278

279-
--- Determines whether a note's frontmatter is managed by obsidian.nvim.
279+
--- Determines whether a note's frontmatter is created by obsidian.nvim.
280280
---
281281
---@param note obsidian.Note
282282
---
@@ -295,10 +295,38 @@ Client.should_save_frontmatter = function(self, note)
295295

296296
if not note:should_save_frontmatter() then
297297
return false
298-
elseif type(self.opts.disable_frontmatter) == "boolean" then
299-
return not self.opts.disable_frontmatter
300-
elseif type(self.opts.disable_frontmatter) == "function" then
301-
return not self.opts.disable_frontmatter(tostring(self:vault_relative_path(note.path, { strict = true })))
298+
elseif type(self.opts.disable_frontmatter_creation) == "boolean" then
299+
return not self.opts.disable_frontmatter_creation
300+
elseif type(self.opts.disable_frontmatter_creation) == "function" then
301+
return not self.opts.disable_frontmatter_creation(tostring(self:vault_relative_path(note.path, { strict = true })))
302+
else
303+
return true
304+
end
305+
end
306+
307+
--- Determines whether a note's frontmatter can be updated obsidian.nvim
308+
---
309+
---@param note obsidian.Note
310+
---
311+
---@return boolean
312+
Client.should_update_frontmatter = function(self, note)
313+
-- Check if the note is a template.
314+
local templates_dir = self:templates_dir()
315+
if templates_dir ~= nil then
316+
templates_dir = templates_dir:resolve()
317+
for _, parent in ipairs(note.path:parents()) do
318+
if parent == templates_dir then
319+
return false
320+
end
321+
end
322+
end
323+
324+
if not note:should_save_frontmatter() then
325+
return false
326+
elseif type(self.opts.disable_frontmatter_update) == "boolean" then
327+
return not self.opts.disable_frontmatter_update
328+
elseif type(self.opts.disable_frontmatter_update) == "function" then
329+
return not self.opts.disable_frontmatter_update(tostring(self:vault_relative_path(note.path, { strict = true })))
302330
else
303331
return true
304332
end
@@ -1901,7 +1929,7 @@ end
19011929
---
19021930
---@return boolean updated If the the frontmatter was updated.
19031931
Client.update_frontmatter = function(self, note, bufnr)
1904-
if not self:should_save_frontmatter(note) then
1932+
if not self:should_update_frontmatter(note) then
19051933
return false
19061934
end
19071935

lua/obsidian/config.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ local config = {}
1818
---@field follow_url_func fun(url: string)|?
1919
---@field follow_img_func fun(img: string)|?
2020
---@field note_frontmatter_func (fun(note: obsidian.Note): table)|?
21-
---@field disable_frontmatter (fun(fname: string?): boolean)|boolean|?
21+
---@field disable_frontmatter_creation (fun(fname: string?): boolean)|boolean|?
22+
---@field disable_frontmatter_update (fun(fname: string?): boolean)|boolean|?
2223
---@field completion obsidian.config.CompletionOpts
2324
---@field mappings obsidian.config.MappingOpts
2425
---@field picker obsidian.config.PickerOpts
@@ -52,6 +53,7 @@ config.ClientOpts.default = function()
5253
follow_url_func = nil,
5354
note_frontmatter_func = nil,
5455
disable_frontmatter = false,
56+
update_frontmatter = false,
5557
completion = config.CompletionOpts.default(),
5658
mappings = config.MappingOpts.default(),
5759
picker = config.PickerOpts.default(),

test/obsidian/client_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("Client", function()
4242

4343
it("should not add frontmatter for today when disabled", function()
4444
with_tmp_client(function(client)
45-
client.opts.disable_frontmatter = true
45+
client.opts.disable_frontmatter_creation = true
4646
local new_note = client:today()
4747

4848
local saved_note = Note.from_file(new_note.path)
@@ -52,7 +52,7 @@ describe("Client", function()
5252

5353
it("should not add frontmatter for yesterday when disabled", function()
5454
with_tmp_client(function(client)
55-
client.opts.disable_frontmatter = true
55+
client.opts.disable_frontmatter_creation = true
5656
local new_note = client:yesterday()
5757

5858
local saved_note = Note.from_file(new_note.path)

0 commit comments

Comments
 (0)