Skip to content

Add option to define function to customize dailies #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ require("obsidian").setup {
template = nil,
-- Optional, if you want `Obsidian yesterday` to return the last work day or `Obsidian tomorrow` to return the next work day.
workdays_only = true,
-- Optional, a function for more complex customization.
-- If defined, it is called with datetime (integer) as its only argument.
-- The return value must be a tuple (string, string|nil) which represents note path and alias where alias is optional.
-- The note path must be relative and is treated as a sub path of the vault! Don't forget to add ".md" at the end if desired.
-- If this function is defined, it's returned values are used instead of folder, date_format and alias_format.
func = nil,
},

-- Optional, completion of wiki links, local markdown links, and tags using nvim-cmp.
Expand Down
28 changes: 25 additions & 3 deletions lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Client.set_workspace = function(self, workspace, opts)
notes_subdir:mkdir { parents = true, exists_ok = true }
end

if self.opts.daily_notes.folder ~= nil then
if self.opts.daily_notes.func == nil and self.opts.daily_notes.folder ~= nil then
local daily_notes_subdir = self.dir / self.opts.daily_notes.folder
daily_notes_subdir:mkdir { parents = true, exists_ok = true }
end
Expand Down Expand Up @@ -643,6 +643,8 @@ Client.resolve_note_async = function(self, query, callback, opts)
paths_to_check[#paths_to_check + 1] = self.dir / self.opts.notes_subdir / fname
end

-- TODO: Check the notes created by custom daily notes func...

if self.opts.daily_notes.folder ~= nil then
paths_to_check[#paths_to_check + 1] = self.dir / self.opts.daily_notes.folder / fname
end
Comment on lines +646 to 650
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still unsure about this.

Expand Down Expand Up @@ -1717,6 +1719,7 @@ Client.parse_title_id_path = function(self, title, id, dir)
-- note is actually in the workspace.
and self.dir:is_parent_of(bufpath)
-- note is not in dailies folder
-- TODO: handle custom daily notes function
and (self.opts.daily_notes.folder == nil or not (self.dir / self.opts.daily_notes.folder):is_parent_of(bufpath))
then
base_dir = self.buf_dir or assert(bufpath:parent())
Expand Down Expand Up @@ -1966,11 +1969,30 @@ end
Client._daily = function(self, datetime, opts)
opts = opts or {}

local path, id = self:daily_note_path(datetime)
---@type string|?, string|?
local custom_path, custom_alias
if self.opts.daily_notes.func ~= nil then
custom_path, custom_alias = self.opts.daily_notes.func(datetime)
if custom_path == nil then
error "Custom daily notes function must return non-nil path"
end
end

---@type obsidian.Path|?, string|number
local path, id
if custom_path ~= nil then
path = Path:new(self.dir) / custom_path
path:parent():mkdir { parents = true, exists_ok = true }
id = path.stem
else
path, id = self:daily_note_path(datetime)
end

---@type string|?
local alias
if self.opts.daily_notes.alias_format ~= nil then
if custom_alias ~= nil then
alias = custom_alias
elseif self.opts.daily_notes.alias_format ~= nil then
alias = tostring(os.date(self.opts.daily_notes.alias_format, datetime))
end

Expand Down
29 changes: 27 additions & 2 deletions lua/obsidian/commands/dailies.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local log = require "obsidian.log"
local Path = require "obsidian.path"

---@param arg string
---@return number
Expand Down Expand Up @@ -46,8 +47,32 @@ return function(client, data)
local dailies = {}
for offset = offset_end, offset_start, -1 do
local datetime = os.time() + (offset * 3600 * 24)
local daily_note_path = client:daily_note_path(datetime)
local daily_note_alias = tostring(os.date(client.opts.daily_notes.alias_format or "%A %B %-d, %Y", datetime))

---@type string|?, string|?
local custom_path, custom_alias
if client.opts.daily_notes.func ~= nil then
custom_path, custom_alias = client.opts.daily_notes.func(datetime)
if custom_path == nil then
error "Custom daily notes function must return non-nil path"
end
end

---@type obsidian.Path|?
local daily_note_path
if custom_path ~= nil then
daily_note_path = Path:new(client.dir) / custom_path
else
daily_note_path = client:daily_note_path(datetime)
end

---@type string|?
local daily_note_alias
if custom_alias ~= nil then
daily_note_alias = custom_alias
else
daily_note_alias = tostring(os.date(client.opts.daily_notes.alias_format or "%A %B %-d, %Y", datetime))
end

if offset == 0 then
daily_note_alias = daily_note_alias .. " @today"
elseif offset == -1 then
Expand Down
2 changes: 2 additions & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ end
---@field template string|?
---@field default_tags string[]|?
---@field workdays_only boolean
---@field func? fun(datetime: integer): (string, string|?) default to nil
config.DailyNotesOpts = {}

--- Get defaults.
Expand All @@ -443,6 +444,7 @@ config.DailyNotesOpts.default = function()
alias_format = nil,
default_tags = { "daily-notes" },
workdays_only = true,
func = nil,
}
end

Expand Down
12 changes: 12 additions & 0 deletions tests/test_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ describe("Client", function()
end)
end)

it("should be able to initialize a daily note with custom func", function()
with_tmp_client(function(client)
client.opts.daily_notes.func = function(datetime)
local note = os.date("%Y/%m-%B/%Y-%m-%d", datetime)
return "journal/" .. note .. ".md"
end
local note = client:today()
MiniTest.expect.equality(true, note.path ~= nil)
MiniTest.expect.equality(true, note:exists())
end)
end)

it("should not add frontmatter for today when disabled", function()
with_tmp_client(function(client)
client.opts.disable_frontmatter = true
Expand Down