Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing attributes for links and other types of elements in pandoc

Tags:

haskell

pandoc

I am trying to use pandoc (with hakyll, but this is probably not important) to achieve the following:

  1. Read a markdown file.
  2. Convert it to HTML.
  3. Add a target="_blank" attribute to every link that is automatically produced.

The problem is that the definition of Inline in pandoc seems to support attributes only for some types of Inlines, e.g., for Span and Code, and not for others, e.g., for Link and Image.

Is this an arbitrary limitation of pandoc, or was it made on purpose for some reason that I do not understand? Furthermore, is there a way to achieve what I want, without resorting to low-level string processing or to using the RawInline constructor?


Here is a MWE:

import Text.Pandoc
import Text.Pandoc.Walk

fixLinks :: Pandoc -> Pandoc
fixLinks = walk fixLink
  where fixLink (Link inlines (url, title)) =
          Link inlines (url, "I want to add a target=_blank to this link!")
        fixLink inline = inline

main = do
  let md = "This is a link to [StackOverflow](http://stackoverflow.com/)."
  (putStrLn . writeHtmlString def . fixLinks . readMarkdown def) md
like image 320
nickie Avatar asked Apr 26 '15 09:04

nickie


People also ask

Can pandoc convert HTML to markdown?

Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.

What is pandoc Python?

Pandoc – 🐍 Python LibraryIt can be used to analyze, create and transform documents, in Python : >>> import pandoc >>> text = "Hello world!" >>> doc = pandoc.


1 Answers

Image and link attributes are documented as a feature 'not in Pandoc' on the Pandoc vs Multimarkdown Wiki page.

There are open issues around this, like Permit adding attributes to all Markdown elements #684. I didn't see attached code/pull requests to implement them when I looked.

If you want to add a target="_blank" attribute to auto-generated links, I think you'll need to use a workaround in the meantime. (And note that you might not need to use raw string processing if you post-process the HTML with a non-Pandoc HTML parser.)

like image 143
Will Angley Avatar answered Oct 27 '22 00:10

Will Angley