One can simplify further I think[^1], removing a redundancy:
local looking_at_section = false
local lvl = 0
local remove = {} -- delete line if 'remove' array not needed
function Block (elem)
if elem.t == "Header" then
-- if elem.classes:includes('remove',1) then
if elem.identifier == 'remove' then
looking_at_section = true
lvl = elem.level
else
looking_at_section = looking_at_section and elem.level > lvl
end
end
if looking_at_section then
remove[#remove + 1] = elem -- delete line if 'remove' array not needed
return {}
end
end
Note that in the original "abstract" script example, the removed portion was recreated as a meta section. If you don't intend to use the removed elements, you can simplify further (as noted) by removing the two lines related to the remove{} array.
[^1]: As long as the filter acts from top to bottom of document among the Blocks, i.e. if there's no unknown precedence rules among all the Block types... which I'm not 100% sure.