pandoc:如何链接到另一个Markdown文件中的某个部分? [英] pandoc: How to link to a section in another markdown file?

查看:196
本文介绍了pandoc:如何链接到另一个Markdown文件中的某个部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建两个markdown文件,它们之间有链接.这里的挑战是,我是否希望Pandic将它们连接到单个HTML文件或分离HTML文件,以使文件正常工作.问题在于,在后一种情况下,链接需要知道其他HTML文件的名称才能正常工作.

I would like to create two markdown files with links between their sections. The challenge here it's that I want the files to work correctly whether I ask pandic to concatenate them to a single HTML file, or to separate HTML files. The trouble is that in the latter case the link needs to know there name of the other HTML file in order to work properly.

pandoc是否有某种方法可以在不创建降价输入的不同版本的情况下进行管理?

It's there some way for pandoc to manage this without creating distinct versions of the markdown input?

推荐答案

以下使用 lua过滤器修复您的链接.假定通过在链接之前添加定义链接的文件(例如[see here](some-other-file.md#topic))来编写链接.一些编辑器使切换到相应文件的操作变得简单,因此这可能是另一个优点.

The following uses lua filters to fix your links. It assumes that links are written by prefixing them with the file in which the link is defined, for example [see here](some-other-file.md#topic). Some editors make it simple to switch to the respective file, so this can be an additional advantage.

转换为多个HTML文件时,我们要做的就是用.html替换这些链接中的.md文件扩展名.

When converting to multiple HTML files, all we need to do is replace the .md file extension in these links with .html.

-- fix-links-multiple-files.lua
function Link (link)
  link.target = link.target:gsub('(.+)%.md%#(.+)', '%1.html#%2')
  return link
end

使用

pandoc --lua-filter fix-links-multiple-files.lua file-1.md -o file-1.html

对于单个文件,我们只需将链接的文件部分删除:

In the case of a single file, we can just drop the file part of the link:

-- fix-links-single-file.lua
function Link (link)
  link.target = link.target:gsub('.+%.md%#(.+)', '#%1')
  return link
end

运行方式

pandoc --lua-filter fix-links-single-file.lua *.md -o outfile.html

这篇关于pandoc:如何链接到另一个Markdown文件中的某个部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆