Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert markdown links to html with Pandoc

Tags:

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 
like image 753
P.Tail Avatar asked Dec 06 '16 10:12

P.Tail


People also ask

How do I convert Markdown to HTML?

To convert Markdown to HTML using Typora, click File —> Export —> HTML.

Can pandoc convert PDF to HTML?

You can use the program pandoc on the SCF Linux and Mac machines (via the terminal window) to convert from formats such as HTML, LaTeX and Markdown to formats such as HTML, LaTeX, Word, OpenOffice, and PDF, among others.

How do I convert Markdown to PDF with pandoc?

There are actually two steps involved in converting a Markdown file to a PDF file: The Markdown source file is converted to a LaTeX source file. Pandoc invokes the pdflatex or xelatex or other TeX command and converts the . tex source file to a PDF file.


2 Answers

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 
like image 200
JW. Avatar answered Sep 19 '22 07:09

JW.


You can create a filter that checks every link element and--if the url ends with .md--replaces it with .html.

Example with Python, using the panflute package:

import panflute as pf  def action(elem, doc):     if isinstance(elem, pf.Link) and elem.url.endswith('.md'):         elem.url = elem.url[:-3] + '.html'         return elem  if __name__ == '__main__':     pf.run_filter(action) 
like image 39
Sergio Correia Avatar answered Sep 21 '22 07:09

Sergio Correia