Python-Markdown / markdown

A Python implementation of John Gruber’s Markdown with Extension support.
https://python-markdown.github.io/
BSD 3-Clause "New" or "Revised" License
3.74k stars 858 forks source link

How to use target="_blank" in link item #1341

Closed VanilleAngeles closed 1 year ago

VanilleAngeles commented 1 year ago

With Pandoc shell command line no problem. With markdown python program it's impossible. I use several tests without success. A solution ?

Markdown syntaxe: [Markdown Python](https://github.com/Python-Markdown/markdown/issues){:target="_blank"}

Thanks

waylan commented 1 year ago

Could you please provide us with the options you are passing to Python-Markdown (including any extensions you are using) as well as the actual output and the output you are expecting?

VanilleAngeles commented 1 year ago

Good evening The python file with open (file, 'r') as f: Html = markdown.markdown(f.read()) I gave the markdown syntax previously. Is it enough for you ? Thanks

waylan commented 1 year ago

As a reminder, the {:target="_blank"} syntax is not part of Markdown's standard syntax. Therefore, we do not support it by default. However, with the attr_list extension we do provide support. If you enable the extension, then your input would presumably work as desired. I say "presumably" because you did not provide me with an example of the output you expect. Therefore, I can only assume the extension will match your expectations. If it doesn't then you may want to explore other third-party extensions and/or build your own extension.

VanilleAngeles commented 1 year ago

Thank you for your explanation. The extension will apply target to ALL links; it's not what I want. I will use a python instruction. On MD link line ( if I want open the link on a new page) I will add {:target="_blank"} at the end of the address [Markdown Python](https://github.com/Python-Markdown/markdown/issues {:target="_blank"}) and after convertinf md to html the python instruction FinalHtml = FinalHtml.replace('{:target="_blank"}">','" target="_blank">')

Thank you

waylan commented 1 year ago

I'm confused as to what you want. Isn't this the desired behavior?

>>> import markdown
>>> src = '''
... [blank](http://example.com){:target="_blank"}
... [no blank](http://example.com/no_blank)
... '''
>>> print(markdown.markdown(src, extensions=['attr_list']))
<p><a href="http://example.com" target="_blank">blank</a>
<a href="http://example.com/no_blank">no blank</a></p>
VanilleAngeles commented 1 year ago

Exactly what I want. Thanks very much from France.