Pandoc:Lua-Filter 用于将 {{helloworld}} 替换为 <div>abc</div>

Pandoc: Lua-Filter for replacing {{helloworld}} with <div>abc</div>

manual 我找到了这个 pandoc 的例子 lua-filter:

return {
  {
    Str = function (elem)
      if elem.text == "{{helloworld}}" then
        return pandoc.Emph {pandoc.Str "Hello, World"}
      else
        return elem
      end
    end,
  }
}

我想用 <div>abc</div> 替换 {{helloworld}}。我的尝试:

return {
  {
    Str = function (elem)
      if elem.text == "{{helloworld}}" then
        return pandoc.RawInline('html','<div>abc</div>')
      else
        return elem
      end
    end,
  }
}

...但这给了我以下输出:

<p></p>
<div>abc</div>
<p></p>

如何去掉空的 p 标签?

附加信息

我从 markdown 转换为 html,我的 markdown 文件如下所示:

manual 说:

The function’s output must result in an element of the same type as the input. This means a filter function acting on an inline element must return either nil, an inline, or a list of inlines, and a function filtering a block element must return one of nil, a block, or a list of block elements. Pandoc will throw an error if this condition is violated.

您希望将输出呈现为块 (<div>abc</div>),但您的输入 (Str) 是内联的。这就是为什么它不起作用。将 Str(内联)更改为 Para(块),将 elem.text 更改为 element.content[1].text,将 RawInline 更改为 RawBlock,它将起作用:

return {
  {
    Para = function (elem)
      if elem.content[1].text == "{{helloworld}}" then
        return pandoc.RawBlock('html','<div>abc</div>')
      else
        return elem
      end
    end,
  }
}