diff --git a/Parsedown.php b/Parsedown.php index 79f900b..91b28b4 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -81,7 +81,7 @@ class Parsedown { foreach ($matches as $matches) { - $this->reference_map[$matches[1]] = $matches[2]; + $this->reference_map[strtolower($matches[1])] = $matches[2]; $text = str_replace($matches[0], '', $text); } @@ -526,14 +526,20 @@ class Parsedown # Reference(d) Link / Image - if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\][ ]?\[(.+?)\]/', $text, $matches, PREG_SET_ORDER)) + if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\](?:[ ]?\[(.*?)\])?/ms', $text, $matches, PREG_SET_ORDER)) { foreach ($matches as $matches) { - if (array_key_exists($matches[3], $this->reference_map)) + $link_difinition = isset($matches[3]) && $matches[3] + ? $matches[3] + : $matches[2]; # implicit + + $link_difinition = strtolower($link_difinition); + + if (isset($this->reference_map[$link_difinition])) { - $url = $this->reference_map[$matches[3]]; - + $url = $this->reference_map[$link_difinition]; + if ($matches[1]) # image { $element = ''.$matches[2].''; diff --git a/tests/data/reference_link.html b/tests/data/reference_link.html index 84ed9e5..90a907b 100644 --- a/tests/data/reference_link.html +++ b/tests/data/reference_link.html @@ -1,8 +1,13 @@ -

Here's a reference link.

-

Here's one with an alternative syntax.

-

Here's one on the next line.

-

Here's one on 2 lines.

-

Here's one with a different URL.

-

Here's one with a semantic name.

-

Here's [one][404] with no definition.

-

Here's an image: Markdown Logo

\ No newline at end of file +

Here's a reference link.

+

Here's one with an alternative syntax.

+

Here's one on the next line.

+

Here's one on 2 lines.

+

Here's one with a different URL.

+

Here's one with a semantic name.

+

Here's [one][404] with no definition.

+

Here's an image: Markdown Logo

+

Here's an implicit one.

+

Here's an implicit one.

+

Here's an implicit one with an empty link definition.

+

Here's a multiline +one defined on 2 lines.

\ No newline at end of file diff --git a/tests/data/reference_link.md b/tests/data/reference_link.md index e6fb2b3..9b2c620 100644 --- a/tests/data/reference_link.md +++ b/tests/data/reference_link.md @@ -1,29 +1,40 @@ -Here's a [reference link][1]. - -[1]: http://parsedown.org - -Here's [one] [2] with an alternative syntax. - -[2] :http://parsedown.org - -Here's [one][3] on the next line. -[3]: http://parsedown.org - -Here's [one][4] on 2 lines. - -[4]: -http://parsedown.org - -Here's [one][5] with a different URL. - -[5]: http://parsedown.org/tests/ - -Here's [one][the website] with a semantic name. - -[the website]: http://parsedown.org - -Here's [one][404] with no definition. - -Here's an image: ![Markdown Logo][image] - -[image]: https://raw.github.com/dcurtis/markdown-mark/master/png/32x20-solid.png \ No newline at end of file +Here's a [reference link][1]. + +[1]: http://parsedown.org + +Here's [one] [2] with an alternative syntax. + +[2] :http://parsedown.org + +Here's [one][3] on the next line. +[3]: http://parsedown.org + +Here's [one][4] on 2 lines. + +[4]: +http://parsedown.org + +Here's [one][5] with a different URL. + +[5]: http://parsedown.org/tests/ + +Here's [one][website] with a semantic name. + +[website]: http://parsedown.org + +Here's [one][404] with no definition. + +Here's an image: ![Markdown Logo][image] + +[image]: https://raw.github.com/dcurtis/markdown-mark/master/png/32x20-solid.png + +Here's an [implicit one]. + +Here's an [implicit one]. + +[implicit one]: http://google.com + +Here's an [implicit one][] with an empty link definition. + +Here's a [multiline +one][website] defined on 2 lines. \ No newline at end of file