1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Added ability to specify link titles to inline and reference links.

Fixed whitespace bug with reference link regex.
Updated tests.
This commit is contained in:
malorisdead 2013-12-14 02:13:53 -05:00
parent 51a08fad85
commit 019a4af2af
5 changed files with 34 additions and 7 deletions

View File

@ -399,11 +399,16 @@ class Parsedown
# reference # reference
if (preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $deindented_line, $matches)) if (preg_match('/^\[(.+?)\]:\s*([^\s]+)(?:\s+["\'\(](.+)["\'\)])?/', $deindented_line, $matches))
{ {
$label = strtolower($matches[1]); $label = strtolower($matches[1]);
$this->reference_map[$label] = trim($matches[2], '<>');; $this->reference_map[$label] = trim($matches[2], '<>');
if (isset($matches[3]))
{
$this->reference_map[$label.":title"] = $matches[3];
}
continue 2; continue 2;
} }
@ -631,7 +636,7 @@ class Parsedown
# inline link / inline image (recursive) # inline link / inline image (recursive)
if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)\)/', $text, $matches, PREG_SET_ORDER)) if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)(?:\s+["\'\(](.*?)["\'\)])?\)/', $text, $matches, PREG_SET_ORDER))
{ {
foreach ($matches as $matches) foreach ($matches as $matches)
{ {
@ -647,8 +652,15 @@ class Parsedown
{ {
$element_text = $this->parse_span_elements($matches[3]); $element_text = $this->parse_span_elements($matches[3]);
if (isset($matches[5]))
{
$element = '<a href="'.$url.'" title="'.$matches[5].'">'.$element_text.'</a>';
}
else
{
$element = '<a href="'.$url.'">'.$element_text.'</a>'; $element = '<a href="'.$url.'">'.$element_text.'</a>';
} }
}
# ~ # ~
@ -688,8 +700,15 @@ class Parsedown
{ {
$element_text = $this->parse_span_elements($matches[2]); $element_text = $this->parse_span_elements($matches[2]);
if (isset($this->reference_map[$link_definition.":title"]))
{
$element = '<a href="'.$url.'" title="'.$this->reference_map[$link_definition.":title"].'">'.$element_text.'</a>';
}
else
{
$element = '<a href="'.$url.'">'.$element_text.'</a>'; $element = '<a href="'.$url.'">'.$element_text.'</a>';
} }
}
# ~ # ~

View File

@ -1,2 +1,3 @@
<p>an <a href="http://example.com">implicit</a> reference link</p> <p>an <a href="http://example.com">implicit</a> reference link</p>
<p>an <a href="http://example.com">implicit</a> reference link with an empty link definition</p> <p>an <a href="http://example.com">implicit</a> reference link with an empty link definition</p>
<p>an <a href="http://example.com" title="Example">explicit</a> reference link with a title</p>

View File

@ -3,3 +3,7 @@ an [implicit] reference link
[implicit]: http://example.com [implicit]: http://example.com
an [implicit][] reference link with an empty link definition an [implicit][] reference link with an empty link definition
an [explicit][example] reference link with a title
[example]: http://example.com "Example"

View File

@ -1,3 +1,4 @@
<p><a href="http://example.com">link</a></p> <p><a href="http://example.com">link</a></p>
<p><a href="http://example.com"><code>link</code></a></p> <p><a href="http://example.com"><code>link</code></a></p>
<p><a href="http://example.com" title="Example">link with title</a></p>
<p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png"></a></p> <p><a href="http://example.com"><img alt="MD Logo" src="http://parsedown.org/md.png"></a></p>

View File

@ -2,4 +2,6 @@
[`link`](http://example.com) [`link`](http://example.com)
[link with title](http://example.com "Example")
[![MD Logo](http://parsedown.org/md.png)](http://example.com) [![MD Logo](http://parsedown.org/md.png)](http://example.com)