Skip to content

Commit bdc799f

Browse files
authored
fix: support YAML list format for frontmatter tags (#29)
The tag search parser previously only supported bracket format tags: [tag1, tag2] but failed to parse the standard YAML list format with tags on separate lines (tags:\n - tag1). This caused "No tags found in frontmatter" errors for notes using list format. Updated parser to handle both formats: - Bracket format: tags: [tag1, tag2, tag3] - YAML list format: tags:\n - tag1\n - tag2 Parser now enters list mode when tags: line is not in bracket format and processes subsequent lines starting with - until hitting a non-list item or end of frontmatter section.
1 parent d538088 commit bdc799f

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

lua/markdown-notes/notes.lua

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,36 @@ function M.search_tags()
170170
for _, file in ipairs(all_files) do
171171
local content = vim.fn.readfile(file)
172172
local in_frontmatter = false
173+
local in_tags_list = false
173174

174-
for _, line in ipairs(content) do
175+
for i, line in ipairs(content) do
175176
if line == "---" then
176177
in_frontmatter = not in_frontmatter
177-
elseif in_frontmatter and line:match("^tags:") then
178-
-- Extract tags from YAML array format: tags: [tag1, tag2, tag3]
179-
local tags_line = line:gsub("^tags:%s*", "")
180-
if tags_line:match("^%[.*%]$") then
181-
-- Remove brackets and split by comma
182-
local tags_content = tags_line:gsub("^%[", ""):gsub("%]$", "")
183-
for tag in tags_content:gmatch("[^,]+") do
178+
in_tags_list = false
179+
elseif in_frontmatter then
180+
if line:match("^tags:") then
181+
-- Extract tags from YAML array format: tags: [tag1, tag2, tag3]
182+
local tags_line = line:gsub("^tags:%s*", "")
183+
if tags_line:match("^%[.*%]$") then
184+
-- Remove brackets and split by comma
185+
local tags_content = tags_line:gsub("^%[", ""):gsub("%]$", "")
186+
for tag in tags_content:gmatch("[^,]+") do
187+
local clean_tag = tag:gsub("%s", ""):gsub('"', ""):gsub("'", "")
188+
if clean_tag ~= "" then
189+
if not tags[clean_tag] then
190+
tags[clean_tag] = {}
191+
end
192+
table.insert(tags[clean_tag], file)
193+
end
194+
end
195+
else
196+
-- YAML list format: tags are on following lines starting with -
197+
in_tags_list = true
198+
end
199+
elseif in_tags_list and line:match("^%s*-%s+") then
200+
-- Extract tag from YAML list item: " - tagname"
201+
local tag = line:match("^%s*-%s+(.+)")
202+
if tag then
184203
local clean_tag = tag:gsub("%s", ""):gsub('"', ""):gsub("'", "")
185204
if clean_tag ~= "" then
186205
if not tags[clean_tag] then
@@ -189,8 +208,10 @@ function M.search_tags()
189208
table.insert(tags[clean_tag], file)
190209
end
191210
end
211+
elseif in_tags_list and not line:match("^%s*-%s+") and not line:match("^%s*$") then
212+
-- Hit a non-list-item, non-empty line, stop looking for tags
213+
in_tags_list = false
192214
end
193-
break -- Only process first tags line in frontmatter
194215
end
195216
end
196217
end

0 commit comments

Comments
 (0)