diff --git a/Markdown.pl b/Markdown.pl index 73cbc8a..2bb9bf4 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -846,7 +846,7 @@ sub _DoAnchors { }xsge; # - # Finally, inline-style links: [link text](url "optional title") + # Subsequently, inline-style links: [link text](url "optional title") # $text =~ s{ ( # wrap whole match in $1 @@ -888,6 +888,41 @@ sub _DoAnchors { $result; }xsge; + # + # Finally, handle reference-style implicit shortcut links: [link text] + # + $text =~ s{ + ( # wrap whole match in $1 + \[ + ($g_nested_brackets) # link text = $2 + \] + ) + }{ + my $result; + my $whole_match = $1; + my $link_text = $2; + my $link_id = lc $2; + + if (defined($g_urls{$link_id}) || defined($g_anchors{$link_id})) { + my $url = $g_urls{$link_id}; + $url = defined($url) ? _PrefixURL($url) : $g_anchors{$link_id}; + # We've got to encode these to avoid conflicting + # with italics, bold and strike through. + $url =~ s!([*_~])!$g_escape_table{$1}!g; + $result = "`) may be used for this purpose provided it is preceded and followed by at least one blank line. @@ -775,11 +775,15 @@ are equivalent. The *implicit link name* shortcut allows you to omit the name of the link, in which case the link text itself is used as the name. -Just use an empty set of square brackets -- e.g., to link the word -"Google" to the google.com web site, you could simply write: +Just use an empty set of square brackets (or none) -- e.g., to link the +word "Google" to the google.com web site, you could simply write: [Google][] +Or even just this: + + [Google] + And then define the link: [Google]: http://google.com/ @@ -787,12 +791,18 @@ And then define the link: Because link names may contain spaces, this shortcut even works for multiple words in the link text: - Visit [Daring Fireball][] for more information. + Visit [Daring Fireball] for more information. And then define the link: [Daring Fireball]: http://daringfireball.net/ +Text inside square brackets is left completely unchanged (including the +surrounding brackets) _unless_ it matches a link definition. Furthermore, +the single pair of surrounding square brackets case is always checked +for last so you may only omit the trailing `[]` of an *implicit link name* +shortcut when the result would still be unambiguous. + Link definitions can be placed anywhere in your Markdown document. I tend to put them immediately after each paragraph in which they're used, but if you want, you can put them all at the end of your @@ -817,8 +827,8 @@ Here's an example of reference links in action: Using the implicit link name shortcut, you could instead write: - I get 10 times more traffic from [Google][] than from - [Yahoo][] or [MSN][]. + I get 10 times more traffic from [Google] than from + [Yahoo] or [MSN]. [google]: http://google.com/ "Google" [yahoo]: http://search.yahoo.com/ "Yahoo Search"