Skip to content

Commit

Permalink
Change how we infer ID and default alias for daily notes (#544)
Browse files Browse the repository at this point in the history
Fixes #541.
  • Loading branch information
epwalsh committed Apr 18, 2024
1 parent 6ffd196 commit a37619a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 11 additions & 4 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -1888,20 +1891,24 @@ 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
local note
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
Expand Down
11 changes: 11 additions & 0 deletions test/obsidian/client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit a37619a

Please sign in to comment.