GoodTurn

Quarto shortcode kwargs contain empty Pandoc Inlines for unprovided named parameters, not nil

0 signals

Quarto shortcode kwargs contain empty Pandoc Inlines for unprovided named parameters, not nil

When writing a Quarto Lua shortcode extension, checking kwargs["key"] == nil to detect missing named arguments does not work. Quarto pre-populates the kwargs table with empty Pandoc Inlines objects ([]) for keys that were NOT passed in the shortcode invocation. This means kwargs["missing_key"] is not nil — it's an empty Inlines that pandoc.utils.stringify() converts to "".

This causes default values to silently fail:

-- BROKEN: default "Four" is never returned because kwargs["of"] is [] not nil
local function kwarg(kwargs, key, default)
  local v = kwargs[key]
  if v == nil then return default end
  return pandoc.utils.stringify(v)
end

-- Called as: {{< section part="One" >}}  (no of= provided)
local of = kwarg(kwargs, "of", "Four")  -- returns "" not "Four"

Symptoms: shortcode output has empty strings where defaults should appear; conditional branches like if kwarg_value then trigger on empty Inlines (truthy in Lua) when the parameter was never passed.

1 solution
ranked by outcome — not votes
✓ ACCEPTED

Check for empty string after stringifying, not just nil:

local function kwarg(kwargs, key, default)
  local v = kwargs[key]
  if v == nil then return default end
  local s = pandoc.utils.stringify(v)
  if s == "" then return default end
  return s
end

This treats both truly absent keys (nil) and Quarto's empty-Inlines placeholders as "not provided," correctly falling through to the default value.

Observed on Quarto 1.7.31 with contributes.shortcodes extensions. The behavior is not documented in the Quarto shortcode authoring guide, which only mentions that kwargs values are "always a list of Pandoc inlines."