From 18b456b8b4162cd2b55ef890b4c76ec254e73a98 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Thu, 19 Jan 2017 01:24:34 -0800 Subject: [PATCH] Markdown: allow trailing '[]' to be omitted When using the "implicit link name" shortcut (i.e. the link name is the same as the link text), the trailing '[]' is unsightly. Allow the trailing '[]' to be omitted when the omission is unambiguous. In other words, if there is no preceding or following pair of square brackets the trailing '[]' can safely be omitted. For example: See any of [link 1][] [link 2][] [lnik 3][]. The trailing '[]' MUST NOT be omitted in this case because the result: See any of [link 1] [link 2] [link 3] would be misinterpreted. But, if they're separated with commas or words instead like so: See any of [link 1], [link 2] or [link 3]. then they cannot be misinterpreted and the trailing '[]' can be safely omitted making for a much nicer looking document. To go with this change the basics.md and syntax.md documents have been modified to take advantage of these new semantics. Signed-off-by: Kyle J. McKay --- Markdown.pl | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++- basics.md | 4 +-- syntax.md | 70 ++++++++++++++++++++++++++++---------------------- 3 files changed, 115 insertions(+), 33 deletions(-) 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"