From 8fcc8d8b5342158250a2d58c08bc2dce678d6f37 Mon Sep 17 00:00:00 2001 From: noriapi <70106808+noriapi@users.noreply.github.com> Date: Tue, 25 Jul 2023 01:44:59 +0900 Subject: [PATCH] Refactor `client.today` and `client.yesterday` (#154) * Refactor `client.today` and `client.yesterday` * Update CHANGELOG --- CHANGELOG.md | 4 ++++ lua/obsidian/init.lua | 43 ++++++++++++++++--------------------------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 619dc9048..b1f0e19b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Changed + +- (internal) Refactored daily note creation. + ## [v1.12.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v1.12.0) - 2023-07-15 ### Added diff --git a/lua/obsidian/init.lua b/lua/obsidian/init.lua index f230f3e4e..7cf339a06 100644 --- a/lua/obsidian/init.lua +++ b/lua/obsidian/init.lua @@ -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. @@ -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.