Skip to content

Commit

Permalink
Adjusted reference link parsing to work more similar to inline links
Browse files Browse the repository at this point in the history
Single and double quotes are now allowed in both forms, provided
they are not preceeded by a space (they were already allowed in
inline links although this was not documented). The syntax
document has been adjusted accordingly.

Additional, the syntax document has been adjusted for reference
links to mention that the spaces/tabs between link ID in brackets
and the URL are optional.

Fixes #239
  • Loading branch information
gettalong committed Apr 26, 2015
1 parent 2008b24 commit ec8ca61
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
11 changes: 5 additions & 6 deletions doc/syntax.page
Original file line number Diff line number Diff line change
Expand Up @@ -1150,9 +1150,8 @@ Notes:
However, if you use square brackets within the link text, you have to either properly nest them or
to escape them. It is not possible to create nested links!

* The link URL must not contain single or double quotes and it has to contain properly nested
parentheses if no title is specified, or the link URL must be contained in angle brackets
(incorrectly nested parentheses are allowed).
* The link URL has to contain properly nested parentheses if no title is specified, or the link URL
must be contained in angle brackets (incorrectly nested parentheses are allowed).

* The link title may not contain its delimiters and may not be empty.

Expand Down Expand Up @@ -1194,9 +1193,9 @@ definition looks like this:
The link definition has the following structure:

* The link identifier in square brackets, optionally indented up to three spaces,
* then a colon and one or more spaces/tabs,
* then the link URL which must not contain any single or double quotes and which must contain at
least one non-space character, or a left angle bracket, the link URL and a right angle bracket,
* then a colon and one or more optional spaces/tabs,
* then the link URL which must contain at least one non-space character, or a left angle bracket,
the link URL and a right angle bracket,
* then optionally the title in single or double quotes, separated from the link URL by one or more
spaces or on the next line by itself indented any number of spaces/tabs.

Expand Down
3 changes: 2 additions & 1 deletion lib/kramdown/parser/kramdown/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def normalize_link_id(id)
id.gsub(/[\s]+/, ' ').downcase
end

LINK_DEFINITION_START = /^#{OPT_SPACE}\[([^\n\]]+)\]:[ \t]*(?:<(.*?)>|([^'"\n]*?\S[^'"\n]*?))[ \t]*?(?:\n?[ \t]*?(["'])(.+?)\4[ \t]*?)?\n/
LINK_DEFINITION_START = /^#{OPT_SPACE}\[([^\n\]]+)\]:[ \t]*(?:<(.*?)>|([^\n]*?\S[^\n]*?))(?:(?:[ \t]*?\n|[ \t]+?)[ \t]*?(["'])(.+?)\4)?[ \t]*?\n/

# Parse the link definition at the current location.
def parse_link_definition
return false if @src[3].to_s =~ /[ \t]+["']/
@src.pos += @src.matched_size
link_id, link_url, link_title = normalize_link_id(@src[1]), @src[2] || @src[3], @src[5]
warning("Duplicate link ID '#{link_id}' on line #{@src.current_line_number} - overwriting") if @link_defs[link_id]
Expand Down
2 changes: 1 addition & 1 deletion test/testcases/span/01_link/link_defs.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

<p>Points to <a href="one.url">1</a> and <a href="two.url">2</a> and <a href="three.url">3</a> but not [4]</p>

<p>Points to <a href="http://example.com">_.:,;!?-</a></p>
<p>Points to <a href="http://example.com">_.:,;!?-</a> and <a href="test.html#'test'" title="title">otherid8</a></p>
3 changes: 2 additions & 1 deletion test/testcases/span/01_link/link_defs.text
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This is a para.
[otherid6]: some spaces.html 'title'
[otherid7]: some spaces
"title"
[otherid8]:test.html#'test' 'title'

[break]: http://www.example.com/test/asdf.html
'Another title'
Expand All @@ -23,4 +24,4 @@ Points to [1] and [2] and [3] but not [4]

[_.:,;!?-]: http://example.com

Points to [_.:,;!?-]
Points to [_.:,;!?-] and [otherid8]

0 comments on commit ec8ca61

Please sign in to comment.