Skip to content

Commit

Permalink
Refactor client.today and client.yesterday
Browse files Browse the repository at this point in the history
  • Loading branch information
noriapi committed Jul 21, 2023
1 parent 1982447 commit 0d364d9
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions lua/obsidian/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,19 @@ client.daily_note_path = function(self, id)
return path
end

---Create a new daily note for today.
---Open (or create) the daily note.
---
---@param self obsidian.Client
---@param datetime integer
---@return obsidian.Note
client.today = function(self)
---@type string|osdate
---@diagnostic disable-next-line: assign-type-mismatch
local formatted_date
client._daily = function(self, datetime)
local id
if self.opts.daily_notes.date_format ~= nil then
formatted_date = os.date(self.opts.daily_notes.date_format)
id = tostring(os.date(self.opts.daily_notes.date_format, datetime))
else
formatted_date = os.date "%Y-%m-%d"
id = tostring(os.date("%Y-%m-%d", datetime))
end
local id = tostring(formatted_date)
local alias = tostring(os.date "%B %-d, %Y")
local alias = tostring(os.date("%B %-d, %Y", datetime))
local path = self:daily_note_path(id)

-- Create Note object and save if it doesn't already exist.
Expand All @@ -335,28 +334,18 @@ client.today = function(self)
return note
end

---Open (or create) the daily note for today.
---
---@return obsidian.Note
client.today = function(self)
return self:_daily(os.time())
end

---Open (or create) the daily note from the last weekday.
---
---@return obsidian.Note
client.yesterday = function(self)
local yesterday = obsidian.util.working_day_before(os.time())
local id
if self.opts.daily_notes.date_format ~= nil then
id = tostring(os.date(self.opts.daily_notes.date_format, yesterday))
else
id = tostring(os.date("%Y-%m-%d", yesterday))
end
local alias = tostring(os.date("%B %-d, %Y", yesterday))
local path = self:daily_note_path(id)

-- Create Note object and save if it doesn't already exist.
local note = obsidian.note.new(id, { alias }, { "daily-notes" }, path)
if not note:exists() then
note:save(nil, not self.opts.disable_frontmatter)
echo.info("Created note " .. tostring(note.id) .. " at " .. tostring(note.path), self.opts.log_level)
end

return note
return self:_daily(obsidian.util.working_day_before(os.time()))
end

---Resolve the query to a single note.
Expand Down

0 comments on commit 0d364d9

Please sign in to comment.