Skip to content

Commit 50aef45

Browse files
committed
Add Lua utils function quarto.utils.is_empty_node
1 parent 91b1c38 commit 50aef45

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/resources/pandoc/datadir/_utils.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,32 @@ local function match(...)
567567
return match_fun(reset, table.unpack(result))
568568
end
569569

570+
--- Returns `true` iff the given AST node is empty.
571+
-- A node is considered "empty" if it's an empty list, table, or a node
572+
-- without any text or nested AST nodes.
573+
local function is_empty_node (node)
574+
if not node then
575+
return true
576+
elseif type(node) == 'table' then
577+
-- tables are considered empty if they don't have any fields.
578+
return not next(node)
579+
elseif node.content then
580+
return not next(node.content)
581+
elseif node.caption then
582+
-- looks like an image, figure, or table
583+
if node.caption.long then
584+
return not next(node.caption.long)
585+
end
586+
return not next(node.caption)
587+
elseif node.text then
588+
-- looks like a code node or text node
589+
return node.text ~= ''
590+
else
591+
-- Not sure what this is, but it's probably not empty.
592+
return false
593+
end
594+
end
595+
570596
return {
571597
dump = dump,
572598
type = get_type,
@@ -577,6 +603,7 @@ return {
577603
},
578604
as_inlines = as_inlines,
579605
as_blocks = as_blocks,
606+
is_empty_node = is_empty_node,
580607
match = match,
581608
add_to_blocks = function(blocks, block)
582609
if pandoc.utils.type(blocks) ~= "Blocks" then

0 commit comments

Comments
 (0)