移除Markdown链接但保留链接文本和方括号以进行常规引用,使用Lua筛选器。

huangapple go评论65阅读模式
英文:

Remove Markdown links but keep link text and square brackets for normal citation using Lua filter

问题

我想删除所有的Markdown链接,但保留链接文本和方括号,使用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

有一个[相关的问题],但答案对我的问题不够直接。因为我的目的是将[@a-local-file, page 15]用作NormalCitation。但如果添加一对方括号,它将被更改为AuthorInText,这是不希望的。

如何修改代码以保留Pandoc的普通引用中的链接文本和方括号?提前感谢!

英文:

I’d like to remove all markdown links but keep the link text and square bracket using Lua filter. For example, the original content is like:

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

And I’d like to convert it to:

[@a-local-file, page 15]

I’ve tried to write a Lua filter for this conversion:

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

However, with this Lua filter, it returned only the link text:

@a-local-file, page 15

There is a related question but the answer is not very straightforward to my question. Because my purpose is to use
[@a-local-file, page 15] as NormalCitation. But if a pair of
square brackets is added, it will be changed as AuthorInText,
which is undesirable.

How to modify the code to keep both link text and square brackets for Pandoc’s normal citations? Thanks in advance!

答案1

得分: 2

以下是您要翻译的内容:

A simple trick is to wrap strings into tables, as pandoc will then treat them as pandoc.Inlines items and allow to concatenate them to the link.content, which is also of type pandoc.Inlines.

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

Pandoc will treat this as if we had written

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

Now, some formats use square brackets as part of their markup, and pandoc will escape them in that case. If the goal is to have unescaped brackets while round-tripping to Markdown, then we'll need to use RawInline elements instead of Str:

    return raw'[' .. link.content .. raw']';

where raw is defined as

local function raw (s)
  return {pandoc.RawInline('markdown', s)}
end

The content of RawInline elements is not escaped (if the raw format matches the target format).


Citations

The updated question mentions the desire to have this treated as a normal citation. This isn't quite as straight forward, because citation parsing isn't simple. What we can do, however, is create a Markdown string that looks the way we'd want it to, and then use pandoc.read to parse it into an AST:

    local linkstring = pandoc.utils.stringify(link.contents)
    -- alternative:
    -- local placeholder = pandoc.Pandoc{pandoc.Plain(link.contents)}
    -- local linkstring = pandoc.write(placeholder, 'markdown')
    local citationmd = string.format('[%s]', linkstring)
    return pandoc.utils.blocks_to_inlines(
      pandoc.read(citationmd, 'markdown').blocks
    )

Make sure that the filter runs before citeproc.

英文:

A simple trick is to wrap strings into tables, as pandoc will then treat them as pandoc.Inlines items and allow to concatenate them to the link.content, which is also of type pandoc.Inlines.

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

Pandoc will treat this as if we had written

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

Now, some formats use square brackets as part of their markup, and pandoc will escape them in that case. If the goal is to have unescaped brackets while round-tripping to Markdown, then we'll need to use RawInline elements instead of Str:

    return raw'[' .. link.content .. raw']'

where raw is defined as

local function raw (s)
  return {pandoc.RawInline('markdown', s)}
end

The content of RawInline elements is not escaped (if the raw format matches the target format).


Citations

The updated question mentions the desire to have this treated as a normal citation. This isn't quite as straight forward, because citation parsing isn't simple. What we can do, however, is create a Markdown string that looks the way we'd want it to, and then use pandoc.read to parse it into an AST:

    local linkstring = pandoc.utils.stringify(link.contents)
    -- alternative:
    -- local placeholder = pandoc.Pandoc{pandoc.Plain(link.contents)}
    -- local linkstring = pandoc.write(placeholder, 'markdown')
    local citationmd = string.format('[%s]', linkstring)
    return pandoc.utils.blocks_to_inlines(
      pandoc.read(citationmd, 'markdown').blocks
    )

Make sure that the filter runs before citeproc.

huangapple
  • 本文由 发表于 2023年2月24日 17:10:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定