Markdown is a great tool for formatting plain text into pretty html, but it doesn't turn plain-text links into URLs automatically. Like this one:
http://www.google.com/
How do I get markdown to add tags to URLs when I format a block of text?
Markdown is a great tool for formatting plain text into pretty html, but it doesn't turn plain-text links into URLs automatically. Like this one:
http://www.google.com/
How do I get markdown to add tags to URLs when I format a block of text?
You could write an extension to markdown. Save this code as mdx_autolink.py
import markdown
from markdown.inlinepatterns import PatternEXTRA_AUTOLINK_RE = r'(?<!"|>)((https?://|www)[-\w./#?%=&]+)'class AutoLinkPattern(Pattern):def handleMatch(self, m):el = markdown.etree.Element('a')if m.group(2).startswith('http'):href = m.group(2)else:href = 'http://%s' % m.group(2)el.set('href', href)el.text = m.group(2)return elclass AutoLinkExtension(markdown.Extension):"""There's already an inline pattern called autolink which handles <http://www.google.com> type links. So lets call this extra_autolink """def extendMarkdown(self, md, md_globals):md.inlinePatterns.add('extra_autolink', AutoLinkPattern(EXTRA_AUTOLINK_RE, self), '<automail')def makeExtension(configs=[]):return AutoLinkExtension(configs=configs)
Then use it in your template like this:
{% load markdown %}(( content|markdown:'autolink'))
Update:
I've found an issue with this solution: When markdown's standard link syntax is used and the displayed portion matches the regular expression, eg:
[www.google.com](http://www.yahoo.co.uk)
strangely becomes:www.google.com
But who'd want to do that anyway?!