Convert markdown links to html with Pandoc

P.Tail picture P.Tail · Dec 6, 2016 · Viewed 8.4k times · Source

In my new project I have multiple markdown files which are linked to each other. These links refer to the original .md files.

Example: File README.md

...
1. [Development documentation](Development.md)
1. [User documentation](Usage.md)
...

If I convert these files with Pandoc, e.g. to html files, all links are still pointing to the original .md file. What I'm looking for is a way to convert also the link type, which means that output files should refer to the output file type such as HTML, PDF, TeX etc. Is there a way to convert the internal link type with Pandoc?

I use this to convert the files:

pandoc -f markdown -t html5 input.md -o output.html

Answer

JW. picture JW. · Mar 21, 2018

Example with the built-in Lua filters:

# links-to-html.lua
function Link(el)
  el.target = string.gsub(el.target, "%.md", ".html")
  return el
end

Then:

pandoc -f markdown -t html5 input.md -o output.html --lua-filter=links-to-html.lua