Search code examples
htmlhyperlinkmarkdownpandoc

Convert Markdown links to HTML with Pandoc


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. I'm looking for a way to also convert 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

Solution

  • 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)