Skip to content

Commit

Permalink
Add Lua utils function quarto.utils.is_empty_node
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Nov 27, 2024
1 parent 697a3da commit 9794066
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/resources/pandoc/datadir/_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,32 @@ local function match(...)
return match_fun(reset, table.unpack(result))
end

--- Returns `true` iff the given AST node is empty.
-- A node is considered "empty" if it's an empty list, table, or a node
-- without any text or nested AST nodes.
local function is_empty_node (node)
if not node then
return true
elseif type(node) == 'table' then
-- tables are considered empty if they don't have any fields.
return not next(node)
elseif node.content then
return not next(node.content)
elseif node.caption then
-- looks like an image, figure, or table
if node.caption.long then
return not next(node.caption.long)
end
return not next(node.caption)
elseif node.text then
-- looks like a code node or text node
return node.text ~= ''
else
-- Not sure what this is, but it's probably not empty.
return false
end
end

return {
dump = dump,
type = get_type,
Expand All @@ -567,6 +593,7 @@ return {
},
as_inlines = as_inlines,
as_blocks = as_blocks,
is_empty_node = is_empty_node,
match = match,
add_to_blocks = function(blocks, block)
if pandoc.utils.type(blocks) ~= "Blocks" then
Expand Down

0 comments on commit 9794066

Please sign in to comment.