I am trying to use pandoc
(with hakyll
, but this is probably not important) to achieve the following:
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 Inline
s, 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
Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.
Pandoc – 🐍 Python LibraryIt can be used to analyze, create and transform documents, in Python : >>> import pandoc >>> text = "Hello world!" >>> doc = pandoc.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With