3

I use Markdown and Pandoc extensively. However, I would like to generate a PDF with embedded links (like usual), but in the event the document is printed, I'd like to also include a table of links at the end of the document. Is there a way to do this automatically?

Ex.

Title
-----


[Python][] is cool!

...

## Links ##
[Python]: http://python.org
[Pip]: https://pip.readthedocs.org

where I would actually get an extra page in my PDF with something like

Python: http://python.org
Pip: https://pip.readthedocs.org

Thanks!

1 Answer 1

4

This is something that is easy to achieve with filters.

Here is linkTable.hs. A filter which adds a table of links to the end of your document.

import Text.Pandoc.JSON
import Text.Pandoc.Walk
import Data.Monoid

main :: IO ()
main = toJSONFilter appendLinkTable

appendLinkTable :: Pandoc -> Pandoc
appendLinkTable (Pandoc m bs) = Pandoc m (bs ++ linkTable bs)

linkTable :: [Block] -> [Block]
linkTable p = [Header 2 ("linkTable", [], []) [Str "Links"] , Para links]
  where
    links = concatMap makeRow $ query getLink p
    getLink (Link txt (url, _)) = [(url,txt)]
    getLink _ = []
    makeRow (url, txt) = txt ++ [Str ":", Space, Link [Str url] (url, ""), LineBreak]

Compile the filter with ghc linkTable.hs. The output is as follows.

> ghc linkTable.hs 
[1 of 1] Compiling Main             ( linkTable.hs, linkTable.o )
Linking linkTable ...

> cat example.md 
Title
-----


[Python][] is cool!

[Pip] is a package manager.

...

[Python]: http://python.org
[Pip]: https://pip.readthedocs.org

Then running pandoc with the filter.

> pandoc -t markdown --filter=./linkTable example.md
Title
-----

[Python](http://python.org) is cool!

[Pip](https://pip.readthedocs.org) is a package manager.

...

Links {#linkTable}
-----

Python: <http://python.org>\
Pip: <https://pip.readthedocs.org>\
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.