Enabling True Autolinking in Markdown

How to alter Markdown to enable parsing of plain links

NOTE: This practise is unadvisable. To auto-link a piece of content in PHP or JavaScript, you should run it through Cassis’s auto_link function, after any Markdown processing.

There are all sorts of reasons to not want to have to use a silly syntax for adding simple links — mine was that my content gets distributed out to other services and it’s easier to make autolinking better than to remove the autolink syntax for the syndicated copies.

The fix is fairly simple:

  1. Find the doAutoLinks($text) function (or whatever function processes auto links in your version of Markdown). In PHP Markdown Extra, it’s on line 1443.
  2. Find following regex:

    '{<((https?|ftp|dict):[^'">s]+)>}i'

    This is the one that identifies links specified with the <http://address.tld> syntax.

  3. Add a question mark (e.g. ‘?’) after each of the ‘<’s and ‘>’s.
  4. Ta-daa! You can now use either plain links or surround them with angle brackets and they’ll be converted to <a> tags as usual.

How this works

The regex in markdown searches for a URI surrounded by angle brackets. By adding a question mark after each of the angle brackets you’re specifying that the preceding character should be matched zero or more times — i.e. that it is optional.

Therefore, the following syntaxes now work (and coincidentally are recommended reading on the topics covered in this article):

Markdown Output (live)
<http://daringfireball.net/projects/markdown/ http://daringfireball.net/projects/markdown/
http://michelf.com/projects/php-markdown/extra/> http://michelf.com/projects/php-markdown/extra/
<http://www.zytrax.com/tech/web/regex.htm> http://www.zytrax.com/tech/web/regex.htm
http://www.php.net/manual/en/ref.pcre.php http://www.php.net/manual/en/ref.pcre.php