diff --git a/CHANGELOG.md b/CHANGELOG.md index 5356b748c..5a37eb62e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed an issue where template insertion occurred below the intended line, it now correctly inserts at the current line. - Fixed `:ObsidianOpen` issue on WSL OS name identifier check with different release name case. +- Ensure ID of daily notes is always equal to the stem of the path. + +### Changed + +- Don't insert a default alias for daily notes when `daily_notes.alias_format` is `nil`. ## [v3.7.8](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.8) - 2024-04-09 diff --git a/lua/obsidian/client.lua b/lua/obsidian/client.lua index bf00a028a..7774748b2 100644 --- a/lua/obsidian/client.lua +++ b/lua/obsidian/client.lua @@ -1871,6 +1871,9 @@ Client.daily_note_path = function(self, datetime) path = path / (id .. ".md") + -- ID may contain additional path components, so make sure we use the stem. + id = path.stem + return path, id end @@ -1888,11 +1891,10 @@ Client._daily = function(self, datetime, opts) local path, id = self:daily_note_path(datetime) + ---@type string|? local alias if self.opts.daily_notes.alias_format ~= nil then alias = tostring(os.date(self.opts.daily_notes.alias_format, datetime)) - else - alias = tostring(os.date("%B %d, %Y", datetime)) end ---@type obsidian.Note @@ -1900,8 +1902,13 @@ Client._daily = function(self, datetime, opts) if path:exists() then note = Note.from_file(path, opts.load) else - note = Note.new(id, { alias }, { "daily-notes" }, path) - note.title = alias + note = Note.new(id, {}, { "daily-notes" }, path) + + if alias then + note:add_alias(alias) + note.title = alias + end + if not opts.no_write then self:write_note(note, { template = self.opts.daily_notes.template }) end diff --git a/test/obsidian/client_spec.lua b/test/obsidian/client_spec.lua index 14d6bcba9..a2bc1d1fb 100644 --- a/test/obsidian/client_spec.lua +++ b/test/obsidian/client_spec.lua @@ -216,3 +216,14 @@ describe("Client:create_note()", function() end) end) end) + +describe("Client:daily_note_path()", function() + it("should use the path stem as the ID", function() + with_tmp_client(function(client) + client.opts.daily_notes.date_format = "%Y/%b/%Y-%m-%d" + local path, id = client:daily_note_path() + assert(vim.endswith(tostring(path), tostring(os.date("%Y/%b/%Y-%m-%d.md", os.time())))) + assert.equals(id, os.date("%Y-%m-%d", os.time())) + end) + end) +end)