gpt4 book ai didi

lua - Pandoc:Lua 过滤器,用于用
abc
替换 {{helloworld}}

转载 作者:行者123 更新时间:2023-12-04 01:32:53 26 4
gpt4 key购买 nike

manual我找到了一个 pandoc lua 过滤器的例子:

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

我要换 {{helloworld}}<div>abc</div> .我的尝试:
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 文件如下所示:

enter image description here

最佳答案

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.textelement.content[1].textRawInlineRawBlock它会起作用:

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

关于lua - Pandoc:Lua 过滤器,用于用 <div>abc</div> 替换 {{helloworld}},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60525983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com