Skip to content

Commit

Permalink
Allow to display link reference in paragraph, without footnotes
Browse files Browse the repository at this point in the history
  • Loading branch information
tzi committed Mar 16, 2014
1 parent 5235b9f commit 9eacd79
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/Markdownify/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,24 @@ protected function flushFootnotes()
} else {
$this->out("\n", true);
}
$this->out(' [' . $tag['linkID'] . ']: ' . $tag['href'] . (isset($tag['title']) ? ' "' . $tag['title'] . '"' : ''), true);
$this->out(' [' . $tag['linkID'] . ']: ' . $this->getLinkReference($tag), true);
$tag['unstacked'] = true;
$this->footnotes[$k] = $tag;
}
}
}

/**
* return formated link reference
*
* @param array $tag
* @return string link reference
*/
protected function getLinkReference($tag)
{
return $tag['href'] . (isset($tag['title']) ? ' "' . $tag['title'] . '"' : '');
}

/**
* flush enqued linebreaks
*
Expand Down Expand Up @@ -769,6 +780,11 @@ protected function handleTag_a_converter($tag, $buffer)
# [1]: mailto:[email protected] Title
$tag['href'] = 'mailto:' . $bufferDecoded;
}

if ($this->linkPosition == self::LINK_IN_PARAGRAPH) {
return '[' . $buffer . '](' . $this->getLinkReference($tag) . ')';
}

# [This link][id]
foreach ($this->footnotes as $tag2) {
if ($tag2['href'] == $tag['href'] && $tag2['title'] === $tag['title']) {
Expand All @@ -780,7 +796,6 @@ protected function handleTag_a_converter($tag, $buffer)
$tag['linkID'] = count($this->footnotes) + 1;
array_push($this->footnotes, $tag);
}
//var_dump($tag);

return '[' . $buffer . '][' . $tag['linkID'] . ']';
}
Expand Down Expand Up @@ -810,7 +825,7 @@ protected function handleTag_img()

if (empty($this->parser->tagAttributes['src'])) {
# support for "empty" images... dunno if this is really needed
# but there are some testcases which do that...
# but there are some test cases which do that...
if (!empty($this->parser->tagAttributes['title'])) {
$this->parser->tagAttributes['title'] = ' ' . $this->parser->tagAttributes['title'] . ' ';
}
Expand All @@ -821,7 +836,18 @@ protected function handleTag_img()
$this->parser->tagAttributes['src'] = $this->decode($this->parser->tagAttributes['src']);
}

# [This link][id]
$out = '![' . $this->parser->tagAttributes['alt'] . ']';
if ($this->linkPosition == self::LINK_IN_PARAGRAPH) {
$out .= '(' . $this->parser->tagAttributes['src'];
if ($this->parser->tagAttributes['title']) {
$out .= ' "' . $this->parser->tagAttributes['title'] . '"';
}
$out .= ')';
$this->out($out, true);
return ;
}

# ![This image][id]
$link_id = false;
if (!empty($this->footnotes)) {
foreach ($this->footnotes as $tag) {
Expand All @@ -842,8 +868,9 @@ protected function handleTag_img()
);
array_push($this->footnotes, $tag);
}

$this->out('![' . $this->parser->tagAttributes['alt'] . '][' . $link_id . ']', true);
$out .= '[' . $link_id . ']';

$this->out($out, true);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/Test/Markdownify/ConverterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ public function providerLinkConversion()
[2]: http://example2.com/ "Title"';
$data['url-multiple-2']['linkPosition'] = Converter::LINK_AFTER_PARAGRAPH;

// Multiple paragraph link
$data['url-multiple-2']['html'] =
'<p>This is <a href="http://example1.com/" title="Title">an example</a> inline link.</p>
<p>This is <a href="http://example2.com/" title="Title">another example</a> inline link.</p>';
$data['url-multiple-2']['md'] = 'This is [an example](http://example1.com/ "Title") inline link.
This is [another example](http://example2.com/ "Title") inline link.';
$data['url-multiple-2']['linkPosition'] = Converter::LINK_IN_PARAGRAPH;

// Direct link
$data['url-direct']['html'] = '<p><a href="http://example.com">http://example.com</a>.</p>';
$data['url-direct']['md'] = '<http://example.com>.';
Expand Down Expand Up @@ -290,12 +299,22 @@ public function providerLinkConversion()
$data['image']['md'] = '![Alt text][1]
[1]: /path/to/img.jpg';

// Image with src + alt attributes in content
$data['image--in']['html'] = '<img src="/path/to/img.jpg" alt="Alt text" />';
$data['image--in']['md'] = '![Alt text](/path/to/img.jpg)';
$data['image--in']['linkPosition'] = Converter::LINK_IN_PARAGRAPH;

// Image with src + alt + title attributes
$data['image-title']['html'] = '<img src="/path/to/img.jpg" alt="Alt text" title="Optional title attribute" />';
$data['image-title']['md'] = '![Alt text][1]
[1]: /path/to/img.jpg "Optional title attribute"';

// Image with src + alt + title attributes in content
$data['image-title--in']['html'] = '<img src="/path/to/img.jpg" alt="Alt text" title="Optional title attribute" />';
$data['image-title--in']['md'] = '![Alt text](/path/to/img.jpg "Optional title attribute")';
$data['image-title--in']['linkPosition'] = Converter::LINK_IN_PARAGRAPH;

// Escaped image
$data['image-escape']['html'] = '![This link](/path)';
Expand Down

0 comments on commit 9eacd79

Please sign in to comment.