删除 Markdown 链接,但保留链接文本和方括号以使用 Lua 过滤器进行正常引用

问题描述 投票:0回答:1

我想删除所有降价链接,但使用 Lua 过滤器保留链接文本和方括号。比如原来的内容是这样的:

[@a-local-file, page 15](x-devonthink-item://742BD8FE-B962-422F-98C1-B1K4DQA5A117?page=15)

我想将其转换为:

[@a-local-file, page 15]

我尝试为这个转换写一个 Lua 过滤器:

function Link(el)
    if el.target:find("^x%-devonthink%-item://") then
        return el.content
    end
end

然而,使用这个 Lua 过滤器,它只返回链接文本:

@a-local-file, page 15

有一个related question但是我的问题的答案不是很直接。因为我的目的是使用

[@a-local-file, page 15]
NormalCitation
。但如果一对 添加了方括号,它会变成
AuthorInText
, 这是不可取的。

如何修改代码以保留 Pandoc 的正常引用 的链接文本和方括号?提前致谢!

lua markdown pandoc pandoc-citeproc
1个回答
1
投票

一个简单的技巧是将字符串包装到表中,因为 pandoc 会将它们视为

pandoc.Inlines
项目并允许将它们连接到
link.content
,这也是
pandoc.Inlines
类型。

function Link (link)
  if link.target:match '^x%-devonthink%-item://' then
    return {'['} .. link.content .. {']'}
  end
end

Pandoc 会把它当作我们写过的

    return
        pandoc.Inlines{pandoc.Str '['} ..
        link.content ..
        pandoc.Inlines{pandoc.Str ']'}

另一种方法是使用

pandoc.utils.stringify
,它使我们能够使用普通的字符串函数,但也会从链接文本中删除所有标记:

function Link (link)
  if link.target:match '^x%-devonthink%-item://' then
    return string.format('[%s]', pandoc.utils.stringify(link.content))
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.