Skip to content

Commit 35da593

Browse files
authored
fix(journal): journal index generation (#1680)
1 parent a1e1961 commit 35da593

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

lua/neorg/modules/core/dirman/module.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ module.private = {
151151

152152
---@class core.dirman
153153
module.public = {
154+
---@return table<string, PathlibPath>
154155
get_workspaces = function()
155156
return module.config.public.workspaces
156157
end,
@@ -160,6 +161,7 @@ module.public = {
160161
end,
161162
--- If present retrieve a workspace's path by its name, else returns nil
162163
---@param name string #The name of the workspace
164+
---@return PathlibPath
163165
get_workspace = function(name)
164166
return module.config.public.workspaces[name]
165167
end,

lua/neorg/modules/core/journal/module.lua

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ module.setup = function()
9797
}
9898
end
9999

100+
---@type core.integrations.treesitter
101+
local ts
102+
module.load = function()
103+
ts = module.required["core.integrations.treesitter"] --[[@as core.integrations.treesitter]]
104+
105+
if module.config.private.strategies[module.config.public.strategy] then
106+
module.config.public.strategy = module.config.private.strategies[module.config.public.strategy]
107+
end
108+
109+
modules.await("core.neorgcmd", function(neorgcmd)
110+
neorgcmd.add_commands_from_table({
111+
journal = {
112+
min_args = 1,
113+
max_args = 2,
114+
subcommands = {
115+
tomorrow = { args = 0, name = "journal.tomorrow" },
116+
yesterday = { args = 0, name = "journal.yesterday" },
117+
today = { args = 0, name = "journal.today" },
118+
custom = { max_args = 1, name = "journal.custom" }, -- format :yyyy-mm-dd
119+
template = { args = 0, name = "journal.template" },
120+
toc = {
121+
args = 1,
122+
name = "journal.toc",
123+
subcommands = {
124+
open = { args = 0, name = "journal.toc.open" },
125+
update = { args = 0, name = "journal.toc.update" },
126+
},
127+
},
128+
},
129+
},
130+
})
131+
end)
132+
end
133+
100134
module.config.public = {
101135
-- Which workspace to use for the journal files, the default behaviour
102136
-- is to use the current workspace.
@@ -251,11 +285,21 @@ module.public = {
251285
return handle
252286
end
253287

254-
-- Gets the title from the metadata of a file, must be called in a vim.schedule
288+
---Gets the title from the metadata of a file, must be called in a vim.schedule
289+
---@param file PathlibPath
290+
---@return string?
255291
local get_title = function(file)
256-
local buffer = vim.fn.bufadd(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. file)
257-
local meta = module.required["core.integrations.treesitter"].get_document_metadata(buffer)
258-
return meta.title
292+
local path = workspace_path / folder_name / file
293+
local meta
294+
if vim.fn.bufexists(tostring(path)) == 1 then
295+
local buf = vim.fn.bufloaded(vim.fn.bufnr(tostring(path)))
296+
meta = ts.get_document_metadata(buf)
297+
else
298+
meta = ts.get_document_metadata(path)
299+
end
300+
if meta then
301+
return meta.title
302+
end
259303
end
260304

261305
vim.loop.fs_scandir(workspace_path .. config.pathsep .. folder_name .. config.pathsep, function(err, handle)
@@ -416,36 +460,6 @@ module.public = {
416460
end,
417461
}
418462

419-
module.load = function()
420-
if module.config.private.strategies[module.config.public.strategy] then
421-
module.config.public.strategy = module.config.private.strategies[module.config.public.strategy]
422-
end
423-
424-
modules.await("core.neorgcmd", function(neorgcmd)
425-
neorgcmd.add_commands_from_table({
426-
journal = {
427-
min_args = 1,
428-
max_args = 2,
429-
subcommands = {
430-
tomorrow = { args = 0, name = "journal.tomorrow" },
431-
yesterday = { args = 0, name = "journal.yesterday" },
432-
today = { args = 0, name = "journal.today" },
433-
custom = { max_args = 1, name = "journal.custom" }, -- format :yyyy-mm-dd
434-
template = { args = 0, name = "journal.template" },
435-
toc = {
436-
args = 1,
437-
name = "journal.toc",
438-
subcommands = {
439-
open = { args = 0, name = "journal.toc.open" },
440-
update = { args = 0, name = "journal.toc.update" },
441-
},
442-
},
443-
},
444-
},
445-
})
446-
end)
447-
end
448-
449463
module.on_event = function(event)
450464
if event.split_type[1] == "core.neorgcmd" then
451465
if event.split_type[2] == "journal.tomorrow" then

0 commit comments

Comments
 (0)