Skip to content

Commit 47fc968

Browse files
committed
Support note types with the same open and close tag
1 parent 0f56db1 commit 47fc968

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

core/fold.lua

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ M.parse = function(ctx)
111111
line = string.sub(line, indent_chars + 1)
112112

113113
for note_idx, note_type in ipairs(note_schema) do
114-
local prefix = note_type[2]
115-
if str.starts_with(line, prefix) then
114+
local open_tag = note_type[2]
115+
local do_not_open_only_close = line == open_tag
116+
and curr_section
117+
and open_tag == curr_section:close_tag()
118+
if
119+
str.starts_with(line, open_tag) and not do_not_open_only_close
120+
then
116121
local new_section
117122
new_section, err = Section:new(
118123
id,
@@ -134,10 +139,14 @@ M.parse = function(ctx)
134139
end
135140
end
136141
-- TODO(gitpushjoe): maybe still check if a suffix for another note type slipped through?
142+
local do_not_close_only_open = line == curr_section:open_tag()
143+
and line == curr_section:close_tag()
144+
and curr_section.start_line == i
137145
if
138146
curr_section
139-
and curr_section.type[1] ~= "ROOT"
147+
and not curr_section:is_root()
140148
and str.ends_with(line, curr_section:close_tag())
149+
and not do_not_close_only_open
141150
then
142151
curr_section.end_line = i
143152
curr_section = curr_section.parent
@@ -157,7 +166,7 @@ M.prepare = function(section_root, ctx)
157166
local created_files = {}
158167

159168
local _, err = traverse.preorder(section_root, function(section)
160-
if section.type[1] == "ROOT" then
169+
if section:is_root() then
161170
section.path = ctx.dest_path
162171
return
163172
end
@@ -210,7 +219,7 @@ M.prepare = function(section_root, ctx)
210219
end
211220

212221
_, err = traverse.postorder(section_root, function(section)
213-
if section.type[1] == "ROOT" then
222+
if section:is_root() then
214223
section.lines = ctx.lines
215224
return
216225
end
@@ -350,7 +359,7 @@ M.execute = function(section_root, ctx, is_dry_run)
350359
end
351360

352361
while true do
353-
if section.type[1] == "ROOT" then
362+
if section:is_root() then
354363
is_overwrite = ctx.src_path == ctx.dest_path
355364
break
356365
end
@@ -379,7 +388,7 @@ M.execute = function(section_root, ctx, is_dry_run)
379388
full_path = tostring(section.path) or ""
380389
end
381390

382-
if section.type[1] == "ROOT" and ctx.preserve then
391+
if section:is_root() and ctx.preserve then
383392
return
384393
end
385394

0 commit comments

Comments
 (0)