Skip to content

Commit 6202285

Browse files
authored
feat(summary): reimplement nested categories (#1274)
1 parent 7e4fb30 commit 6202285

File tree

1 file changed

+88
-55
lines changed

1 file changed

+88
-55
lines changed

lua/neorg/modules/core/summary/module.lua

+88-55
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,57 @@ module.load = function()
7979
end
8080
end
8181

82+
-- Return true if catagories_path is or is a subcategory of an entry in included_categories
83+
local is_included_category = function (included_categories, category_path)
84+
local found_match = false
85+
for _, included in ipairs(included_categories) do
86+
local included_path = vim.split(included, ".", { plain = true })
87+
for i, path in ipairs(included_path) do
88+
if path == category_path[i] and i == #included_path then
89+
found_match = true
90+
break
91+
elseif path ~= category_path[i] then
92+
break
93+
end
94+
end
95+
end
96+
return found_match
97+
end
98+
99+
-- Insert a categorized record for the given file into the categories table
100+
local insert_categorized = function (categories, category_path, norgname, metadata)
101+
local leaf_categories = categories
102+
for i, path in ipairs(category_path) do
103+
local titled_path = lib.title(path)
104+
if i == #category_path then
105+
-- There are no more sub catergories so insert the record
106+
table.insert(leaf_categories[titled_path], {
107+
title = tostring(metadata.title),
108+
norgname = norgname,
109+
description = metadata.description,
110+
})
111+
break
112+
end
113+
local sub_categories = vim.defaulttable()
114+
if leaf_categories[titled_path] then
115+
-- This category already been added so find it's sub_categories table
116+
for _, item in ipairs(leaf_categories[titled_path]) do
117+
if item.sub_categories then
118+
leaf_categories = item.sub_categories
119+
goto continue
120+
end
121+
end
122+
end
123+
-- This is a new sub category
124+
table.insert(leaf_categories[titled_path], {
125+
title = titled_path,
126+
sub_categories = sub_categories,
127+
})
128+
leaf_categories = sub_categories
129+
::continue::
130+
end
131+
end
132+
82133
module.config.public.strategy = lib.match(module.config.public.strategy)({
83134
default = function()
84135
return function(files, ws_root, heading_level, include_categories)
@@ -107,78 +158,60 @@ module.load = function()
107158
elseif not vim.tbl_islist(metadata.categories) then
108159
metadata.categories = { tostring(metadata.categories) }
109160
end
110-
for _, category in ipairs(metadata.categories) do
161+
162+
if not metadata.title then
163+
metadata.title = get_first_heading_title(bufnr)
111164
if not metadata.title then
112-
metadata.title = get_first_heading_title(bufnr)
113-
if not metadata.title then
114-
metadata.title = vim.fs.basename(norgname)
115-
end
165+
metadata.title = vim.fs.basename(norgname)
116166
end
167+
end
117168

118-
if metadata.description == vim.NIL then
119-
metadata.description = nil
120-
end
169+
if metadata.description == vim.NIL then
170+
metadata.description = nil
171+
end
121172

122-
if not include_categories or vim.tbl_contains(include_categories, category:lower()) then
123-
table.insert(categories[lib.title(category)], {
124-
title = tostring(metadata.title),
125-
norgname = norgname,
126-
description = metadata.description,
127-
})
173+
for _, category in ipairs(metadata.categories) do
174+
local category_path = vim.split(category, ".", { plain = true })
175+
176+
if include_categories then
177+
if is_included_category(include_categories, category_path) then
178+
insert_categorized(categories, category_path, norgname, metadata)
179+
end
180+
else
181+
insert_categorized(categories, category_path, norgname, metadata)
128182
end
129-
130-
-- local leaf_categories = categories
131-
-- local categories_path = vim.split(category, ".", { plain = true })
132-
-- for i, path in ipairs(categories_path) do
133-
-- local titled_path = lib.title(path)
134-
-- if i == #categories_path then
135-
-- table.insert(leaf_categories[titled_path], {
136-
-- title = tostring(metadata.title),
137-
-- norgname = norgname,
138-
-- description = metadata.description,
139-
-- })
140-
-- break
141-
-- end
142-
-- local sub_categories = vim.defaulttable()
143-
-- if leaf_categories[titled_path] then
144-
-- for _, item in ipairs(leaf_categories[titled_path]) do
145-
-- if item.sub_categories then
146-
-- leaf_categories = item.sub_categories
147-
-- goto continue
148-
-- end
149-
-- end
150-
-- end
151-
-- table.insert(leaf_categories[titled_path], {
152-
-- title = titled_path,
153-
-- sub_categories = sub_categories,
154-
-- })
155-
-- leaf_categories = sub_categories
156-
-- ::continue::
157-
-- end
158183
end
159184
end)
160-
local result = {}
161-
local prefix = string.rep("*", heading_level)
162185

163-
for category, data in vim.spairs(categories) do
164-
table.insert(result, prefix .. " " .. category)
186+
local result = {}
187+
local starting_prefix = string.rep("*", heading_level)
165188

189+
local function add_category(category, data, level)
190+
local new_prefix = starting_prefix .. string.rep("*", level)
191+
table.insert(result, new_prefix .. " " .. category)
166192
for _, datapoint in ipairs(data) do
167-
table.insert(
168-
result,
169-
table.concat({
170-
string.rep(" ", heading_level),
193+
if datapoint.sub_categories then
194+
level = level + 1
195+
for sub_category, sub_data in vim.spairs(datapoint.sub_categories) do
196+
add_category(sub_category, sub_data, level)
197+
end
198+
else
199+
table.insert(result, table.concat({
200+
string.rep(" ", level + 1),
171201
" - {:$",
172202
datapoint.norgname,
173203
":}[",
174204
lib.title(datapoint.title),
175205
"]",
176-
})
177-
.. (datapoint.description and (table.concat({ " - ", datapoint.description })) or "")
178-
)
206+
})
207+
.. (datapoint.description and (table.concat({ " - ", datapoint.description })) or "")
208+
)
209+
end
179210
end
180211
end
181-
212+
for category, data in vim.spairs(categories) do
213+
add_category(category, data, 0)
214+
end
182215
return result
183216
end
184217
end,

0 commit comments

Comments
 (0)