Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Autolink with markdown?

judgejjudgej
edited April 2011 in Vanilla 2.0 - 2.8
I have markdown set as the default format for posts on my site. When a URL is added to a post, it does not get converted into a link. Is that expected for the markdown formatting?

Users get very confused with the markdown format for links and just want to post a URL (having to give it a label and special brackets is one step too far - people just want to throw stuff at the forum and not have to learn its intricacies for formatting).

So - are autolinks supported in Vanilla with markdown set as the default format? If not, does anyone have any hints on how to enable it or add it, perhaps something that can be made into a plugin?

Comments

  • judgejjudgej
    edited April 2011
    Never mind. I have added a filter to the mardown() function (third party libraries) to pre-process its input, and convert any URL that is *not* in markdown syntax to an appropriate markdown tag.

    The URLs that I convert are any that are not preceded by a ", <, [ or (, plus optional white space.

    It works okay as a hack, but it would be nice to see a hook in place in the format class (library/core/class.format.php - which I have had to hack anyway to increase tge username length to 40 characters), so I could pre-process the comments and discussions text in a plugin.
  • Can you describe how you did this? This is one of those useful hacks that I want to implement too.
  • judgejjudgej
    edited April 2011
    The change was in library/vendors/markdown.php

    First I added a function to do the pre-processing:
    // 2010-04-05 JDJ
    function custom_markdown_autolinks($text)
    {
    $url_regex = '(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}'
    . '(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'\/\\\+&%\$#\=~])*[^\.\,\)\(\s]';
    // URL must not be inside markdown tags already.
    // If a URL has a ", <, ( or [ in front, then don't match it.
    return preg_replace(
    '/' . '(^|[^\[\(<"]\s*)' . '(' . $url_regex . ')' . '/',
    '$1[$2]($2)',
    $text
    );
    }
    Then right at the end of the Markdown() function, change the return value to this:
    	# Transform text using parser.
    return $parser->transform(custom_markdown_autolinks($text));
    I will probably add something for emails too, but that is not in there yet. I'm sure the regex could do with some refinement - the "&amp;" in it does not look right, but the concept and so far in practice, it works at least. I got the regex from an RE site, and this one looked better than most, but I have not tested it thoroughly.

    This converts URL into [URL](URL) which becomes a linked URL after it passes through the markdown filter. Being cleverer, the label part [in square brackets] could be shortened to just the domain and an abbreviation of the path.

    In theory even <URL> should be a clickable link, but it does not seem to be working properly (it munges part of the URL) and even this is one step too far for most users to have to learn and get to grips with, even if it did work.
Sign In or Register to comment.