Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandoc: How to link to a section in another markdown file?

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.

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

like image 453
David Roundy Avatar asked Jan 09 '18 14:01

David Roundy


People also ask

How can Pandoc manage Markdown input without creating distinct versions?

It's there some way for pandoc to manage this without creating distinct versions of the markdown input? 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).

Can I use brackets in a shortcut reference link in Pandoc?

So the following is fine in pandoc, though not in most other implementations: In a shortcut reference link, the second pair of brackets may be omitted entirely: To link to another section of the same document, use the automatically generated identifier (see Heading identifiers ). For example: See the [Introduction] (#introduction).

How to add Link part of the same document in Markdown?

How to add link part of the same document in markdown. Sometimes we want to navigate sections or parts of a document on the same webpage. mailto links are used to include a link with an email address. to add email address links with markdown markup, Link contains mailto with colon and email address The syntax

How do I reference a section in Pandoc?

Sign in to your account Pandoc currently generates mangled reference IDs for section headers from their titles using the documented algorithm. In order to reference a section, one has to create an HTML-style cross-link with a target name made using this algorithm. Example: # Section One See [section one] (#section-one).


1 Answers

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.

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

Run it with

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

Run with

pandoc --lua-filter fix-links-single-file.lua *.md -o outfile.html
like image 197
tarleb Avatar answered Sep 26 '22 01:09

tarleb