@@ -79,6 +79,57 @@ module.load = function()
79
79
end
80
80
end
81
81
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
+
82
133
module .config .public .strategy = lib .match (module .config .public .strategy )({
83
134
default = function ()
84
135
return function (files , ws_root , heading_level , include_categories )
@@ -107,78 +158,60 @@ module.load = function()
107
158
elseif not vim .tbl_islist (metadata .categories ) then
108
159
metadata .categories = { tostring (metadata .categories ) }
109
160
end
110
- for _ , category in ipairs (metadata .categories ) do
161
+
162
+ if not metadata .title then
163
+ metadata .title = get_first_heading_title (bufnr )
111
164
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 )
116
166
end
167
+ end
117
168
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
121
172
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 )
128
182
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
158
183
end
159
184
end )
160
- local result = {}
161
- local prefix = string.rep (" *" , heading_level )
162
185
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 )
165
188
189
+ local function add_category (category , data , level )
190
+ local new_prefix = starting_prefix .. string.rep (" *" , level )
191
+ table.insert (result , new_prefix .. " " .. category )
166
192
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 ),
171
201
" - {:$" ,
172
202
datapoint .norgname ,
173
203
" :}[" ,
174
204
lib .title (datapoint .title ),
175
205
" ]" ,
176
- })
177
- .. (datapoint .description and (table.concat ({ " - " , datapoint .description })) or " " )
178
- )
206
+ })
207
+ .. (datapoint .description and (table.concat ({ " - " , datapoint .description })) or " " )
208
+ )
209
+ end
179
210
end
180
211
end
181
-
212
+ for category , data in vim .spairs (categories ) do
213
+ add_category (category , data , 0 )
214
+ end
182
215
return result
183
216
end
184
217
end ,
0 commit comments